一步一步學Linq to sql(九) 其它補充

2021-05-23 18:17:37 字數 3785 閱讀 1759

外部對映檔案

我們可以使用

sqlmetal

命令列工具來生成外部對映檔案,使用方法如下: 1

、開始選單

-》vs2008

-》vs

工具-》

vs2008

命令列提示 2

、輸入命令:

d:/program files/microsoft visual studio 9.0/vc>sqlmetal /conn:server=***;

database=northwind;uid=***;pwd=*** /map:c:/northwind.map /code:c:/northwind.cs

3、這樣,我們就可以在

c盤下得到乙個

xml對映檔案和

c#的實體類** 4

、把.cs

檔案新增到專案中來(放到

目錄),然後使用下面的**載入對映檔案:

string

path =

@"c:/northwind.map"

;xms =

.fromxml(

file

.readalltext(path));

northwind

ctx =

newnorthwind

("server=***;database=northwind;uid=***;pwd=***"

, xms);

5、現在就可以照常進行其它工作了。使用

sqlmetal

可以很方便的同步資料庫與實體和對映檔案。每次修改資料庫結構,從

dbml

設計器上刪除表、儲存過程然後再重新新增也是很麻煩的事情。

處理空值

varcount = (

from

c in

ctx.customers

where

c.region ==

null

select

c).count();

response.write(count + ""

); varquery =

from

emp

inctx.employees

select

emp.reportsto;

foreach

(nullable

<

int> r

inquery)

**執行後捕獲到下面的

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

呼叫查詢方式如下:

gridview1.datasource =

queries

.customersbycity(ctx,

"london"

); gridview1.databind();

獲取一些資訊

varquery =

from

c in

ctx.customers

select

c; response.write(

"provider

型別:"""

); response.write(

"資料庫:""

");response.write("表:

"typeof

(customer

)).tablename + ""

); response.write(

"表示式:

"+ query.expression.tostring() + ""

); response.write(

"sql:"

+ query.provider.tostring() + ""

); 上面的**執行結果如下:

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]

窗體頂端

窗體底端

撤銷提交

varcustomer = ctx.customers.single(c => c.customerid ==

"arout"

); customer.contactname =

"zhuye"

; customer.country =

"shanghai"

; response.write(

string

.format(

"name:,country:

", customer.contactname, customer.country));

customer = ctx.customers.getoriginalentitystate(customer);

response.write(

string

.format(

"name:,country:

", customer.contactname, customer.country));

上面的**執行效果如下:

name:zhuye,country:shanghai

name:thomas hardy,country:uk

批量操作

下面的**會導致提交n次

delete

操作:

varquery =

from

c in

ctx.customers

select

c; ctx.customers.removeall(query);

ctx.submitchanges();

應該使用

sql語句進行批操作:

string

sql =

string

.format(

"delete from "

typeof

(customer

)).tablename);

ctx.executecommand(sql);

對於批量更新操作也是同樣道理。

本文將會不斷補充其它點滴。最後一篇將會結合分層分布式應用給出乙個實際的專案。

一步一步學Linq to sql

一步一步學linq to sql 一 預備知識 一步一步學linq to sql 二 datacontext與實體 一步一步學linq to sql 三 增刪改 一步一步學linq to sql 四 查詢句法 一步一步學linq to sql 五 儲存過程 一步一步學linq to sql 六 特性...

一步一步學Linq to sql(九) 其它補充

外部對映檔案 我們可以使用 sqlmetal 命令列工具來生成外部對映檔案,使用方法如下 1 開始選單 vs2008 vs 工具 vs2008 命令列提示 2 輸入命令 d program files microsoft visual studio 9.0 vc sqlmetal conn serv...

一步一步學Linq to sql(九) 其它補充

外部對映檔案 我們可以使用sqlmetal命令列工具來生成外部對映檔案,使用方法如下 1 開始選單 vs2008 vs工具 vs2008命令列提示 2 輸入命令 d program files microsoft visual studio 9.0 vc sqlmetal conn server d...