本文是前一篇posting《Enterprise Library: Data Access Application Block使用向导,Part 1》的Part 2,主要演示使用Data Access Application Block (Enterprise Library)的代码部分。
Data Access Application Block使用向导:
1.增加对Microsoft.Practices.EnterpriseLibrary.Data.dll和Microsoft.Practices.EnterpriseLibrary.Configuration.dll的引用,并在代码在添加:
using Microsoft.Practices.EnterpriseLibrary.Data;
2.调用代码示例:
(1) ExecuteDataSet方法
Database db = DatabaseFactory.CreateDatabase();
DataSet dsCustomers = db.ExecuteDataSet(CommandType.Text, "Select * From Customers" );
customerGrid.DataSource = dsCustomers.Tables[0];
(2) ExecuteReader方法
Database db = DatabaseFactory.CreateDatabase();
string sqlCommand = "Select top 5 * From Customers";
DBCommandWrapper dbCommandWrapper = db.GetSqlStringCommandWrapper(sqlCommand);
StringBuilder readerData = new StringBuilder();
using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
{
while (dataReader.Read())
{
readerData.Append(dataReader["ContactName"]);
readerData.Append(Environment.NewLine);
}
}
txtResult.Text = readerData.ToString();
为了执行SQL语句,上述代码使用GetSqlStringCommandWrapper方法创建合适command wrapper对象,然后作为参数传递给ExecuteReader方法。
(3) ExecuteNonQuery方法
Database db = DatabaseFactory.CreateDatabase();
string sqlCommand = "getProductDetails";
DBCommandWrapper dbCommandWrapper = db.GetStoredProcCommandWrapper(sqlCommand);
// Add paramters
int productID=1;
// Input parameters can specify the input value
dbCommandWrapper.AddInParameter("@ProductID", DbType.Int32, productID);
// Output parameters specify the size of the return data
dbCommandWrapper.AddOutParameter("@ProductName", DbType.String, 40);
dbCommandWrapper.AddOutParameter("@UnitPrice", DbType.Currency, 8);
dbCommandWrapper.AddOutParameter("@QtyPerUnit", DbType.String, 20);
db.ExecuteNonQuery(dbCommandWrapper);
// Row of data is captured via output parameters
string results = string.Format(CultureInfo.CurrentCulture, "{0}, {1}, {2:C}, {3} ",
dbCommandWrapper.GetParameterValue("@ProductID"),
dbCommandWrapper.GetParameterValue("@ProductName"),
dbCommandWrapper.GetParameterValue("@UnitPrice"),
dbCommandWrapper.GetParameterValue("@QtyPerUnit"));
txtResult.Text = results;
为了执行存储过程,上述代码使用GetStoredProcCommandWrapper方法创建合适的command wrapper对象,然后作为参数传递给ExecuteNonQuery方法。
(4) ExecuteScalar方法
Database db = DatabaseFactory.CreateDatabase();
string sqlCommand = "GetProductName";
int productID=1;
DBCommandWrapper dbCommandWrapper = db.GetStoredProcCommandWrapper(sqlCommand, productID);
// Retrieve ProdcutName. ExecuteScalar returns an object, so
// we cast to the correct type (string).
string productName = (string) db.ExecuteScalar(dbCommandWrapper);
txtResult.Text = productName;
Demo程序界面如下(注意在项目属性窗口中设置Post-build Event Command Line属性值:copy "$(ProjectDir)*.config" "$(TargetDir)"):
***
作者:Rickie Lee (rickieleemail#yahoo.com)
本文参考Enterprise Library: Data Access Application Block Quick Start范例,简单编写一个DEMO。
References:
1.
2. Rickie, Microsoft patterns & practices Enterprise Library January 2005 [中文稿], http://www.cnblogs.com/rickie/archive/2005/01/30/99443.html
3. Rickie, Enterprise Library released! http://www.cnblogs.com/rickie/archive/2005/01/29/99106.html