用Enterprise Library Configuration进行配置,生成App.config文件内容如下:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
1
.增加对
Microsoft.Practices.EnterpriseLibrary.Data.dll
和
Microsoft.Practices.EnterpriseLibrary.Common.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;
更改来源于:http://www.cnblogs.com/rickie/archive/2005/02/06/102656.html