LINQ to SQL连接数据库及语句


LINQ简介

LINQ:语言集成查询(Language INtegrated Query)是一组用于c#和Visual Basic语言的扩展。它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作内存数据。

LINQ是一门查询语言,和SQL一样,通过一些关键字的组合,实现最终的查询。

LINQ的分类

LINQ to Object
LINQ to XML
LINQ to SQL
LINQ to DataSet
LINQ to ADO.NET

命名空间为System.Linq;

LINQ查询

语法:
from 临时变量 in 集合对象或数据库对象
  where 条件表达式
  [orderby条件]
  [group by 条件]
  select 临时变量中被查询的值

例:from c in Student select c;


假设Student是一个数据库表对应的一个实体类

则查询语句为:
from c in Student select c; 
 //整表查询

from c in Student where c.name==“张三“ select c;  //查询姓名为张三的所有信息

其中C为临时变量,可任意取。

查询几个字段

1、查询student表中的几个字段

var query=from c in student  select new {c.number,c.name,c.age};

2、查询student表中的几个字段,并重新设定列名

var query=from c in student select new {学号=c.number,姓名=c.name, 年领=c.age};

注意事项

linq查询语句必须以from子句开始,以select 子句结束。
Linq是在.NET Framework 3.5 中出现的技术,所以在创建新项目的时候必须要选3.5或者更高版本,否则无法使用。

3、排序

var query=from c in student  orderby c.age ascending  select c;  //升序

var query=from c in studeng orderby c.age descending  select c;//降序

4、分组

var  query=from c in student group c by c.sex into d select new {性别=c.age}; //d为新表,c.sex为分组字段

5、过滤重复记录

var query=(from c in dc.student  select new {c.place}).Distinct();//Distinct()的作用是过滤重复的记录。
var query=(from c in dc.student select new {分布地区=c.place}).Distinct();

6、查询行数

(1)查询表的总行数

int count=student.count();

(2)查询满足条件的行数

int count=(from c in  student  where c.name=="王明" select c).count();

7、模糊查询

from c in dc.Student where c.name.Contain(“王”) select c

查询姓名中含有王字的所有学生

var query=from c in dc.Student where c.number.Contain(“2009”) select c

查询学号中含有2009字符的所有学生

查询结果

LINQ的查询结果有可能是一个对象,也有可能是一个数据集,可用var类型进行接收
如:var query=from c in Student select c;
输入结果可用foreach循环
如:var query=from c in Student select c;
       foreach( var x in query)
       { Response.Write(x.toString());}

常用函数

Count( ):计算查询结果的行数 Distinct( ):对查询结果的重复行进行筛选 First( ):取得查询结果的第一行 Last( ):取得查询结果的最后一行 Take(n):取得查询结果的前n行 Skip(n):略过前n行,从n+1行开始取 Skip(m).Take(n):从m+1行开始取后面的n行

8、更新操作

思路:先把需要更新的行查询出来,然后进行更新。LINQ只需要写出查询语句即可,不需要写更新语句!
例:将学生表中学号为00001的学生进行更新

1、(from c in Stuent where c.id==“00001select c).First();

在数据空间中显示数据查询结果:

//前两行是连接数据库,其中第一中,经常用,可以放到最开始,这样就不必每次用到时都写了。

studentDataContext dc = new studentDataContext(); 注意://xxxDataContext需要与.dbml的文件名一致

var query=from c in dc.student select c; GridView1.DataSource=query; GridView1.DataBind();

更新操作
       

string num = TextBox2.Text.Trim();  //将要更新学号为多少的相关信息
        string name = TextBox3.Text.Trim();//更新的姓名
        int age = Convert.ToInt32(TextBox4.Text.Trim());//Int32整型 //更新的年龄
         StudentDataContext  dc=new StudentDataContext(); student stu=(from c in dc.student where c.number==num select c).First();//变量,选取第一行。where后根据主键来更新,其他字段不能。即通过获取主键后,来更新其他字段。 //除过主键不修改外,其他字段都可以修改
        stu.name = name;//将新修改的名字赋值给数据库中的字段名name
        stu.age = age;//修改年龄
        dc.SubmitChanges();//真正的用于修改数据库。
        bind();//一更改,就显示,和及时刷新相同。

 

 private void bind() { studentDataContext dc = new studentDataContext(); var query = (from c in dc.student select c);    //全表查询
 GridView1.DataSource = query; GridView1.DataBind(); }

9、插入操作

 

//插入
string num = TextBox1.Text.Trim(); string username = TextBox2.Text.Trim(); string sex = TextBox3.Text.Trim(); string place = TextBox4.Text.Trim(); int age = Convert.ToInt32(TextBox5.Text.Trim()); student stu = new student();//创建对象 //赋新值 //主键不能重复
 stu.number = num; stu.name = username; stu.sex = sex; stu.place = place; stu.age = age; dc.student.InsertOnSubmit(stu);//对表studen表进行插入操作。//注意,该函数必须写正确。
 dc.SubmitChanges();//数据库保存
 bind();//内容和上面的相同

10、数据删除

 string num = TextBox6.Text.Trim(); student stu =(from c in dc.student where c.number == num select c).First(); dc.student.DeleteOnSubmit(stu);//删除数据库中的字段,具体怎样删除不管,只管调用该函数即可。
 dc.SubmitChanges(); bind();

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM