优秀的编程知识分享平台

网站首页 > 技术文章 正文

LINQ(1-3)操作SQL数据库案例练习-C#LINQ基础学习

nanyue 2024-08-09 07:08:32 技术文章 12 ℃

使用LINQ查询SQL Server 数据库

案例练习-多条件查询商品信息

创建一个Windows窗体,通过LINQ技术分别根据商品编号、商品名称和产地查询库存商品信息。在Form1窗体中添加一个ComboBox控件,用来选择查询条件,添加一个TextBox控件,用来输入查询关键字,添加一个Button控件,用来执行查询操作,再添加一个DataGriadView控件,用来显示数据中的数据。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LINQSelectSql
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //定义数据库连接字符串
        string strCon = "Data Source=souData-pc\\SQL2019;Database=db_use;Uid=sa;Pwd=123456;";
        linqtosqlClassDataContext linq;          //声明Linq连接对象

        private void Form1_Load(object sender, EventArgs e)
        {
            BindInfo();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            BindInfo();
        }

        #region 查询商品信息
        /// <summary>
        /// 查询商品信息
        /// </summary>
        private void BindInfo()
        {
            linq = new linqtosqlClassDataContext(strCon);    //创建Linq连接对象
            if (txtKeyWord.Text == "")
            {
                //获取所有商品信息
                var result = from info in linq.tb_stock
                             select new
                             {
                                 商品编号 = info.tradecode,
                                 商品全称 = info.fullname,
                                 商品型号 = info.type,
                                 商品规格 = info.standard,
                                 单位 = info.unit,
                                 产地 = info.produce,
                                 库存数量 = info.qty,
                                 进货时的最后一次进价 = info.price,
                                 加权平均价 = info.averageprice
                             };
                dgvInfo.DataSource = result;            //对DataGridView控件进行数据绑定
            }
            else
            {
                switch (cboxCondition.Text)
                {
                    case "商品编号":
                        //根据商品编号查询商品信息
                        var resultid = from info in linq.tb_stock
                                       where info.tradecode == txtKeyWord.Text
                                       select new
                                       {
                                           商品编号 = info.tradecode,
                                           商品全称 = info.fullname,
                                           商品型号 = info.type,
                                           商品规格 = info.standard,
                                           单位 = info.unit,
                                           产地 = info.produce,
                                           库存数量 = info.qty,
                                           进货时的最后一次进价 = info.price,
                                           加权平均价 = info.averageprice
                                       };
                        dgvInfo.DataSource = resultid;
                        break;
                    case "商品名称":
                        //根据商品名称查询商品信息
                        var resultname = from info in linq.tb_stock
                                         where info.fullname.Contains(txtKeyWord.Text)
                                         select new
                                         {
                                             商品编号 = info.tradecode,
                                             商品全称 = info.fullname,
                                             商品型号 = info.type,
                                             商品规格 = info.standard,
                                             单位 = info.unit,
                                             产地 = info.produce,
                                             库存数量 = info.qty,
                                             进货时的最后一次进价 = info.price,
                                             加权平均价 = info.averageprice
                                         };
                        dgvInfo.DataSource = resultname;
                        break;
                    case "产地":
                        //根据产地查询商品信息
                        var resultsex = from info in linq.tb_stock
                                        where info.produce == txtKeyWord.Text
                                        select new
                                        {
                                            商品编号 = info.tradecode,
                                            商品全称 = info.fullname,
                                            商品型号 = info.type,
                                            商品规格 = info.standard,
                                            单位 = info.unit,
                                            产地 = info.produce,
                                            库存数量 = info.qty,
                                            进货时的最后一次进价 = info.price,
                                            加权平均价 = info.averageprice
                                        };
                        dgvInfo.DataSource = resultsex;
                        break;
                }
            }
        }
        #endregion

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void label16_Click(object sender, EventArgs e)
        {

        }

        private void cboxCondition_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txtKeyWord_TextChanged(object sender, EventArgs e)
        {

        }

        private void dgvInfo_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

Tags:

最近发表
标签列表