zoukankan      html  css  js  c++  java
  • Linq与数据库的连接显示查询(一)

    使用linq查询sql数据库是首先需要创建一个 linq  to  sql 类文件

    创建linq  to  sql的步骤:

    1在Visual  Studio 2015开发环境中建立一个目标框架 Framework  SDK4.6的项目

    2.在解决方案下的windows 窗体资源管理器下点击右键选择  添加 添加新项目

    :

    3.这个Student就是一个表名字将这个表拖至服务资源故那里器中.

    4前面起得名字会变为另外一种数据上下文类  比如:起得名字是student  则系统自动生成的数据上下文类是studentDateContext.

    5创建一个windows窗体应用程序,添加一个下拉框和文本框和DateGridView控件.

    下拉列表的名字叫做comboBox1文本框textBox1  DateGridView叫做DateGridView1

    代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace linqyongfa
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
             //连接数据库
            string strstu = " Data Source =.; Initial Catalog = Exam01WebService01; Integrated Security = True";
            //声明linq连接对象也即是数据上下文类
            DataClasses1DataContext linq;
            //from加载
            private void Form1_Load(object sender, EventArgs e)
            {
                Bindinfo();
            }
            //显示和按条件查询
            private void Bindinfo()
            {
                linq = new DataClasses1DataContext(strstu);
                if (textBox1.Text=="")
                {
                    var resault = from info in linq.Student
                                  select new
                                  {
                                      员工编号 = info.Id,
                                      姓名 = info.Name,
                                      性别 = info.Sex,
                                      电话 = info.Phone,
                                      邮箱 = info.Email,
                                      家乡 = info.HomePlace,
                                  };
                    dataGridView1.DataSource = resault;
                }
                else
                {
                    switch (comboBox1.Text)
                    {
                        case "员工编号"://根据员工编号查询
                            var resaultid = from info in linq.Student
                                            where info.Id == Convert.ToInt32(textBox1.Text)
                                            select new
                                            {
                                                员工编号 = info.Id,
                                                姓名 = info.Name,
                                                性别 = info.Sex,
                                                电话 = info.Phone,
                                                邮箱 = info.Email,
                                                家乡 = info.HomePlace,
                                            };
                            dataGridView1.DataSource = resaultid;
                            break;
                        case "姓名"://根据姓名查询
                            var resaultName = from info in linq.Student
                                              where info.Name == textBox1.Text
                                              select new
                                              {
                                                  员工编号 = info.Id,
                                                  姓名 = info.Name,
                                                  性别 = info.Sex,
                                                  电话 = info.Phone,
                                                  邮箱 = info.Email,
                                                  家乡 = info.HomePlace,
                                              };
                            dataGridView1.DataSource = resaultName;
                            break;
                        case "地址"://根据地址搜索
                            var resaultHomePlace = from info in linq.Student
                                              where info.HomePlace == textBox1.Text
                                              select new
                                              {
                                                  员工编号 = info.Id,
                                                  姓名 = info.Name,
                                                  性别 = info.Sex,
                                                  电话 = info.Phone,
                                                  邮箱 = info.Email,
                                                  家乡 = info.HomePlace,
                                              };
                            dataGridView1.DataSource = resaultHomePlace;
                            break;
                        default:
    
                            break;
                    }
    
                }
                
            }
    
           
        }
    }
                                            
  • 相关阅读:
    vtk 矩阵管理系统
    在opengl中使用纹理
    [译文]:单元测试的七种境界
    [翻译]:六分钟八法则塑造优秀程序员
    weekly review 200921: Power Sleep
    Bye, Scofield
    weekly review 200922: Goal
    weekly review 200920: Prototype Demo
    转载:测试驱动开发三原则
    weekly review 200918: productive
  • 原文地址:https://www.cnblogs.com/zsznh/p/10554159.html
Copyright © 2011-2022 走看看