企业库之数据访问的一个小应用
使用过微软的企业库的朋友应该能感受到Application Block给我们带来的方便和高速的开发体验,
特别是在DataAccessApplicationBlock这一块.它简化了数据访问的公共方法,并且能适用于多种
应用情景,比如读取数据来显示,在各应用层之间传递数据,或提交更新后的数据到数据库.应用程
序块支持存储过程,内联SQL语句.同时压缩了像管理连接对象,创建和缓存参数等通用任务管理的应
用程序方法体.换句话说,数据访问应用程序块在简化的类中提供了ADO.net频繁使用的功能,推进了
开发的效率.
毋须多言,下面介绍一个使用DataSet更新数据库的范例,其中有借助了ADO.net中的离线模型的功能.
1.准备一组增加,删除,修改的存储过程,代码如下:
CREATE PROCEDURE AddProduct
(
@ProductName nvarchar(50),
@CategoryID int,
@UnitPrice money
)
AS
INSERT INTO Products (ProductName, CategoryID, UnitPrice)
VALUES (@ProductName, @CategoryID, @UnitPrice)
SELECT ProductID, ProductName, CategoryID, UnitPrice
FROM Products
WHERE ProductID = SCOPE_IDENTITY()
GO
CREATE PROCEDURE DeleteProduct
(
@ProductID int
)
AS
DELETE Products
WHERE ProductID = @ProductID
GO
CREATE PROCEDURE UpdateProduct
(
@ProductID int,
@ProductName nvarchar(50),
@LastUpdate datetime
)
AS
UPDATE Products
SET ProductName = @ProductName
WHERE ProductID = @ProductID AND LastUpdate = @LastUpdate
IF @@ROWCOUNT > 0
-- This statement is used to update the DataSet if changes are done on the updated record (identities, timestamps or triggers )
SELECT ProductID, ProductName, CategoryID, UnitPrice
FROM Products
WHERE ProductID = @ProductID
GO
(
@ProductName nvarchar(50),
@CategoryID int,
@UnitPrice money
)
AS
INSERT INTO Products (ProductName, CategoryID, UnitPrice)
VALUES (@ProductName, @CategoryID, @UnitPrice)
SELECT ProductID, ProductName, CategoryID, UnitPrice
FROM Products
WHERE ProductID = SCOPE_IDENTITY()
GO
CREATE PROCEDURE DeleteProduct
(
@ProductID int
)
AS
DELETE Products
WHERE ProductID = @ProductID
GO
CREATE PROCEDURE UpdateProduct
(
@ProductID int,
@ProductName nvarchar(50),
@LastUpdate datetime
)
AS
UPDATE Products
SET ProductName = @ProductName
WHERE ProductID = @ProductID AND LastUpdate = @LastUpdate
IF @@ROWCOUNT > 0
-- This statement is used to update the DataSet if changes are done on the updated record (identities, timestamps or triggers )
SELECT ProductID, ProductName, CategoryID, UnitPrice
FROM Products
WHERE ProductID = @ProductID
GO
2.建立操作数据库的函数
Database db = DatabaseFactory.CreateDatabase();//使用默认连接字符串配置
//初始化DataSet并填充数据
DataSet productsDataSet = new DataSet();
string sqlCommand = "Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate " +
"From Products";
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
string productsTable = "Products";
db.LoadDataSet(dbCommand, productsDataSet, productsTable);
// 从DataSet中得到DataTable
DataTable table = productsDataSet.Tables[productsTable];
//增加一行新值
DataRow addedRow = table.Rows.Add(new object[] {DBNull.Value, "New product", 11, 25});
//修改一行记录的值
table.Rows[0]["ProductName"] = "Modified product";
//创建DbCommand对象,将增加,删除,修改的存储过程与待更新的DataSet绑定.
DbCommand insertCommand = db.GetStoredProcCommand("AddProduct");
db.AddInParameter(insertCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
db.AddInParameter(insertCommand, "CategoryID", DbType.Int32, "CategoryID", DataRowVersion.Current);
db.AddInParameter(insertCommand, "UnitPrice", DbType.Currency, "UnitPrice", DataRowVersion.Current);
DbCommand deleteCommand = db.GetStoredProcCommand("DeleteProduct");
db.AddInParameter(deleteCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
DbCommand updateCommand = db.GetStoredProcCommand("UpdateProduct");
db.AddInParameter(updateCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
db.AddInParameter(updateCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
db.AddInParameter(updateCommand, "LastUpdate", DbType.DateTime, "LastUpdate", DataRowVersion.Current);
//提交DataSet,自动调用存储过程更新数据库中相关的记录
int rowsAffected = db.UpdateDataSet(productsDataSet, "Products", insertCommand, updateCommand,
deleteCommand, Microsoft.Practices.EnterpriseLibrary.DataAccess.UpdateBehavior.Standard);
//初始化DataSet并填充数据
DataSet productsDataSet = new DataSet();
string sqlCommand = "Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate " +
"From Products";
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
string productsTable = "Products";
db.LoadDataSet(dbCommand, productsDataSet, productsTable);
// 从DataSet中得到DataTable
DataTable table = productsDataSet.Tables[productsTable];
//增加一行新值
DataRow addedRow = table.Rows.Add(new object[] {DBNull.Value, "New product", 11, 25});
//修改一行记录的值
table.Rows[0]["ProductName"] = "Modified product";
//创建DbCommand对象,将增加,删除,修改的存储过程与待更新的DataSet绑定.
DbCommand insertCommand = db.GetStoredProcCommand("AddProduct");
db.AddInParameter(insertCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
db.AddInParameter(insertCommand, "CategoryID", DbType.Int32, "CategoryID", DataRowVersion.Current);
db.AddInParameter(insertCommand, "UnitPrice", DbType.Currency, "UnitPrice", DataRowVersion.Current);
DbCommand deleteCommand = db.GetStoredProcCommand("DeleteProduct");
db.AddInParameter(deleteCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
DbCommand updateCommand = db.GetStoredProcCommand("UpdateProduct");
db.AddInParameter(updateCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
db.AddInParameter(updateCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
db.AddInParameter(updateCommand, "LastUpdate", DbType.DateTime, "LastUpdate", DataRowVersion.Current);
//提交DataSet,自动调用存储过程更新数据库中相关的记录
int rowsAffected = db.UpdateDataSet(productsDataSet, "Products", insertCommand, updateCommand,
deleteCommand, Microsoft.Practices.EnterpriseLibrary.DataAccess.UpdateBehavior.Standard);
这个方法不需要主动调用增加,删除,修改的存储过程,而是指派给DbCommand对象,由它根据提交的数据集
自动比照来决定何时调用,此乃居家必备之良方-----相当滴省心啊.
关于企业库的好东东,希望各位DotNeters多多交流.以便我们的开发更有乐趣.