Linq to sql(九):其它补充
来源:互联网 发布:winner 知乎 编辑:程序博客网 时间:2024/06/08 01:22
外部映射文件
我们可以使用sqlmetal命令行工具来生成外部映射文件,使用方法如下:
1、开始菜单 -》 VS2008 -》VS工具 -》VS2008命令行提示
2、输入命令:
D:\Program Files\Microsoft Visual Studio 9.0\VC>sqlmetal /conn:server=xxx;
database=Northwind;uid=xxx;pwd=xxx /map:c:\northwind.map /code:c:\northwind.cs
3、这样,我们就可以在C盘下得到一个xml映射文件和C#的实体类代码
4、把.cs文件添加到项目中来(放到App_Code目录),然后使用下面的代码加载映射文件:
String path = @"C:\Northwind.map";
XmlMappingSource xms = XmlMappingSource.FromXml(File.ReadAllText(path));
Northwind ctx = new Northwind("server=xxx;database=Northwind;uid=xxx;pwd=xxx", xms);
5、现在就可以照常进行其它工作了。使用sqlmetal可以很方便的同步数据库与实体和映射文件。每次修改数据库结构,从dbml设计器上删除表、存储过程然后再重新添加也是很麻烦的事情。
处理空值
var count = (from c in ctx.Customers where c.Region == null selectc).Count();
Response.Write(count + "<br/>");
var query = from emp in ctx.Employees select emp.ReportsTo;
foreach (Nullable<int> r in query)
{
Response.Write(r.HasValue ? r.Value.ToString() + "<br/>" : "没有<br/>");
}
代码执行后捕获到下面的SQL被执行:
SELECT COUNT(*) AS [value]
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[Region] IS NULL
SELECT [t0].[ReportsTo]
FROM [dbo].[Employees] AS [t0]
已编译查询
对于一些在项目中经常被用到的查询可以封装成已编译查询,这样就能提高执行效率:
static class Queries
{
public static Func<NorthwindDataContext, string, IQueryable<Customer>>
CustomersByCity = CompiledQuery.Compile((NorthwindDataContext ctx, stringcity) => from c in ctx.Customers where c.City == city select c);
}
调用查询方式如下:
GridView1.DataSource = Queries.CustomersByCity(ctx, "London");
GridView1.DataBind();
获取一些信息
var query = from c in ctx.Customers select c;
Response.Write("Provider类型:" + ctx.Mapping.ProviderType + "<br/>");
Response.Write("数据库:" + ctx.Mapping.DatabaseName + "<br/>");
Response.Write("表:" + ctx.Mapping.GetTable(typeof(Customer)).TableName + "<br/>");
Response.Write("表达式:" + query.Expression.ToString() + "<br/>");
Response.Write("sql:" + query.Provider.ToString() + "<br/>");
上面的代码执行结果如下:
Provider类型:System.Data.Linq.SqlClient.SqlProvider
数据库:Northwind
表:dbo.Customers
表达式:Table(Customer).Select(c => c)
sql: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]
撤销提交
var customer = ctx.Customers.Single(c => c.CustomerID == "AROUT");
customer.ContactName = "zhuye";
customer.Country = "Shanghai";
Response.Write(string.Format("Name:{0},Country:{1}<br/>", customer.ContactName, customer.Country));
customer = ctx.Customers.GetOriginalEntityState(customer);
Response.Write(string.Format("Name:{0},Country:{1}<br/>", customer.ContactName, customer.Country));
上面的代码执行效果如下:
Name:zhuye,Country:Shanghai
Name:Thomas Hardy,Country:UK
批量操作
下面的代码会导致提交N次DELETE操作:
var query = from c in ctx.Customers select c;
ctx.Customers.RemoveAll(query);
ctx.SubmitChanges();
应该使用sql语句进行批操作:
string sql = String.Format("delete from {0}", ctx.Mapping.GetTable(typeof(Customer)).TableName);
ctx.ExecuteCommand(sql);
对于批量更新操作也是同样道理。
本文将会不断补充其它点滴。最后一篇将会结合分层分布式应用给出一个实际的项目。
转载原文http://www.cnblogs.com/lovecherry/archive/2007/08/22/865980.html
- Linq to sql(九):其它补充
- Linq to Sql(九):其它补充
- Linq to sql(九):其它补充
- Linq to sql(九):其它补充
- 一步一步学Linq to sql(九):其它补充
- 一步一步学Linq to sql(九):其它补充
- 一步一步学Linq to sql(九):其它补充
- 一步一步学Linq to sql(九):其它补充
- 一步一步学Linq to sql(九):其它补充
- 一步一步学Linq to sql(九):其它补充
- 一步一步学Linq to sql(九):其它补充
- 一步一步学Linq to sql(九):其它补充
- Linq to Sql 学习系列之九 其它补充
- linq,linq to sql
- Linq初探--Linq to SQL
- 在LINQ to SQL中处理“更新已被其它用户删除对象”的错误
- linq to sql 学习(6)linq的分页实例
- LinQ&EF任我行(三)--LinQ to SQL
- EXT4.1翻译心得
- Spring如何与struts2结合
- java中资源读取
- Foursquare关闭把妹应用API
- 三句话影响人的一生【转】
- Linq to sql(九):其它补充
- Delphi下的接口编程
- 计算机视觉领域的一些牛人博客,超有实力的研究机构等的网站链接
- 中石化洛阳加油站被指油品加水 回应称可能渗水
- Cross-Site Scripting(XSS): 跨站脚本攻击介绍
- Linq to sql(十):分层构架的例子
- Linux下crontab命令的用法
- jquery ajax提交表单数据的两种方式
- 虚函数与抽象类的区别