本文實例講述了asp.net使用LINQ to SQL連接數據庫及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表中的幾個字段
2、查詢student表中的幾個字段,并重新設定列名
注意事項
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、分組
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
查詢姓名中含有王字的所有學生
查詢學號中含有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=="00001" select 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;//將新修改的名字賦值給數據庫中的字段名namestu.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();