|
系列文章導(dǎo)航:
一步一步學(xué)Linq to sql(一):預(yù)備知識(shí)
一步一步學(xué)Linq to sql(二):DataContext與實(shí)體
一步一步學(xué)Linq to sql(五):存儲(chǔ)過程
一步一步學(xué)Linq to sql(七):并發(fā)與事務(wù)
一步一步學(xué)Linq to sql(八):繼承與關(guān)系
一步一步學(xué)Linq to sql(九):其它補(bǔ)充
一步一步學(xué)Linq to sql(十):分層構(gòu)架的例子
外部映射文件
我們可以使用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盤下得到一個(gè)xml映射文件和C#的實(shí)體類代碼
4、把.cs文件添加到項(xiàng)目中來(放到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、現(xiàn)在就可以照常進(jìn)行其它工作了。使用sqlmetal可以很方便的同步數(shù)據(jù)庫與實(shí)體和映射文件。每次修改數(shù)據(jù)庫結(jié)構(gòu),從dbml設(shè)計(jì)器上刪除表、存儲(chǔ)過程然后再重新添加也是很麻煩的事情。
處理空值
var count = (from c in ctx.Customers where c.Region == null select c).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/>"); } |
代碼執(zhí)行后捕獲到下面的SQL被執(zhí)行:
SELECT COUNT(*) AS [value] FROM [dbo].[Customers] AS [t0] WHERE [t0].[Region] IS NULL
SELECT [t0].[ReportsTo] FROM [dbo].[Employees] AS [t0] |
系列文章導(dǎo)航:
一步一步學(xué)Linq to sql(一):預(yù)備知識(shí)
一步一步學(xué)Linq to sql(二):DataContext與實(shí)體
一步一步學(xué)Linq to sql(五):存儲(chǔ)過程
一步一步學(xué)Linq to sql(七):并發(fā)與事務(wù)
一步一步學(xué)Linq to sql(八):繼承與關(guān)系
一步一步學(xué)Linq to sql(九):其它補(bǔ)充
一步一步學(xué)Linq to sql(十):分層構(gòu)架的例子
已編譯查詢
對(duì)于一些在項(xiàng)目中經(jīng)常被用到的查詢可以封裝成已編譯查詢,這樣就能提高執(zhí)行效率:
static class Queries { public static Func<NorthwindDataContext, string, IQueryable<Customer>> CustomersByCity = CompiledQuery.Compile((NorthwindDataContext ctx, string city) => from c in ctx.Customers where c.City == city select c); } |
調(diào)用查詢方式如下:
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("數(shù)據(jù)庫:" + ctx.Mapping.DatabaseName + "<br/>"); Response.Write("表:" + ctx.Mapping.GetTable(typeof(Customer)).TableName + "<br/>"); Response.Write("表達(dá)式:" + query.Expression.ToString() + "<br/>"); Response.Write("sql:" + query.Provider.ToString() + "<br/>"); |
上面的代碼執(zhí)行結(jié)果如下:
Provider類型:System.Data.Linq.SqlClient.SqlProvider |
系列文章導(dǎo)航:
一步一步學(xué)Linq to sql(一):預(yù)備知識(shí)
一步一步學(xué)Linq to sql(二):DataContext與實(shí)體
一步一步學(xué)Linq to sql(五):存儲(chǔ)過程
一步一步學(xué)Linq to sql(七):并發(fā)與事務(wù)
一步一步學(xué)Linq to sql(八):繼承與關(guān)系
一步一步學(xué)Linq to sql(九):其它補(bǔ)充
一步一步學(xué)Linq to sql(十):分層構(gòu)架的例子
撤銷提交
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)); |
上面的代碼執(zhí)行效果如下:
Name:zhuye,Country:Shanghai |
批量操作
下面的代碼會(huì)導(dǎo)致提交N次DELETE操作:
var query = from c in ctx.Customers select c; ctx.Customers.RemoveAll(query); ctx.SubmitChanges(); |
應(yīng)該使用sql語句進(jìn)行批操作:
string sql = String.Format("delete from {0}", ctx.Mapping.GetTable(typeof(Customer)).TableName); ctx.ExecuteCommand(sql); |
對(duì)于批量更新操作也是同樣道理。
本文將會(huì)不斷補(bǔ)充其它點(diǎn)滴。最后一篇將會(huì)結(jié)合分層分布式應(yīng)用給出一個(gè)實(shí)際的項(xiàng)目。
it知識(shí)庫:一步一步學(xué)Linq to sql(九):其它補(bǔ)充,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。