国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net使用LINQ to SQL連接數(shù)據(jù)庫及SQL操作語句用法分析

2024-07-10 13:30:38
字體:
供稿:網(wǎng)友
這篇文章主要介紹了asp.net使用LINQ to SQL連接數(shù)據(jù)庫及SQL操作語句用法,較為詳細的分析了LINQ操作sql語句的功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下
 

本文實例講述了asp.net使用LINQ to SQL連接數(shù)據(jù)庫及SQL操作語句用法。分享給大家供大家參考,具體如下:

LINQ簡介

LINQ:語言集成查詢(Language INtegrated Query)是一組用于c#和Visual Basic語言的擴展。它允許編寫C#或者Visual Basic代碼以查詢數(shù)據(jù)庫相同的方式操作內(nèi)存數(shù)據(jù)。

LINQ是一門查詢語言,和SQL一樣,通過一些關(guān)鍵字的組合,實現(xiàn)最終的查詢。

LINQ的分類

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

命名空間為System.Linq;

LINQ查詢

語法:
from 臨時變量 in 集合對象或數(shù)據(jù)庫對象 
  where 條件表達式 
  [orderby條件] 
  [group by 條件] 
  select 臨時變量中被查詢的值

例:

from c in Student select c;

假設(shè)Student是一個數(shù)據(jù)庫表對應(yīng)的一個實體類

則查詢語句為:

from c in Student select c; //整表查詢from c in Student where c.name=="張三" select c;//查詢姓名為張三的所有信息

其中C為臨時變量,可任意取。

查詢幾個字段

1、查詢student表中的幾個字段

復(fù)制代碼代碼如下:
var query=from c in student  select new {c.number,c.name,c.age};

 

2、查詢student表中的幾個字段,并重新設(shè)定列名

復(fù)制代碼代碼如下:
var query=from c in student select new {學(xué)號=c.number,姓名=c.name, 年領(lǐng)=c.age};

注意事項

 

linq查詢語句必須以from子句開始,以select 子句結(jié)束。

Linq是在.NET Framework 3.5 中出現(xiàn)的技術(shù),所以在創(chuàng)建新項目的時候必須要選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、分組

復(fù)制代碼代碼如下:
var  query=from c in student group c by c.sex into d select new {性別=c.age}; //d為新表,c.sex為分組字段

5、過濾重復(fù)記錄

 

var query=(from c in dc.student select new {c.place}).Distinct();//Distinct()的作用是過濾重復(fù)的記錄。var query=(from c in dc.student select new {分布地區(qū)=c.place}).Distinct();

6、查詢行數(shù)

(1)查詢表的總行數(shù)

int count=student.count();

(2)查詢滿足條件的行數(shù)

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

7、模糊查詢

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

查詢姓名中含有王字的所有學(xué)生

復(fù)制代碼代碼如下:
var query=from c in dc.Student where c.number.Contain("2009") select c

 

查詢學(xué)號中含有2009字符的所有學(xué)生

查詢結(jié)果

LINQ的查詢結(jié)果有可能是一個對象,也有可能是一個數(shù)據(jù)集,可用var類型進行接收

如:

var query=from c in Student select c;

輸入結(jié)果可用foreach循環(huán)

如:

var query=from c in Student select c;foreach( var x in query){ Response.Write(x.toString());}

常用函數(shù)

Count( ):計算查詢結(jié)果的行數(shù)
Distinct( ):對查詢結(jié)果的重復(fù)行進行篩選
First( ):取得查詢結(jié)果的第一行
Last( ):取得查詢結(jié)果的最后一行
Take(n):取得查詢結(jié)果的前n行
Skip(n):略過前n行,從n+1行開始取
Skip(m).Take(n):從m+1行開始取后面的n行

8、更新操作

思路:先把需要更新的行查詢出來,然后進行更新。LINQ只需要寫出查詢語句即可,不需要寫更新語句!

例:將學(xué)生表中學(xué)號為00001的學(xué)生進行更新

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

在數(shù)據(jù)空間中顯示數(shù)據(jù)查詢結(jié)果:

前兩行是連接數(shù)據(jù)庫,其中第一中,經(jīng)常用,可以放到最開始,這樣就不必每次用到時都寫了。

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(); //將要更新學(xué)號為多少的相關(guān)信息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后根據(jù)主鍵來更新,其他字段不能。即通過獲取主鍵后,來更新其他字段。//除過主鍵不修改外,其他字段都可以修改stu.name = name;//將新修改的名字賦值給數(shù)據(jù)庫中的字段名namestu.age = age;//修改年齡dc.SubmitChanges();//真正的用于修改數(shù)據(jù)庫。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();//創(chuàng)建對象//賦新值//主鍵不能重復(fù)stu.number = num;stu.name = username;stu.sex = sex;stu.place = place;stu.age = age;dc.student.InsertOnSubmit(stu);//對表studen表進行插入操作。//注意,該函數(shù)必須寫正確。dc.SubmitChanges();//數(shù)據(jù)庫保存bind();//內(nèi)容和上面的相同

10、數(shù)據(jù)刪除

string num = TextBox6.Text.Trim();student stu =(from c in dc.student where c.number == num select c).First();dc.student.DeleteOnSubmit(stu);//刪除數(shù)據(jù)庫中的字段,具體怎樣刪除不管,只管調(diào)用該函數(shù)即可。dc.SubmitChanges();bind();
 


注:相關(guān)教程知識閱讀請移步到ASP.NET教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 资源县| 天长市| 江门市| 墨玉县| 法库县| 阿坝| 望谟县| 彩票| 黄浦区| 阿城市| 西林县| 茂名市| 上思县| 阜南县| 甘南县| 台北县| 治县。| 汉川市| 横峰县| 洪雅县| 香河县| 宁陵县| 荥经县| 石阡县| 噶尔县| 息烽县| 资中县| 尼木县| 青铜峡市| 且末县| 元朗区| 泾源县| 蒲江县| 五寨县| 晋宁县| 鄂伦春自治旗| 洞头县| 隆回县| 郸城县| 连云港市| 鹤庆县|