日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

Linq to SQL学习

發(fā)布時(shí)間:2024/6/5 数据库 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linq to SQL学习 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

??? 在.net3.5中出現(xiàn)了一個(gè)很新的技術(shù),那就是Linq to SQL,一直聽(tīng)論壇里面的朋友說(shuō)這Linq to SQL做起事來(lái)很方便,如果用他的話就會(huì)喜歡上這個(gè),最近就懷著好奇的思想搞了一把,確實(shí)不錯(cuò),能做的東西很多,也很方便。下面是我的具體操作

一,?示例數(shù)據(jù)庫(kù)

字段名

字段類(lèi)型

允許空

字段說(shuō)明

ID

uniqueidentifier

?

表主鍵字段

UserName

varchar(50)

?

留言用戶名

PostTime

datetime

?

留言時(shí)間

Message

varchar(400)

留言內(nèi)容

IsReplied

bit

?

留言是否回復(fù)

Reply

varchar(400)

留言管理員回復(fù)

?????? 在數(shù)據(jù)庫(kù)中創(chuàng)建一個(gè)名為GuestBook的數(shù)據(jù)庫(kù),在里面創(chuàng)建一個(gè)tbGuestBook的表,結(jié)構(gòu)如上表。?

?

二,生成實(shí)體類(lèi)?

?????? 右鍵點(diǎn)擊網(wǎng)站項(xiàng)目,選擇添加新項(xiàng),然后選擇“Linq to sql Classes”,命名為GuestBook。然后打開(kāi)App_Code里面的GuestBook.dbml。設(shè)計(jì)視圖上的文字提示你可以從服務(wù)器資源管理器或者攻擊箱拖動(dòng)項(xiàng)到設(shè)計(jì)界面上來(lái)創(chuàng)建實(shí)體類(lèi)。

?????? 那么,我們就在服務(wù)器資源管理器中創(chuàng)建一個(gè)指向GuestBook數(shù)據(jù)庫(kù)的數(shù)據(jù)連接,然后把tbGuestBook表拖動(dòng)到GuestBook.dbml的設(shè)計(jì)視圖上,按CTRL+S保存。打開(kāi)GuestBook.designer.cs可以發(fā)現(xiàn)系統(tǒng)自動(dòng)創(chuàng)建了GuestBook數(shù)據(jù)庫(kù)中tbGuestBook表的映射。

?

三,相關(guān)的CRUD方法,基本包含常用的方法,如下

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.Linq; using Model; namespace Default { public partial class AllList : System.Web.UI.Page { GuestBookDataContext guestbook = new GuestBookDataContext("server=.;database=GuestBook;uid=sa;pwd=111"); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { LoadData(); } } void LoadData() { SetBindByBasic(); var resutl = from c in guestbook.tbGuestBook select new { 編號(hào) = c.ID, 姓名 = c.UserName, }; this.ddlID1.DataSource = resutl; this.ddlID1.DataValueField = "編號(hào)"; this.ddlID1.DataTextField = "編號(hào)"; this.ddlID1.DataBind(); this.ddlID2.DataSource = resutl; this.ddlID2.DataValueField = "編號(hào)"; this.ddlID2.DataTextField = "編號(hào)"; this.ddlID2.DataBind(); this.ddlID3.DataSource = resutl; this.ddlID3.DataValueField = "編號(hào)"; this.ddlID3.DataTextField = "編號(hào)"; this.ddlID3.DataBind(); } //************************************************************簡(jiǎn)單查詢******************************************** //基礎(chǔ)查詢,單項(xiàng)返回 void SetBindBySingle() { var resutl = from c in guestbook.tbGuestBook select c.ID; this.gv.DataSource = resutl; this.gv.DataBind(); } //基礎(chǔ)查詢 void SetBindByBasic() { var resutl = from c in guestbook.tbGuestBook select c; this.gv.DataSource = resutl; this.gv.DataBind(); } //where條件查詢 void SetBindByWhere() { var resutl = from c in guestbook.tbGuestBook where c.ID > 2 select c; this.gv.DataSource = resutl; this.gv.DataBind(); } //挑選列 void SetBindByNew() { var resutl = from c in guestbook.tbGuestBook select new { 姓名 = c.UserName, 時(shí)間 = c.PostTime, 消息 = c.Messages, }; this.gv.DataSource = resutl; this.gv.DataBind(); } //排除重復(fù) void SetBindByDistinct() { var resutl = (from c in guestbook.tbGuestBook select c.UserName).Distinct(); this.gv.DataSource = resutl; this.gv.DataBind(); } //排序 void SetBindByOrderby() { var resutl = from c in guestbook.tbGuestBook orderby c.UserName descending,c.ID ascending select new { 編號(hào) = c.ID, 姓名 = c.UserName, 消息 = c.Messages, }; this.gv.DataSource = resutl; this.gv.DataBind(); } //查詢包含 void SetBindByContains() { var resutl = from c in guestbook.tbGuestBook where c.UserName.Contains("s") select new { 編號(hào) = c.ID, 姓名 = c.UserName, 消息 = c.Messages, }; this.gv.DataSource = resutl; this.gv.DataBind(); } //查詢以什么開(kāi)頭 void SetBindByStartWith() { var resutl = from c in guestbook.tbGuestBook where c.UserName.StartsWith("s") select new { 編號(hào) = c.ID, 姓名 = c.UserName, 消息 = c.Messages, }; this.gv.DataSource = resutl; this.gv.DataBind(); } //行數(shù) void SetBindByTake() { var resutl = (from c in guestbook.tbGuestBook select new { 編號(hào) = c.ID, 姓名 = c.UserName, 消息 = c.Messages, }).Take(5); this.gv.DataSource=resutl; this.gv.DataBind(); } //分頁(yè)(排除前面的行數(shù)) void SetBindBySkip() { var resutl = (from c in guestbook.tbGuestBook select new { 編號(hào) = c.ID, 姓名 = c.UserName, 消息 = c.Messages, }).Skip(5); this.gv.DataSource = resutl; this.gv.DataBind(); } //ToList void SetBindByToList() { var resutl = (from c in guestbook.tbGuestBook select new { 編號(hào) = c.ID, 姓名 = c.UserName, 消息 = c.Messages, }).ToList(); this.gv.DataSource = resutl; this.gv.DataBind(); } //分組排序 void SetBindByGroup() { var resutl = from c in guestbook.tbGuestBook group c by c.UserName into g where g.Count()>2 orderby g.Count() select new { 編號(hào) = g.Key, 行數(shù) = g.Count(), }; this.gv.DataSource = resutl; this.gv.DataBind(); } //************************************************************組合查詢******************************************** //Union,過(guò)濾 void SetBindByUnion() { var resutl = (from c in guestbook.tbGuestBook where c.UserName.Contains("s") select c).Union (from c in guestbook.tbGuestBook where c.UserName.StartsWith("s") select c); this.gv.DataSource = resutl; this.gv.DataBind(); } //Contact,不過(guò)濾 void SetBindByConcat() { var resutl = (from c in guestbook.tbGuestBook where c.UserName.Contains("s") select c).Concat (from c in guestbook.tbGuestBook where c.UserName.StartsWith("s") select c); this.gv.DataSource = resutl; this.gv.DataBind(); } //Intersect,取相交項(xiàng) void SetBindByIntersect() { var resutl = (from c in guestbook.tbGuestBook where c.Messages.Contains("1") select c).Intersect (from c in guestbook.tbGuestBook where c.UserName.StartsWith("s") select c); this.gv.DataSource = resutl; this.gv.DataBind(); } //Except,排除相交項(xiàng) void SetBindByExcept() { var resutl = (from c in guestbook.tbGuestBook where c.Messages.Contains("1") select c).Except (from c in guestbook.tbGuestBook where c.UserName.StartsWith("s") select c); this.gv.DataSource = resutl; this.gv.DataBind(); } //子查詢 void SetBindByChildren() { var resutl = from c in guestbook.tbGuestBook where (from b in guestbook.tbGuestBook where b.Messages == "1" select b.UserName).Contains("1") select c; this.gv.DataSource = resutl; this.gv.DataBind(); } //數(shù)組子查詢 void SetBindByArray() { var resutl = from c in guestbook.tbGuestBook where new string[]{"1","sdf"}.Contains(c.UserName) select c; this.gv.DataSource = resutl; this.gv.DataBind(); } //內(nèi)連接,排除相同的 void SetBindByJoin() { var resutl = (from c in guestbook.tbGuestBook join g in guestbook.tbGuestBook on c.UserName equals g.Messages select c).Distinct(); this.gv.DataSource = resutl; this.gv.DataBind(); } //************************************************************高級(jí)查詢******************************************** //存儲(chǔ)過(guò)程 void SetBindByProc() { var resutl = from c in guestbook.sp_singleresultset() select c; this.gv.DataSource = resutl; this.gv.DataBind(); } //存儲(chǔ)過(guò)程,帶參數(shù) void SetBindByProcParts() { int ?resutl = -1; guestbook.sp_withparameter("1", ref resutl); this.gv.DataSource = resutl.ToString(); this.gv.DataBind(); } //延遲執(zhí)行,如果使用兩次循環(huán)輸出的話,會(huì)把兩次都輸出,最好的做法就是把前一個(gè)用ToList()存下來(lái) void SetBindByQueryLater() { IQueryable query = from c in guestbook.tbGuestBook select c; foreach (tbGuestBook gb in query) Response.Write(gb.ID + "-----" + gb.Messages); Response.Write("<br/>"); foreach (tbGuestBook gb in query) Response.Write(gb.ID + "-----" + gb.Messages); } //單條數(shù)據(jù) void SetBindBySingle1() { tbGuestBook tb = guestbook.tbGuestBook.Single(resutl => resutl.ID == 2); Response.Write(tb.ID + "-----" + tb.Messages); } //************************************************************刪除,添加,修改******************************************** void Delete() { tbGuestBook gb = guestbook.tbGuestBook.Single(c =>c.ID == int.Parse(this.ddlID1.SelectedValue)); guestbook.tbGuestBook.DeleteOnSubmit(gb); guestbook.SubmitChanges(); } void Update() { tbGuestBook gb = guestbook.tbGuestBook.Single(c => c.ID == int.Parse(this.ddlID2.SelectedValue)); gb.Messages=this.txtMessage1.Text; guestbook.SubmitChanges(); } void Insert() { tbGuestBook gb = new tbGuestBook(); gb.UserName = this.txtName.Text; gb.PostTime = DateTime.Now; gb.Messages = this.txtMessage2.Text; gb.IsReplied = false; gb.Reply = ""; guestbook.tbGuestBook.InsertOnSubmit(gb); guestbook.SubmitChanges(); } protected void btnDelete_Click(object sender, EventArgs e) { Delete(); LoadData(); } protected void btnUpdate_Click(object sender, EventArgs e) { Update(); LoadData(); } protected void btnInsert_Click(object sender, EventArgs e) { Insert(); LoadData(); } } }

?

轉(zhuǎn)載于:https://www.cnblogs.com/nlx0201/archive/2010/11/03/1908133.html

總結(jié)

以上是生活随笔為你收集整理的Linq to SQL学习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。