不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
LINQ to SQL简单示例(dbml版)
编辑:dnawo 日期:2011-08-10
今天我们以一个控制台项目为示例,演示下怎么使用LINQ to SQL。
一、LINQ to SQL简单示例
1.在服务器资源管理器添加示例数据库Northwind连接

2.为项目添加一个LINQ to SQL类

3.将Customers表拖动到LINQ to SQL类的O/R 设计器上

4.LINQ to SQL增、删、改记录示例
①.查询记录[1]
SQL Server Profiler监测结果:
引用内容
②.增加记录
SQL Server Profiler监测结果:
引用内容
③.修改记录
SQL Server Profiler监测结果:
引用内容
④.删除记录
SQL Server Profiler监测结果:
引用内容
二、补充说明
[1].使用.NET 3.5中IEnumerable<TSource>的扩展方法也可查询
三、官方文档
@.LINQ to SQL:http://msdn.microsoft.com/zh-cn/library/bb386976(v=VS.90).aspx
一、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);
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();
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();
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();
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);
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 | 引用: 0 | 查看次数: 4654
发表评论
请登录后再发表评论!