代码改变世界

微软企业库5.0学习笔记(三十五)数据访问模块 DataSet以及数据库事务

2010-05-16 11:52  Virus-BeautyCode  阅读(5806)  评论(6编辑  收藏  举报

  使用DataSet进行工作

  使用Database类的ExecuteDataSet方法获取DataSet对象,在DataSet对象中,默认的表名称依次为Table,Table1,Table2.。。。。。。。。。。。。

  如果你想要将数据加载到一个已经存在的DataSet对象中,可以使用LoadDataSet方法。

  

代码
DataSet productDataSet;
// Using a SQL statement and a parameter array.
productDataSet = db.ExecuteDataSet(CommandType.Text, "GetProductsByCategory",
new Object[] { "%bike%" });
// Using a stored procedure and a parameter array.
productDataSet = db.ExecuteDataSet("GetProductsByCategory",
new Object[] { "%bike%" });
// Using a stored procedure and a named parameter.
DbCommand cmd = db.GetStoredProcCommand("GetProductsByCategory");
db.AddInParameter(cmd, 
"CategoryID", DbType.Int32, 7);
productDataSet 
= db.ExecuteDataSet(cmd);
  

 

  将DataSet中的数据更新回数据库

  如果要更新数据库,可以使用Database的UpdateDataSet方法,返回受影响的行数总和。

  有一个参数是UpdateBehavior,决定了更新目标数据库表的方式。包括下面的几个值:

  Standard:如果更新失败,停止更新。

  Continue:如果更新失败,将继续其他的更新。

  Transactional:如果更新失败,所有的操作都将回滚。

  另外你还可以为UpdateDataSet方法的UpdateBatchSize参数提供一个值,设置更新方式为批量更新而不是一个一个的发送到数据库。这么做更高效,但是返回值就是最后一次批量发送的影响行数了,而不是所有的更新总数。

  加载数据到一个已经存在的DataSet  

 

代码
string selectSQL = "SELECT Id, Name, Description FROM Products WHERE Id > 90";
// Fill a DataSet from the Products table using the simple approach
DataSet simpleDS = defaultDB.ExecuteDataSet(CommandType.Text, selectSQL);
DisplayTableNames(simpleDS, 
"ExecuteDataSet");
// Fill a DataSet from the Products table using the LoadDataSet method
// This allows you to specify the name(s) for the table(s) in the DataSet
DataSet loadedDS = new DataSet("ProductsDataSet");
defaultDB.LoadDataSet(CommandType.Text, selectSQL, loadedDS,
new string[] { "Products" });
DisplayTableNames(loadedDS, 
"LoadDataSet");

 

  更新数据库UpdateDataSet

 

代码
string addSQL = "INSERT INTO Products (Name, Description) VALUES (@name,
@description);";
string updateSQL = "UPDATE Products SET Name = @name, Description = @description
WHERE Id = @id";
string deleteSQL = "DELETE FROM Products WHERE Id = @id";
// Create the commands to update the original table in the database
DbCommand insertCommand = defaultDB.GetSqlStringCommand(addSQL);
defaultDB.AddInParameter(insertCommand, 
"name", DbType.String, "Name",
DataRowVersion.Current);
defaultDB.AddInParameter(insertCommand, 
"description", DbType.String,
"Description", DataRowVersion.Current);
DbCommand updateCommand 
= defaultDB.GetSqlStringCommand(updateSQL);
defaultDB.AddInParameter(updateCommand, 
"name", DbType.String, "Name",
DataRowVersion.Current);
defaultDB.AddInParameter(updateCommand, 
"description", DbType.String,
"Description", DataRowVersion.Current);
defaultDB.AddInParameter(updateCommand, 
"id", DbType.String, "Id",
DataRowVersion.Original);
DbCommand deleteCommand 
= defaultDB.GetSqlStringCommand(deleteSQL);
defaultDB.AddInParameter(deleteCommand, 
"id", DbType.Int32, "Id",
DataRowVersion.Original);
Finally, you can apply the changes by calling the UpdateDataSet method, 
as shown here.
// Apply the updates in the DataSet to the original table in the database
int rowsAffected = defaultDB.UpdateDataSet(loadedDS, "Products",
insertCommand, updateCommand, deleteCommand,
UpdateBehavior.Standard);
Console.WriteLine(
"Updated a total of {0} rows in the database.", rowsAffected);

 

 

 

  以数据库连接为基础的事务操作

  就是操作一个数据库,以一个数据库连接为基础。

  一个常用的数据库解决方案,都会包括一些事务的处理,例如银行的转账。

  事务要满足ACID原则:

  Atomicity,原子性,所有的操作要么都成功,要么都失败。

  Consistency,一致性,数据在事务之前和之后保持数据的一致性。  

  Isolation,隔离性,在事务操作的过程中,其他操作不能访问和查看数据。

  Durability,一旦事务成功,在系统中产生的变化将是永久的。

  详细介绍参看:http://www.360doc.com/content/08/0426/18/61497_1217187.shtml

  

代码
using (DbConnection conn = db.CreateConnection())
{
conn.Open();
DbTransaction trans 
= conn.BeginTransaction();
try
{
// execute commands, passing in the current transaction to each one
db.ExecuteNonQuery(cmdA, trans);
db.ExecuteNonQuery(cmdB, trans);
trans.Commit(); 
// commit the transaction
}
catch
{
trans.Rollback(); 
// rollback the transaction
}
}
  

 

  分布式的事务

  如果你的事务包含了访问不同的数据库,或者是包括了消息队列的访问,你必须使用分布式的事务。例如windows的Distributed transaction coordinator(DTC)服务。

  使用TransactionScope类

  

using (TransactionScope scope
= new TransactionScope(TransactionScopeOption.RequiresNew))
{
// perform data access here
}

 

  扩展模块使用其他类型的数据库

  企业库默认支持SQL Server、Oracle和SQL CE。但是你可以扩展来支持更多的数据库。自定义一个provider或者是引入第三方的provider。

  如果是自定义一个provider,你可以继承Database类,然后重写里面的一些方法。在方法中处理一些细节,例如返回值,参数的前缀(@),数据类型转换,和其他的一些相关因素。

 

  未完待续。。。。。。。。。。。。。。。。。。。。。。