欢迎光临

木子屋

标题: LINQ to SQL简单示例(dbml版)

作者: dnawo

日期: 2011-08-10 01:50 PM

分类: Win编程

内容: 今天我们以一个控制台项目为示例，演示下怎么使用LINQ to SQL。
一、LINQ to SQL简单示例
1.在服务器资源管理器添加示例数据库Northwind连接
2.为项目添加一个LINQ to SQL类
3.将Customers表拖动到LINQ to SQL类的O/R 设计器上
4.LINQ to SQL增、删、改记录示例
①.查询记录[1]
复制内容到剪贴板 程序代码//1.创建 DataContext 对象
NorthwindDataContext northwind = new NorthwindDataContext();
//2.创建查询
var customers = from row in northwind.Customers
where row.City == "London"
select row;
//3.执行查询
foreach (var row in customers)
Console.WriteLine(row.ContactName);
SQL Server Profiler监测结果：
引用内容exec sp_executesql N'Select [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax] FROM [dbo].[Customers] AS [t0] Where [t0].[City] = @p0',N'@p0 nvarchar(6)',@p0=N'London'
②.增加记录
复制内容到剪贴板 程序代码//1.创建 DataContext 对象
NorthwindDataContext northwind = new NorthwindDataContext();
//2.添加新记录
Customers customer = new Customers();
customer.CustomerID = "A0001";
customer.CompanyName = "KaiMo";
customer.ContactName = "dnawo";
customer.City = "London";
northwind.Customers.InsertOnSubmit(customer);
//3.更新到数据库
northwind.SubmitChanges();
SQL Server Profiler监测结果：
引用内容exec sp_executesql N'Insert INTO [dbo].[Customers]([CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax]) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10)',N'@p0 nchar(5),@p1 nvarchar(5),@p2 nvarchar(5),@p3 nvarchar(4000),@p4 nvarchar(4000),@p5 nvarchar(6),@p6 nvarchar(4000),@p7 nvarchar(4000),@p8 nvarchar(4000),@p9 nvarchar(4000),@p10 nvarchar(4000)',@p0=N'A0001',@p1=N'KaiMo',@p2=N'dnawo',@p3=NULL,@p4=NULL,@p5=N'London',@p6=NULL,@p7=NULL,@p8=NULL,@p9=NULL,@p10=NULL
③.修改记录
复制内容到剪贴板 程序代码//1.创建 DataContext 对象
NorthwindDataContext northwind = new NorthwindDataContext();
//2.修改记录
var customers = from row in northwind.Customers
where row.CustomerID == "A0001"
select row;
if (customers.Count() > 0)
customers.First().ContactName += "_001";
//3.更新到数据库
northwind.SubmitChanges();
SQL Server Profiler监测结果：
引用内容exec sp_executesql N'Update [dbo].[Customers] SET [ContactName] = @p4 Where ([CustomerID] = @p0) AND ([CompanyName] = @p1) AND ([ContactName] = @p2) AND ([ContactTitle] IS NULL) AND ([Address] IS NULL) AND ([City] = @p3) AND ([Region] IS NULL) AND ([PostalCode] IS NULL) AND ([Country] IS NULL) AND ([Phone] IS NULL) AND ([Fax] IS NULL)',N'@p0 nchar(5),@p1 nvarchar(5),@p2 nvarchar(5),@p3 nvarchar(6),@p4 nvarchar(9)',@p0=N'A0001',@p1=N'KaiMo',@p2=N'dnawo',@p3=N'London',@p4=N'dnawo_001'
④.删除记录
复制内容到剪贴板 程序代码//1.创建 DataContext 对象
NorthwindDataContext northwind = new NorthwindDataContext();
//2.删除记录
var customers = from row in northwind.Customers
where row.CustomerID == "A0001"
select row;
foreach (var row in customers)
northwind.Customers.DeleteOnSubmit(row);
//3.更新到数据库
northwind.SubmitChanges();
SQL Server Profiler监测结果：
引用内容exec sp_executesql N'Delete FROM [dbo].[Customers] Where ([CustomerID] = @p0) AND ([CompanyName] = @p1) AND ([ContactName] = @p2) AND ([ContactTitle] IS NULL) AND ([Address] IS NULL) AND ([City] = @p3) AND ([Region] IS NULL) AND ([PostalCode] IS NULL) AND ([Country] IS NULL) AND ([Phone] IS NULL) AND ([Fax] IS NULL)',N'@p0 nchar(5),@p1 nvarchar(5),@p2 nvarchar(9),@p3 nvarchar(6)',@p0=N'A0001',@p1=N'KaiMo',@p2=N'dnawo_001',@p3=N'London'
二、补充说明
[1].使用.NET 3.5中IEnumerable<TSource>的扩展方法也可查询
复制内容到剪贴板 程序代码//1.创建 DataContext 对象
NorthwindDataContext northwind = new NorthwindDataContext();
//2.创建查询
var customers = from row in northwind.Customers.Where(row => row.City == "London")
select row;
//3.执行查询
foreach (var row in customers)
Console.WriteLine(row.ContactName);
三、官方文档
@.LINQ to SQL：http://msdn.microsoft.com/zh-cn/library/bb386976(v=VS.90).aspx

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.836 ms

返回

标题: LINQ to SQL简单示例(dbml版)

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.836 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.852 ms

返回