Data Access Application Block提供了通用的數據訪問的功能,隨著2.0版本的推出有了很大變化。
一.改進
在DAAB1.1裡面我們知道Database方法返回或者創建一個DBCommandWrapper對象,而在DAAB2.0裡面移除了DBCommandWrapper類,用ADO.NET2.0裡面的DBCommand類代替實現類似的功能,這樣使得DAAB跟我們的.Net類庫的結合更加緊密,回憶一下我們在1.1裡面用DBCommandWrapper來訪問數據時的代碼:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
DBCommandWrapper dbCommand = db.GetStoredProcCommandWrapper("GetProductsByCategory");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
dbCommand.AddInParameter("CategoryID", DbType.Int32, Category);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
DataSet productDataSet = db.ExecuteDataSet(dbCommand);
而用了新的DBCommand類之後則變成了:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
DbCommand dbCommand = db.GetStoredProcCommand("GetProductsByCategory");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, Category);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
DataSet productDataSet = db.ExecuteDataSet(dbCommand);
數據庫連接字符串在我們基於數據庫的開發永遠是少不了的,但是在DAAB1.1下,它所使用的字符串跟我們在.NET類庫中使用的連接字符串卻是不能共享的,它們分別保存在不同的位置。而在2.0的Data Access Application Block使用了ADO.NET2.0裡面<connectionStrings>配置區,這樣帶來的一個好處是連接字符串可以在Application Block和自定義的.Net類之間共享使用該配置區,如:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
<connectionStrings>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
<add
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
name="DataAccessQuickStart"
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
providerName="System.Data.SqlClIEnt"
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
connectionString="server=(local)SQLEXPRESS;database=EntLibQuickStarts;Integrated Security=true" />
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
</connectionStrings>
在.NET2.0下,泛型編程已經成為了一個核心,而2.0版的DAAB中也新增了一個GenericDatabase對象。DAAB中雖然已經包含了SqlDatabase和OrcaleDatabase,但是如果我們需要使用其他的像DB2等數據庫時,就需要用到GenericDatabase,它可以用於任何.Net類庫中的數據提供者,包括OdbcProvider和OleDbProvider。
二.使用示例
DAAB2.0的配置非常簡單,主要有以下幾方面的配置:
配置連接字符串
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301805.jpg)
配置默認數據庫
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301978.jpg)
添加相關的命名空間:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
using Microsoft.Practices.EnterpriseLibrary.Data;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
using System.Data;
使用Data Access Application Block進行數據的讀取和操作,一般分為三步:
1.創建Database對象
2.提供命令參數,如果需要的話
3.執行命令
下面分別看一下DataAccessQuickStart中提供的一些例子:
執行靜態的SQL語句
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
public string GetCustomerList()
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301927.gif)
// 創建Database對象
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// 使用SQL語句創建DbCommand對象
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
string sqlCommand = "Select CustomerID, Name, Address, City, Country, PostalCode " +
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
"From Customers";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
StringBuilder readerData = new StringBuilder();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// 調用ExecuteReader方法
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301913.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
while (dataReader.Read())
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301913.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Get the value of the ''Name'' column in the DataReader
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
readerData.Append(dataReader["Name"]);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
readerData.Append(Environment.NewLine);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
return readerData.ToString();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302066.gif)
}
執行存儲過程並傳遞參數,返回DataSet
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
public DataSet GetProductsInCategory(int Category)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301927.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302020.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Create the Database object, using the default database service. The
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// default database service is determined through configuration.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
string sqlCommand = "GetProductsByCategory";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Retrieve products from the specifIEd category.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, Category);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// DataSet that will hold the returned results
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DataSet productsDataSet = null;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
productsDataSet = db.ExecuteDataSet(dbCommand);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Note: connection was closed by ExecuteDataSet method call
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
return productsDataSet;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302066.gif)
}
利用DataSet更新數據
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
public int UpdateProducts()
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301927.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302020.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Create the Database object, using the default database service. The
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// default database service is determined through configuration.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DataSet productsDataSet = new DataSet();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
string sqlCommand = "Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate " +
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
"From Products";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
string productsTable = "Products";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// RetrIEve the initial data
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.LoadDataSet(dbCommand, productsDataSet, productsTable);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Get the table that will be modifIEd
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DataTable table = productsDataSet.Tables[productsTable];
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Add a new product to existing DataSet
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301913.gif)
DataRow addedRow = table.Rows.Add(new object[] ...{DBNull.Value, "New product", 11, 25});
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Modify an existing product
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
table.Rows[0]["ProductName"] = "ModifIEd product";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Establish our Insert, Delete, and Update commands
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DbCommand insertCommand = db.GetStoredProcCommand("AddProduct");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddInParameter(insertCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddInParameter(insertCommand, "CategoryID", DbType.Int32, "CategoryID", DataRowVersion.Current);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddInParameter(insertCommand, "UnitPrice", DbType.Currency, "UnitPrice", DataRowVersion.Current);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DbCommand deleteCommand = db.GetStoredProcCommand("DeleteProduct");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddInParameter(deleteCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DbCommand updateCommand = db.GetStoredProcCommand("UpdateProduct");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddInParameter(updateCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddInParameter(updateCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddInParameter(updateCommand, "LastUpdate", DbType.DateTime, "LastUpdate", DataRowVersion.Current);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Submit the DataSet, capturing the number of rows that were affected
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
int rowsAffected = db.UpdateDataSet(productsDataSet, "Products", insertCommand, updateCommand,
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
deleteCommand, UpdateBehavior.Standard);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
return rowsAffected;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302066.gif)
}
通過ID獲取記錄詳細信息
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
public string GetProductDetails(int productID)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301927.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302020.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Create the Database object, using the default database service. The
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// default database service is determined through configuration.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
string sqlCommand = "GetProductDetails";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Add paramters
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Input parameters can specify the input value
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddInParameter(dbCommand, "ProductID", DbType.Int32, productID);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Output parameters specify the size of the return data
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddOutParameter(dbCommand, "ProductName", DbType.String, 50);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddOutParameter(dbCommand, "UnitPrice", DbType.Currency, 8);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.ExecuteNonQuery(dbCommand);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Row of data is captured via output parameters
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
string results = string.Format(CultureInfo.CurrentCulture, "{0}, {1}, {2:C} ",
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.GetParameterValue(dbCommand, "ProductID"),
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.GetParameterValue(dbCommand, "ProductName"),
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.GetParameterValue(dbCommand, "UnitPrice"));
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
return results;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302066.gif)
}
以XML格式返回數據
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
public string GetProductList()
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301927.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302020.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Use a named database instance that refers to a SQL Server database.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
SqlDatabase dbSQL = DatabaseFactory.CreateDatabase() as SqlDatabase;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Use "FOR XML AUTO" to have SQL return XML data
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
string sqlCommand = "Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate " +
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
"From Products FOR XML AUTO";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DbCommand dbCommand = dbSQL.GetSqlStringCommand(sqlCommand);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
XMLReader productsReader = null;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
StringBuilder productList = new StringBuilder();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
try
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301913.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
productsReader = dbSQL.ExecuteXMLReader(dbCommand);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Iterate through the XMLReader and put the data into our results.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
while (!productsReader.EOF)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301913.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
if (productsReader.IsStartElement())
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301913.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
productList.Append(productsReader.ReadOuterXML());
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
productList.Append(Environment.NewLine);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
finally
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301913.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Close the Reader.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
if (productsReader != null)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301913.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
productsReader.Close();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Explicitly close the connection. The connection is not closed
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// when the XMLReader is closed.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
if (dbCommand.Connection != null)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301913.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
dbCommand.Connection.Close();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
return productList.ToString();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302066.gif)
}
使用事務
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
public bool Transfer(int transactionAmount, int sourceAccount, int destinationAccount)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301927.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302020.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
bool result = false;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Create the Database object, using the default database service. The
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// default database service is determined through configuration.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Two Operations, one to credit an account, and one to debit another
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// account.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
string sqlCommand = "CreditAccount";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DbCommand creditCommand = db.GetStoredProcCommand(sqlCommand);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
d productsReader.Close();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Explicitly close the connection. The connection is not closed
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// when the XMLReader is closed.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
if (dbCommand.Connection != null)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301913.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
dbCommand.Connection.Close();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
return productList.ToString();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302066.gif)
}
使用事務
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
public bool Transfer(int transactionAmount, int sourceAccount, int destinationAccount)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301927.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302020.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
bool result = false;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Create the Database object, using the default database service. The
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// default database service is determined through configuration.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Two Operations, one to credit an account, and one to debit another
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// account.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
string sqlCommand = "CreditAccount";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DbCommand creditCommand = db.GetStoredProcCommand(sqlCommand);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
dAddInParameter(creditCommand, "AccountID", DbType.Int32, sourceAccount);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddInParameter(creditCommand, "Amount", DbType.Int32, transactionAmount);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
sqlCommand = "DebitAccount";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DbCommand debitCommand = db.GetStoredProcCommand(sqlCommand);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddInParameter(debitCommand, "AccountID", DbType.Int32, destinationAccount);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.AddInParameter(debitCommand, "Amount", DbType.Int32, transactionAmount);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
using (DbConnection connection = db.CreateConnection())
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301913.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
connection.Open();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
DbTransaction transaction = connection.BeginTransaction();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
try
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301913.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Credit the first account
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.ExecuteNonQuery(creditCommand, transaction);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Debit the second account
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
db.ExecuteNonQuery(debitCommand, transaction);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Commit the transaction
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
transaction.Commit();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
result = true;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
catch
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301913.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Rollback transaction
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
transaction.Rollback();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
connection.Close();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
return result;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301955.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302066.gif)
}
三.常見功能
1.創建Database對象
創建一個默認的Database對象
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
Database dbSvc = DatabaseFactory.CreateDatabase();
默認的數據庫在配置文件中:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
<dataConfiguration defaultDatabase="DataAccessQuickStart" />
創建一個實例Database對象
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
// Use a named database instance that refers to an arbitrary database type,
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
// which is determined by configuration information.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
Database myDb = DatabaseFactory.CreateDatabase("DataAccessQuickStart");
創建一個具體的類型的數據庫對象
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
// Create a SQL database.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
SqlDatabase dbSQL = DatabaseFactory.CreateDatabase("DataAccessQuickStart") as SqlDatabase;
2.創建DbCommand對象
靜態的SQL語句創建一個DbCommand
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
string sqlCommand = "Select CustomerID, LastName, FirstName From Customers";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
存儲過程創建一個DbCommand
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
DbCommand dbCommand = db.GetStoredProcCommand("GetProductsByCategory");
3.管理對象
當連接對象打開後,不需要再次連接
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
string sqlCommand = "Select ProductID, ProductName From Products";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
// No need to open the connection; just make the call.
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
DataSet customerDataSet = ExecuteDataSet(dbCommand);
使用Using及早釋放對象
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
DbCommand dbCommand = db.GetSqlStringCommand("Select Name, Address From Customers");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301927.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302020.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301971.gif)
// Process results
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311302066.gif)
}
4.參數處理
Database類提供了如下的方法,用於參數的處理:
AddParameter. 傳遞參數給存儲過程
AddInParameter. 傳遞輸入參數給存儲過程
AddOutParameter. 傳遞輸出參數給存儲過程
GetParameterValue. 得到指定參數的值
SetParameterValue. 設定參數值
使用示例如下:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
string sqlCommand = "GetProductDetails";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
db.AddInParameter(dbCommand, "ProductID", DbType.Int32, 5);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
db.AddOutParameter(dbCommand, "ProductName", DbType.String, 50);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
db.AddOutParameter(dbCommand, "UnitPrice", DbType.Currency, 8);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
Database db = DatabaseFactory.CreateDatabase();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
DbCommand insertCommand = db.GetStoredProcCommand("AddProduct");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
db.AddInParameter(insertCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
db.AddInParameter(insertCommand, "CategoryID", DbType.Int32, "CategoryID", DataRowVersion.Current);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311301827.gif)
db.AddInParameter(insertCommand, "UnitPrice", DbType.Currency, "UnitPrice", DataRowVersion.Current);
四.使用場景
DAAB2.0是對ADO.NET2.0的補充,它允許你使用相同的數據訪問代碼來支持不同的數據庫,您通過改變配置文件就在不同的數據庫之間切換。目前雖然只提供SQLServer和Oracle的支持,但是可以通過GenericDatabase和ADO.NET 2.0下的DbProviderFactory對象來增加對其他數據庫的支持。如果想要編寫出來的數據庫訪問程序具有更好的移植性,則DAAB2.0是一個不錯的選擇,但是如果您想要針對特定數據庫的特性進行編程,就要用ADO.Net了。
參考:Enterprise Libaray –January 2006幫助文檔及QuickStart
原文地址:http://www.cnblogs.com/Terrylee/archive/2006/03/14/350026.Html