Data Access Application Block提供了通用的数据访问的功能,随着2.0版本的推出有了很大变化。
一.改进
DAAB1.1里面我们知道Database方法返回或者创建一个DBCommandWrapper对象,而在DAAB2.0里面移除了DBCommandWrapper类,用ADO.NET2.0里面的DBCommand类代替实现类似的功能,这样使得DAAB跟我们的.NET类库的结合更加紧密,回忆一下我们在1.1里面用DBCommandWrapper来访问数据时的代码:
Enterprise Library2.0(1):Data Access Application Block学习_职场Database db = DatabaseFactory.CreateDatabase();
Enterprise Library2.0(1):Data Access Application Block学习_职场
Enterprise Library2.0(1):Data Access Application Block学习_职场DBCommandWrapper dbCommand 
= db.GetStoredProcCommandWrapper("GetProductsByCategory");
Enterprise Library2.0(1):Data Access Application Block学习_职场
Enterprise Library2.0(1):Data Access Application Block学习_职场dbCommand.AddInParameter(
"CategoryID", DbType.Int32, Category);
Enterprise Library2.0(1):Data Access Application Block学习_职场
Enterprise Library2.0(1):Data Access Application Block学习_职场DataSet productDataSet 
= db.ExecuteDataSet(dbCommand);
而用了新的DBCommand类之后则变成了:
Enterprise Library2.0(1):Data Access Application Block学习_职场Database db = DatabaseFactory.CreateDatabase();
Enterprise Library2.0(1):Data Access Application Block学习_职场
Enterprise Library2.0(1):Data Access Application Block学习_职场DbCommand dbCommand 
= db.GetStoredProcCommand("GetProductsByCategory"); 
Enterprise Library2.0(1):Data Access Application Block学习_职场
Enterprise Library2.0(1):Data Access Application Block学习_职场db.AddInParameter(dbCommand, 
"CategoryID", DbType.Int32, Category);
Enterprise Library2.0(1):Data Access Application Block学习_职场
Enterprise Library2.0(1):Data Access Application Block学习_职场DataSet productDataSet 
= db.ExecuteDataSet(dbCommand);
数据库连接字符串在我们基于数据库的开发永远是少不了的,但是在DAAB1.1下,它所使用的字符串跟我们在.NET类库中使用的连接字符串却是不能共享的,它们分别保存在不同的位置。而在2.0Data Access Application Block使用了ADO.NET2.0里面<connectionStrings>配置区,这样带来的一个好处是连接字符串可以在Application Block和自定义的.NET类之间共享使用该配置区,如:
Enterprise Library2.0(1):Data Access Application Block学习_职场<connectionStrings>
Enterprise Library2.0(1):Data Access Application Block学习_职场        
<add
Enterprise Library2.0(1):Data Access Application Block学习_职场            
name="DataAccessQuickStart" 
Enterprise Library2.0(1):Data Access Application Block学习_职场            providerName
="System.Data.SqlClient"
Enterprise Library2.0(1):Data Access Application Block学习_职场            connectionString
="server=(local)\SQLEXPRESS;database=EntLibQuickStarts;Integrated Security=true" />
Enterprise Library2.0(1):Data Access Application Block学习_职场
</connectionStrings>
.NET2.0下,泛型编程已经成为了一个核心,而2.0版的DAAB中也新增了一个GenericDatabase对象。DAAB中虽然已经包含了SqlDatabaseOrcaleDatabase,但是如果我们需要使用其他的像DB2等数据库时,就需要用到GenericDatabase,它可以用于任何.NET类库中的数据提供者,包括OdbcProviderOleDbProvider
二.使用示例
DAAB2.0的配置非常简单,主要有以下几方面的配置:
配置连接字符串
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_21配置默认数据库
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_22添加相关的命名空间:
Enterprise Library2.0(1):Data Access Application Block学习_职场using Microsoft.Practices.EnterpriseLibrary.Data;
Enterprise Library2.0(1):Data Access Application Block学习_职场
using System.Data;
使用Data Access Application Block进行数据的读取和操作,一般分为三步:
1.创建Database对象
2.提供命令参数,如果需要的话
3.执行命令
下面分别看一下DataAccessQuickStart中提供的一些例子:
执行静态的SQL语句
Enterprise Library2.0(1):Data Access Application Block学习_职场public string GetCustomerList()
Enterprise Library2.0(1):Data Access Application Block学习_休闲_26
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
// 创建Database对象
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Database db = DatabaseFactory.CreateDatabase();
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
// 使用SQL语句创建DbCommand对象
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
string sqlCommand = "Select CustomerID, Name, Address, City, Country, PostalCode " +
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
"From Customers";
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29DbCommand dbCommand 
= db.GetSqlStringCommand(sqlCommand);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29StringBuilder readerData 
= new StringBuilder();
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
// 调用ExecuteReader方法
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
Enterprise Library2.0(1):Data Access Application Block学习_职场_40
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
while (dataReader.Read())
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_44    
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        
// Get the value of the 'Name' column in the DataReader
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
        readerData.Append(dataReader["Name"]);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        readerData.Append(Environment.NewLine);
Enterprise Library2.0(1):Data Access Application Block学习_休闲_50    }

Enterprise Library2.0(1):Data Access Application Block学习_休闲_50}

Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
return readerData.ToString();
Enterprise Library2.0(1):Data Access Application Block学习_休闲_54}
执行存储过程并传递参数,返回DataSet
Enterprise Library2.0(1):Data Access Application Block学习_职场public DataSet GetProductsInCategory(int Category)
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_56
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Create the Database object, using the default database service. The
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// default database service is determined through configuration.
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    Database db = DatabaseFactory.CreateDatabase();
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
string sqlCommand = "GetProductsByCategory";
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    DbCommand dbCommand 
= db.GetStoredProcCommand(sqlCommand);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Retrieve products from the specified category.
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, Category);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// DataSet that will hold the returned results        
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    DataSet productsDataSet = null;
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    productsDataSet 
= db.ExecuteDataSet(dbCommand);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Note: connection was closed by ExecuteDataSet method call 
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29

Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
return productsDataSet;
Enterprise Library2.0(1):Data Access Application Block学习_休闲_54}
利用DataSet更新数据
Enterprise Library2.0(1):Data Access Application Block学习_职场public int UpdateProducts()
Enterprise Library2.0(1):Data Access Application Block学习_职场_79
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Create the Database object, using the default database service. The
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// default database service is determined through configuration.
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    Database db = DatabaseFactory.CreateDatabase();
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    DataSet productsDataSet 
= new DataSet();
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
string sqlCommand = "Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate " +
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        
"From Products";
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    DbCommand dbCommand 
= db.GetSqlStringCommand(sqlCommand);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
string productsTable = "Products";
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Retrieve the initial data
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    db.LoadDataSet(dbCommand, productsDataSet, productsTable);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Get the table that will be modified
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    DataTable table = productsDataSet.Tables[productsTable];
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Add a new product to existing DataSet
Enterprise Library2.0(1):Data Access Application Block学习_EnterpriseLibrary2.0_101
    DataRow addedRow = table.Rows.Add(new object[] {DBNull.Value, "New product"1125});
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Modify an existing product
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    table.Rows[0]["ProductName"= "Modified product";
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Establish our Insert, Delete, and Update commands
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    DbCommand insertCommand = db.GetStoredProcCommand("AddProduct");
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    db.AddInParameter(insertCommand, 
"ProductName", DbType.String, "ProductName", DataRowVersion.Current);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    db.AddInParameter(insertCommand, 
"CategoryID", DbType.Int32, "CategoryID", DataRowVersion.Current);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    db.AddInParameter(insertCommand, 
"UnitPrice", DbType.Currency, "UnitPrice", DataRowVersion.Current);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    DbCommand deleteCommand 
= db.GetStoredProcCommand("DeleteProduct");
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    db.AddInParameter(deleteCommand, 
"ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    DbCommand updateCommand 
= db.GetStoredProcCommand("UpdateProduct");
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    db.AddInParameter(updateCommand, 
"ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    db.AddInParameter(updateCommand, 
"ProductName", DbType.String, "ProductName", DataRowVersion.Current);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    db.AddInParameter(updateCommand, 
"LastUpdate", DbType.DateTime, "LastUpdate", DataRowVersion.Current);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Submit the DataSet, capturing the number of rows that were affected
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    int rowsAffected = db.UpdateDataSet(productsDataSet, "Products", insertCommand, updateCommand,
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29                                        deleteCommand, UpdateBehavior.Standard);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
return rowsAffected;
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_休闲_54}
通过ID获取记录详细信息
Enterprise Library2.0(1):Data Access Application Block学习_职场public string GetProductDetails(int productID)
Enterprise Library2.0(1):Data Access Application Block学习_休闲_130
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Create the Database object, using the default database service. The
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// default database service is determined through configuration.
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    Database db = DatabaseFactory.CreateDatabase();
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
string sqlCommand = "GetProductDetails";
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    DbCommand dbCommand 
= db.GetStoredProcCommand(sqlCommand);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Add paramters
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Input parameters can specify the input value
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    db.AddInParameter(dbCommand, "ProductID", DbType.Int32, productID);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Output parameters specify the size of the return data
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    db.AddOutParameter(dbCommand, "ProductName", DbType.String, 50);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    db.AddOutParameter(dbCommand, 
"UnitPrice", DbType.Currency, 8);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    db.ExecuteNonQuery(dbCommand);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Row of data is captured via output parameters
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    string results = string.Format(CultureInfo.CurrentCulture, "{0}, {1}, {2:C} ",
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29                                   db.GetParameterValue(dbCommand, 
"ProductID"),
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29                                   db.GetParameterValue(dbCommand, 
"ProductName"),
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29                                   db.GetParameterValue(dbCommand, 
"UnitPrice"));
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
return results;
Enterprise Library2.0(1):Data Access Application Block学习_休闲_54}
XML格式返回数据
Enterprise Library2.0(1):Data Access Application Block学习_职场public string GetProductList()
Enterprise Library2.0(1):Data Access Application Block学习_休闲_158
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Use a named database instance that refers to a SQL Server database.
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    SqlDatabase dbSQL = DatabaseFactory.CreateDatabase() as SqlDatabase;
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Use "FOR XML AUTO" to have SQL return XML data
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    string sqlCommand = "Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate " +
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        
"From Products FOR XML AUTO";
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    DbCommand dbCommand 
= dbSQL.GetSqlStringCommand(sqlCommand);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    XmlReader productsReader 
= null;
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    StringBuilder productList 
= new StringBuilder();
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
try
Enterprise Library2.0(1):Data Access Application Block学习_EnterpriseLibrary2.0_173    
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        productsReader 
= dbSQL.ExecuteXmlReader(dbCommand);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        
// Iterate through the XmlReader and put the data into our results.
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
        while (!productsReader.EOF)
Enterprise Library2.0(1):Data Access Application Block学习_职场_180        
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29            
if (productsReader.IsStartElement())
Enterprise Library2.0(1):Data Access Application Block学习_休闲_184            
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29                productList.Append(productsReader.ReadOuterXml());
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29                productList.Append(Environment.NewLine);
Enterprise Library2.0(1):Data Access Application Block学习_休闲_50            }

Enterprise Library2.0(1):Data Access Application Block学习_休闲_50        }

Enterprise Library2.0(1):Data Access Application Block学习_休闲_50    }

Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
finally
Enterprise Library2.0(1):Data Access Application Block学习_职场_193    
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29      
// Close the Reader.
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
      if (productsReader != null)
Enterprise Library2.0(1):Data Access Application Block学习_职场_198      
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29          productsReader.Close();
Enterprise Library2.0(1):Data Access Application Block学习_休闲_50      }

Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29      
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29      
// Explicitly close the connection. The connection is not closed
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29      
// when the XmlReader is closed.
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
      if (dbCommand.Connection != null)
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_207      
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        dbCommand.Connection.Close();
Enterprise Library2.0(1):Data Access Application Block学习_休闲_50      }
  
Enterprise Library2.0(1):Data Access Application Block学习_休闲_50    }

Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
return productList.ToString();
Enterprise Library2.0(1):Data Access Application Block学习_休闲_54}
使用事务
Enterprise Library2.0(1):Data Access Application Block学习_职场public bool Transfer(int transactionAmount, int sourceAccount, int destinationAccount)
Enterprise Library2.0(1):Data Access Application Block学习_EnterpriseLibrary2.0_217
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
bool result = false;
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Create the Database object, using the default database service. The
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// default database service is determined through configuration.
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    Database db = DatabaseFactory.CreateDatabase();
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// Two operations, one to credit an account, and one to debit another
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
// account.
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
    string sqlCommand = "CreditAccount";
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    DbCommand creditCommand 
= db.GetStoredProcCommand(sqlCommand);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    db.AddInParameter(creditCommand, 
"AccountID", DbType.Int32, sourceAccount);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    db.AddInParameter(creditCommand, 
"Amount", DbType.Int32, transactionAmount);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    sqlCommand 
= "DebitAccount";
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    DbCommand debitCommand 
= db.GetStoredProcCommand(sqlCommand);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    db.AddInParameter(debitCommand, 
"AccountID", DbType.Int32, destinationAccount);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    db.AddInParameter(debitCommand, 
"Amount", DbType.Int32, transactionAmount);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29    
using (DbConnection connection = db.CreateConnection())
Enterprise Library2.0(1):Data Access Application Block学习_EnterpriseLibrary2.0_241    
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        connection.Open();
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        DbTransaction transaction 
= connection.BeginTransaction();
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        
try
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_248        
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29            
// Credit the first account
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
            db.ExecuteNonQuery(creditCommand, transaction);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29            
// Debit the second account
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
            db.ExecuteNonQuery(debitCommand, transaction);
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29            
// Commit the transaction
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
            transaction.Commit();
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29            
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29            result 
= true;
Enterprise Library2.0(1):Data Access Application Block学习_休闲_50        }

Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        
catch
Enterprise Library2.0(1):Data Access Application Block学习_职场_262        
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29            
// Rollback transaction 
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
            transaction.Rollback();
Enterprise Library2.0(1):Data Access Application Block学习_休闲_50        }

Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        connection.Close();
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29        
return result;
Enterprise Library2.0(1):Data Access Application Block学习_休闲_50    }

Enterprise Library2.0(1):Data Access Application Block学习_休闲_54}
三.常见功能
1.创建Database对象
创建一个默认的Database对象
Enterprise Library2.0(1):Data Access Application Block学习_职场Database dbSvc = DatabaseFactory.CreateDatabase();
默认的数据库在配置文件中:
Enterprise Library2.0(1):Data Access Application Block学习_职场<dataConfiguration defaultDatabase="DataAccessQuickStart" />
创建一个实例Database对象
Enterprise Library2.0(1):Data Access Application Block学习_职场// Use a named database instance that refers to an arbitrary database type, 
Enterprise Library2.0(1):Data Access Application Block学习_职场
// which is determined by configuration information.
Enterprise Library2.0(1):Data Access Application Block学习_职场
Database myDb = DatabaseFactory.CreateDatabase("DataAccessQuickStart");
创建一个具体的类型的数据库对象
Enterprise Library2.0(1):Data Access Application Block学习_职场// Create a SQL database.
Enterprise Library2.0(1):Data Access Application Block学习_职场
SqlDatabase dbSQL = DatabaseFactory.CreateDatabase("DataAccessQuickStart"as SqlDatabase;
2.创建DbCommand对象
静态的SQL语句创建一个DbCommand
Enterprise Library2.0(1):Data Access Application Block学习_职场Database db = DatabaseFactory.CreateDatabase();
Enterprise Library2.0(1):Data Access Application Block学习_职场
string sqlCommand = "Select CustomerID, LastName, FirstName From Customers";
Enterprise Library2.0(1):Data Access Application Block学习_职场DbCommand dbCommand 
= db.GetSqlStringCommand(sqlCommand);
存储过程创建一个DbCommand
Enterprise Library2.0(1):Data Access Application Block学习_职场Database db = DatabaseFactory.CreateDatabase();
Enterprise Library2.0(1):Data Access Application Block学习_职场DbCommand dbCommand 
= db.GetStoredProcCommand("GetProductsByCategory");
3.管理对象
当连接对象打开后,不需要再次连接
Enterprise Library2.0(1):Data Access Application Block学习_职场Database db = DatabaseFactory.CreateDatabase();
Enterprise Library2.0(1):Data Access Application Block学习_职场
string sqlCommand = "Select ProductID, ProductName From Products";
Enterprise Library2.0(1):Data Access Application Block学习_职场DbCommand dbCommand 
= db.GetSqlStringCommand(sqlCommand); 
Enterprise Library2.0(1):Data Access Application Block学习_职场
// No need to open the connection; just make the call.
Enterprise Library2.0(1):Data Access Application Block学习_职场
DataSet customerDataSet = db.ExecuteDataSet(dbCommand);
使用Using及早释放对象
Enterprise Library2.0(1):Data Access Application Block学习_职场Database db = DatabaseFactory.CreateDatabase();
Enterprise Library2.0(1):Data Access Application Block学习_职场DbCommand dbCommand 
= db.GetSqlStringCommand("Select Name, Address From Customers");
Enterprise Library2.0(1):Data Access Application Block学习_职场
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
Enterprise Library2.0(1):Data Access Application Block学习_职场_293
{
Enterprise Library2.0(1):Data Access Application Block学习_DataAccessApplicatio_29
// Process results
Enterprise Library2.0(1):Data Access Application Block学习_休闲_54
}
4.参数处理
Database类提供了如下的方法,用于参数的处理:
AddParameter. 传递参数给存储过程
AddInParameter.
传递输入参数给存储过程
AddOutParameter.
传递输出参数给存储过程
GetParameterValue.
得到指定参数的值
SetParameterValue.
设定参数值
使用示例如下:
Enterprise Library2.0(1):Data Access Application Block学习_职场Database db = DatabaseFactory.CreateDatabase();
Enterprise Library2.0(1):Data Access Application Block学习_职场
string sqlCommand = "GetProductDetails";
Enterprise Library2.0(1):Data Access Application Block学习_职场DbCommand dbCommand 
= db.GetStoredProcCommand(sqlCommand);
Enterprise Library2.0(1):Data Access Application Block学习_职场db.AddInParameter(dbCommand, 
"ProductID", DbType.Int32, 5);
Enterprise Library2.0(1):Data Access Application Block学习_职场db.AddOutParameter(dbCommand, 
"ProductName", DbType.String, 50);
Enterprise Library2.0(1):Data Access Application Block学习_职场db.AddOutParameter(dbCommand, 
"UnitPrice", DbType.Currency, 8);
Enterprise Library2.0(1):Data Access Application Block学习_职场Database db = DatabaseFactory.CreateDatabase();
Enterprise Library2.0(1):Data Access Application Block学习_职场DbCommand insertCommand 
= db.GetStoredProcCommand("AddProduct");
Enterprise Library2.0(1):Data Access Application Block学习_职场db.AddInParameter(insertCommand, 
"ProductName", DbType.String, "ProductName", DataRowVersion.Current);
Enterprise Library2.0(1):Data Access Application Block学习_职场db.AddInParameter(insertCommand, 
"CategoryID", DbType.Int32, "CategoryID", DataRowVersion.Current);
Enterprise Library2.0(1):Data Access Application Block学习_职场db.AddInParameter(insertCommand, 
"UnitPrice", DbType.Currency, "UnitPrice", DataRowVersion.Current);
四.使用场景
DAAB2.0是对ADO.NET2.0的补充,它允许你使用相同的数据访问代码来支持不同的数据库,您通过改变配置文件就在不同的数据库之间切换。目前虽然只提供SQLServerOracle的支持,但是可以通过GenericDatabaseADO.NET 2.0下的DbProviderFactory对象来增加对其他数据库的支持。如果想要编写出来的数据库访问程序具有更好的移植性,则DAAB2.0是一个不错的选择,但是如果您想要针对特定数据库的特性进行编程,就要用ADO.NET了。
参考:Enterprise Libaray –January 2006帮助文档及QuickStart