欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Enterprise Library v5.0 -- Data Access Appli

系統(tǒng) 1749 0

?


微軟企業(yè)庫 Enterprise Library 5.0 正式發(fā)布?。?!
Enterprise Library 5.0 開發(fā)向導- 簡介(1)
Enterprise Library v5.0 -- Data Access Application Block 開發(fā)向導(2)

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

現(xiàn)代程序開發(fā)都關注“數(shù)據(jù)對象”,使用數(shù)據(jù)傳輸對象(DTO)在應用程序層間傳遞數(shù)據(jù),使用ORM(Object/Relations Mapping)實現(xiàn)數(shù)據(jù)訪問層,或者充分利用客戶端數(shù)據(jù)訪問技術,如LINQ等等。

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

關于Accessors

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

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

如果你不指定參數(shù)mapper,accessor將自動使用默認的mapper來解析參數(shù),但是這一特性僅僅適用于SQL Server或Oracle數(shù)據(jù)庫的存儲過程。當你使用SQL語句或者其他數(shù)據(jù)庫時,你必須知道定制的參數(shù)mapper,負責解析參數(shù)。

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

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

下面的代碼演示如何使用accessor執(zhí)行一個存儲過程,并操作返回的對象序列。你必須指定返回數(shù)據(jù)的對象類型。在本示例中是一個簡單的Product類,有三個屬性: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訪問Accessor返回的數(shù)據(jù)序列,刪除description為空的數(shù)據(jù),并按name排序,接著創(chuàng)建一個新的對象序列,包括Name和Description屬性。

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

幾年前,XML是非??岬男录夹g,將掌管未來世界和改變我們思考數(shù)據(jù)的方法。在很多情況下,從關系型數(shù)據(jù)庫中檢索XML數(shù)據(jù)是非常有用的,DAAB支持這一功能。

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

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ù)。需要注意的是,默認情況下,返回結果為XML數(shù)據(jù)塊,并不是一個有效的XML文檔。

使用上述代碼查詢SQL Server數(shù)據(jù)庫,按FOR XML AUTO查詢,返回默認格式的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" />

檢索單一結果

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

// 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ù)

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

和前面介紹的ExecuteReader方法一樣,ExecuteNonQuery方法有一系列重載。你可以指定CommandType(默認為StoredProcedure)和SQL語句、存儲過程的名稱等等。你可傳遞對象實例數(shù)組,表示查詢參數(shù)。此外,你也可傳遞Command對象,包含了你需要的任何參數(shù),同時也提供了Begin和End版本的方法,用來異步執(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ù)集,同時也支持從DataSet更新原始的數(shù)據(jù)庫記錄。

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

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

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ā)向導(3)


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久视频这里只要精品 | 国产1级片 | 97美女网 | 国产精品成人观看视频国产 | 日本视频在线免费 | 加勒比AV一本大道香蕉大在线 | 波多野结衣在线免费播放 | 亚洲一区二区三区在线免费观看 | 免黄网站| 精品国产午夜福利在线观看 | 91网站国产 | 少妇特黄a一区二区三区88av | 久久精品中文 | 电影长安道无删减免费看 | 亚洲综合五月天欧美 | 一起射综合网 | 欧美成人性性 | 亚洲一区二区三区在线免费观看 | 久久国产免费福利永久 | 亚洲色婷婷久久精品AV蜜桃久久 | 亚洲性色成人 | 亚洲精品久久久久久蜜臀 | 亚洲欧洲日本在线 | 日韩一区二区中文字幕 | 日韩av片免费播放 | 精品久久中文字幕 | 东京一热本色道久久爱 | 国产一区二区黑人欧美xxxx | 久久97久久97精品免视看 | 亚洲一区在线免费观看 | 一级性生活免费视频 | 九九热精| 日本黄色小视频 | 欧日韩不卡在线视频 | 亚欧精品一区二区三区四区 | 欧美一区二区精品 | 午夜视频日本 | 免费看的黄网站 | 欧美成人一级视频 | 欧美aⅴ片 | 成人国产精品免费视频不卡 |