日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Enterprise Library v5.0 -- Data Access Appli

系統(tǒng) 1917 0

?


微軟企業(yè)庫(kù) Enterprise Library 5.0 正式發(fā)布!!!
Enterprise Library 5.0 開發(fā)向?qū)? 簡(jiǎn)介(1)
Enterprise Library v5.0 -- Data Access Application Block 開發(fā)向?qū)В?)

檢索數(shù)據(jù)對(duì)象

現(xiàn)代程序開發(fā)都關(guān)注“數(shù)據(jù)對(duì)象”,使用數(shù)據(jù)傳輸對(duì)象(DTO)在應(yīng)用程序?qū)娱g傳遞數(shù)據(jù),使用ORM(Object/Relations Mapping)實(shí)現(xiàn)數(shù)據(jù)訪問(wèn)層,或者充分利用客戶端數(shù)據(jù)訪問(wèn)技術(shù),如LINQ等等。

DAAB提供了使用SQL語(yǔ)句或存儲(chǔ)過(guò)程檢索數(shù)據(jù)的功能,返回的數(shù)據(jù)實(shí)現(xiàn)了IEnumerable接口的對(duì)象序列。

關(guān)于Accessors

DAAB數(shù)據(jù)訪問(wèn)塊提供了2個(gè)核心類來(lái)執(zhí)行這一查詢:SprocAccessor和SqlStringAccessor。你可以使用Database 類的ExecuteSprocAccessor和ExecuteSqlStringAccessor方法,在一個(gè)方法中創(chuàng)建和執(zhí)行這些Accessors。

Accessors使用2個(gè)其他的對(duì)象來(lái)管理你想傳遞給accessor的參數(shù),并將從數(shù)據(jù)庫(kù)返回的數(shù)據(jù)行映射為對(duì)象屬性,然后返回給客戶端代碼。下圖演示了整個(gè)流程。

如果你不指定參數(shù)mapper,accessor將自動(dòng)使用默認(rèn)的mapper來(lái)解析參數(shù),但是這一特性僅僅適用于SQL Server或Oracle數(shù)據(jù)庫(kù)的存儲(chǔ)過(guò)程。當(dāng)你使用SQL語(yǔ)句或者其他數(shù)據(jù)庫(kù)時(shí),你必須知道定制的參數(shù)mapper,負(fù)責(zé)解析參數(shù)。

如果你只指定輸出的mapper,DAAB將使用默認(rèn)的映射構(gòu)造類,映射返回的數(shù)據(jù)列到創(chuàng)建的對(duì)象屬性。此外,你也可以創(chuàng)建定制的映射,匹配數(shù)據(jù)列和對(duì)象屬性。

創(chuàng)建并執(zhí)行Accessor

下面的代碼演示如何使用accessor執(zhí)行一個(gè)存儲(chǔ)過(guò)程,并操作返回的對(duì)象序列。你必須指定返回?cái)?shù)據(jù)的對(duì)象類型。在本示例中是一個(gè)簡(jiǎn)單的Product類,有三個(gè)屬性:ID、Name和Description。

// Create an object array and populate it with the required parameter values

object[] params = new object[] { "%bike%" };

// Create and execute a sproc accessor that uses the default

// parameter and output mappings.

var productData = defaultDB. ExecuteSprocAccessor <Product>("GetProductList",

params);

// Perform a client‐side query on the returned data. Be aware that

// the orderby and filtering is happening on the client, not in the database.

var results = from productItem in productData

where productItem.Description != null

orderby productItem.Name

select new { productItem.Name, productItem.Description };

// Display the results

foreach (var item in results)

{

Console.WriteLine("Product Name: {0}", item.Name);

Console.WriteLine("Description: {0}", item.Description);

Console.WriteLine();

}

使用LINQ訪問(wèn)Accessor返回的數(shù)據(jù)序列,刪除description為空的數(shù)據(jù),并按name排序,接著創(chuàng)建一個(gè)新的對(duì)象序列,包括Name和Description屬性。

檢索XML 數(shù)據(jù)

幾年前,XML是非常酷的新技術(shù),將掌管未來(lái)世界和改變我們思考數(shù)據(jù)的方法。在很多情況下,從關(guān)系型數(shù)據(jù)庫(kù)中檢索XML數(shù)據(jù)是非常有用的,DAAB支持這一功能。

DAAB提供了檢索XML數(shù)據(jù)的ExecuteXmlReader方法,接收包含F(xiàn)OR XML的SQL語(yǔ)句,向數(shù)據(jù)庫(kù)執(zhí)行SQL語(yǔ)句,返回結(jié)果為XmlReader。你可遍歷返回的XML元素或使用.NET 框架支持的XML類。然而,SQLXML僅限于Microsoft SQL Server數(shù)據(jù)庫(kù)。

static SqlDatabase sqlServerDB = null;

// Resolve a SqlDatabase object from the container using the default database.

sqlServerDB = EnterpriseLibraryContainer.Current.GetInstance<Database>() as SqlDatabase;

// Specify a SQL query that returns XML data

string xmlQuery = "SELECT * FROM OrderList WHERE State = @state FOR XML AUTO";

// Create a suitable command type and add the required parameter

// NB: ExecuteXmlReader is only available for SQL Server databases

using (DbCommand xmlCmd = sqlServerDB.GetSqlStringCommand(xmlQuery))

{

xmlCmd.Parameters.Add(new SqlParameter("state", "Colorado"));

using (XmlReader reader = sqlServerDB.ExecuteXmlReader(xmlCmd))

{

while (!reader.EOF) // Iterate through the elements in the XmlReader

{

if (reader.IsStartElement())

{

Console.WriteLine(reader.ReadOuterXml());

}

}

}

}

上述代碼演示了使用ExecuteXmlReader方法,從XmlReader檢索返回的XML數(shù)據(jù)。需要注意的是,默認(rèn)情況下,返回結(jié)果為XML數(shù)據(jù)塊,并不是一個(gè)有效的XML文檔。

使用上述代碼查詢SQL Server數(shù)據(jù)庫(kù),按FOR XML AUTO查詢,返回默認(rèn)格式的XML數(shù)據(jù),數(shù)據(jù)集中每一列表示為XML屬性,如下所示。

<OrderList Id="1" Status="DRAFT" CreatedOn="2009‐02‐01T11:12:06" Name="Adjustable

Race" LastName="Abbas" FirstName="Syed" ShipStreet="123 Elm Street" ShipCity="Denver"

ShipZipCode="12345" ShippingOption="Two‐day shipping" State="Colorado" />

<OrderList Id="2" Status="DRAFT" CreatedOn="2009‐02‐03T01:12:06" Name="All‐Purpose

Bike Stand" LastName="Abel" FirstName="Catherine" ShipStreet="321 Cedar Court"

ShipCity="Denver" ShipZipCode="12345" ShippingOption="One‐day shipping"

State="Colorado" />

檢索單一結(jié)果

訪問(wèn)數(shù)據(jù)的一個(gè)常見需求是檢索一個(gè)單一數(shù)據(jù)值。DAAB提供了ExecuteScalar方法實(shí)現(xiàn)這一需求。它執(zhí)行了指定的查詢,并以O(shè)bject類型返回第一行/第一列的數(shù)值。這意味著,它比ExecuteReader方法提供更好的性能,因?yàn)椴槐貏?chuàng)建DataReader對(duì)象和構(gòu)造結(jié)果集返回給客戶端。為了最大化效率,你應(yīng)該使用查詢返回一個(gè)單一值,或者單一行。

// Create a suitable command type for a SQL statement

// NB: For efficiency, aim to return only a single value or a single row.

using (DbCommand sqlCmd

= defaultDB.GetSqlStringCommand("SELECT [Name] FROM States"))

{

// Call the ExecuteScalar method of the command

Console.WriteLine("Result using a SQL statement: {0}",

defaultDB.ExecuteScalar(sqlCmd).ToString());

}

// Create a suitable command type for a stored procedure

// NB: For efficiency, aim to return only a single value or a single row.

using (DbCommand sprocCmd = defaultDB.GetStoredProcCommand("GetStatesList"))

{

// Call the ExecuteScalar method of the command

Console.WriteLine("Result using a stored procedure: {0}",

defaultDB.ExecuteScalar(sprocCmd).ToString());

}

更新數(shù)據(jù)

當(dāng)使用UPDATE、DELETE、或者INSERT 語(yǔ)句時(shí),可調(diào)用Database類的ExecuteNonQuery方法,執(zhí)行數(shù)據(jù)更新操作。

和前面介紹的ExecuteReader方法一樣,ExecuteNonQuery方法有一系列重載。你可以指定CommandType(默認(rèn)為StoredProcedure)和SQL語(yǔ)句、存儲(chǔ)過(guò)程的名稱等等。你可傳遞對(duì)象實(shí)例數(shù)組,表示查詢參數(shù)。此外,你也可傳遞Command對(duì)象,包含了你需要的任何參數(shù),同時(shí)也提供了Begin和End版本的方法,用來(lái)異步執(zhí)行更新操作。

string oldDescription = "Carries 4 bikes securely; steel construction, fits 2\" receiver hitch.";

string newDescription = "Bikes tend to fall off after a few miles.";

// Create command to execute the stored procedure and add the parameters.

DbCommand cmd = defaultDB.GetStoredProcCommand("UpdateProductsTable");

defaultDB.AddInParameter(cmd, "productID", DbType.Int32, 84);

defaultDB.AddInParameter(cmd, "description", DbType.String, newDescription);

// Execute the query and check if one row was updated.

if (defaultDB.ExecuteNonQuery(cmd) == 1)

{

// Update succeeded.

}

else

{

Console.WriteLine("ERROR: Could not update just one row.");

}

// Change the value of the second parameter

defaultDB.SetParameterValue(cmd, "description", oldDescription);

// Execute query and check if one row was updated

if (defaultDB.ExecuteNonQuery(cmd) == 1)

{

// Update succeeded.

}

else

{

Console.WriteLine("ERROR: Could not update just one row.");

}

操作DataSet 數(shù)據(jù)集

DAAB支持檢索DataSet數(shù)據(jù)集,同時(shí)也支持從DataSet更新原始的數(shù)據(jù)庫(kù)記錄。

你可以是ExecuteDataSet方法,獲取一個(gè)新的DataSet對(duì)象實(shí)例,該DataSet對(duì)象包含有查詢返回的數(shù)據(jù)表。DataSet中的數(shù)據(jù)表有默認(rèn)的表名稱,如Table、Table1和Table2等等。

如果你想將數(shù)據(jù)裝載進(jìn)一個(gè)現(xiàn)存的DataSet對(duì)象,你可以使用LoadDataSet方法,這樣允許你指定名稱或DataSet中的目標(biāo)數(shù)據(jù)表,還可讓你添加額外的數(shù)據(jù)表到一個(gè)現(xiàn)有的DataSet,或者更新DataSet中的特定表的內(nèi)容。

DataSet productDataSet;

// Using a SQL statement and a parameter array.

productDataSet = db.ExecuteDataSet(CommandType.Text, "GetProductsByCategory",

new Object[] { "%bike%" });

// Using a stored procedure and a parameter array.

productDataSet = db.ExecuteDataSet("GetProductsByCategory",

new Object[] { "%bike%" });

// Using a stored procedure and a named parameter.

DbCommand cmd = db.GetStoredProcCommand("GetProductsByCategory");

db.AddInParameter(cmd, "CategoryID", DbType.Int32, 7);

productDataSet = db.ExecuteDataSet(cmd);

Enterprise Library v5.0 -- Data Access Application Block 開發(fā)向?qū)В?)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 盈江县| 聊城市| 台湾省| 磐石市| 九龙城区| 上犹县| 晴隆县| 洛浦县| 天长市| 永顺县| 麻城市| 菏泽市| 沙田区| 资中县| 合江县| 奉新县| 福海县| 聂荣县| 马鞍山市| 鄂尔多斯市| 德江县| 邓州市| 庐江县| 贵南县| 厦门市| 镇康县| 辽中县| 玉林市| 吉木萨尔县| 务川| 科尔| 大兴区| 长泰县| 颍上县| 方正县| 曲沃县| 通辽市| 海南省| 天镇县| 达孜县| 兴化市|