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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

linq to sql简单使用

發(fā)布時間:2024/9/5 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linq to sql简单使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.新建一個winform項目。

2.添加一個Linq to Sql 類,命名為Northwind

3.打開服務(wù)器資源管理器,將表拖動到linq to sql 類,實體類就由Vs生成了

4.實例化DataContext類

class="brush:csharp;gutter:true;">NorthwindDataContext dc = new NorthwindDataContext();

5.下面就可以使用Linq to Sql啦。

a.查詢Employees所有數(shù)據(jù)

var ee =
from employee in dc.Employees
select employee;

b.加個Where條件,查詢Employee表所有City=London

var e2 =from employee in dc.Employeeswhere employee.City == "London" select employee;

?

c.查詢EmployeeID<5的數(shù)據(jù)

var e3 =from employee in dc.Employeeswhere employee.EmployeeID < 5 select employee;

?

d.查詢雇傭日期在1993年1月1日以前的人員。

var e4 =from employee in dc.Employeeswhere employee.HireDate < new DateTime(1993, 1, 1) select employee;

?

e.多個查詢條件

var e5 =from employee in dc.Employeeswhere employee.HireDate < new DateTime(1993, 1, 1) where employee.EmployeeID < 3 select employee;

?

f.like語句

var e6 =from employee in dc.Employeeswhere SqlMethods.Like(employee.FirstName, "%a%") //where employee.FirstName.Contains("a") //等價于 like '%a%' //where employee.FirstName.StartsWith("a") //等價于 like 'a%' //where employee.FirstName.EndsWith("a") //等價于 like '%a' select employee;

?

g.查詢指定的列

var e7 =(from employee in dc.Employees//select new { employee.EmployeeID, employee.FirstName }; select new { 員工編號 = employee.EmployeeID, 員工姓名 = employee.FirstName, 尊稱 = employee.TitleOfCourtesy });

?

h.子查詢

var e8 =from employee in dc.Employeeswhere employee.Orders.Count > 100 select employee;

?

i.內(nèi)連接

/*** 查詢位于倫敦的客戶的訂單* SELECT ** FROM [dbo].[Orders]* INNER JOIN [dbo].[Customers] ON [Customers].[CustomerID] = [Orders].[CustomerID]* WHERE [Customers].[City] = 'London'*/var e9 =from cus in dc.Customers join ord in dc.Orders on cus.CustomerID equals ord.CustomerID where cus.City == "London" select ord;

?

j.增加

Employees m = new Employees();m.LastName = "閃碩"; m.FirstName = ""; m.Title = "閃碩標(biāo)題"; dc.Employees.InsertOnSubmit(m); dc.SubmitChanges();

?

k.修改

var e10 = dc.Employees.FirstOrDefault(employee => employee.LastName == "閃碩");e10.City = "石家莊"; dc.SubmitChanges();

?

l.刪除

var e10 = dc.Employees.FirstOrDefault(employee => employee.LastName == "閃碩");dc.Employees.DeleteOnSubmit(e10);dc.SubmitChanges();

轉(zhuǎn)載于:https://www.cnblogs.com/caozhenqian/p/4600565.html

總結(jié)

以上是生活随笔為你收集整理的linq to sql简单使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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