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

字號:


    本文實例講述了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 條件表達(dá)式
    [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表中的幾個字段
    代碼如下:
    var query=from c in student select new {c.number,c.name,c.age};
    2、查詢student表中的幾個字段,并重新設(shè)定列名
    代碼如下:
    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、分組
    代碼如下:
    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é)生
    代碼如下:
    var query=from c in dc.Student where c.number.Contain("2009") select c
    查詢學(xué)號中含有2009字符的所有學(xué)生
    查詢結(jié)果
    LINQ的查詢結(jié)果有可能是一個對象,也有可能是一個數(shù)據(jù)集,可用var類型進(jìn)行接收
    如:
    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ù)行進(jìn)行篩選
    First( ):取得查詢結(jié)果的第一行
    Last( ):取得查詢結(jié)果的最后一行
    Take(n):取得查詢結(jié)果的前n行
    Skip(n):略過前n行,從n+1行開始取
    Skip(m).Take(n):從m+1行開始取后面的n行
    8、更新操作
    思路:先把需要更新的行查詢出來,然后進(jìn)行更新。LINQ只需要寫出查詢語句即可,不需要寫更新語句!
    例:將學(xué)生表中學(xué)號為00001的學(xué)生進(jìn)行更新
    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ù)庫中的字段名name
    stu.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表進(jìn)行插入操作。
    //注意,該函數(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();
    希望本文所述對大家asp.net程序設(shè)計有所幫助。