系统比较复杂,更新多个表,所以需要使用事务处理
https://www.microsoft.com/china/msdn/library/webservices/asp.net/dnbdabdasamppet4.mspx?mfr=true
public void TransactionTest()
{
string connectionString = "";
IDbConnection connection = new SqlConnection(connectionString);
connection.Open();
IDbCommand command = new SqlCommand();
command.Connection = connection;
IDbTransaction transaction;
transaction = connection.BeginTransaction(); //Enlisting database
command.Transaction = transaction;
try
{
/* Interact with database here, then commit the transaction
*/
transaction.Commit();
}
catch
{
transaction.Rollback(); //Abort transaction
}
finally
{
connection.Close();
}
}
https://www.microsoft.com/china/msdn/library/webservices/asp.net/dnbdabdasamppet4.mspx?mfr=true
http://editblog.csdn.net/msdncolumn/archive/2005/02/25/1658.aspx
http://blog.joycode.com/kaneboy/archive/2005/02/15/44356.aspx 介绍最清楚System.Transactions
http://idior.cnblogs.com/archive/2005/08/15/214300.html 介绍最深入System.Transactions
Transaction Team Wiki
Florin Lazar's Blog
Jim Johnson's Blog
Angle Saenz-Badillos' Blog
public void TransactionTest()
{
string connectionString = "";
IDbConnection connection = new SqlConnection(connectionString);
connection.Open();
IDbCommand command = new SqlCommand();
command.Connection = connection;
IDbTransaction transaction;
transaction = connection.BeginTransaction(); //Enlisting database
command.Transaction = transaction;
try
{
/* Interact with database here, then commit the transaction
*/
transaction.Commit();
}
catch
{
transaction.Rollback(); //Abort transaction
}
finally
{
connection.Close();
}
}
什么是 System.Transactions?
System.Transactions 是 .NET 2.0 框架中新增的事务控件命名空间。它是一种处理分布式事务的新方式,没有 COM+ 注册和 COM+ 目录的开销。请注意,Microsoft 分布式事务协调器用于初始化事务。
运行情况
同步定单处理中的 Order.Insert() 方法使用 System.Transactions 插入一个定单并更新库存。通过添加对 System.Transaction 命名空间的引用,并将定单插入方法和库存减少方法包装在 TransactionScope 内,我们实现了 Order.Insert() 方法,如代码清单 1 所示。
清单 1. 运行中的 System.Transactions
using System; using System.Transactions; using PetShop.IBLLStrategy; namespace PetShop.BLL { /// <summary> /// This is a synchronous implementation of IOrderStrategy /// By implementing IOrderStrategy interface, the developer can /// add a new order insert strategy without re-compiling the whole /// BLL. /// </summary> public class OrderSynchronous : IOrderStrategy { ... /// <summary> /// Inserts the order and updates the inventory stock within /// a transaction. /// </summary> /// <param name="order">All information about the order</param> public void Insert(PetShop.Model.OrderInfo order) { using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required)) { dal.Insert(order); // Update the inventory to reflect the current inventory // after the order submission. Inventory inventory = new Inventory(); inventory.TakeStock(order.LineItems); // Calling Complete commits the transaction. // Excluding this call by the end of TransactionScope's // scope will rollback the transaction. ts.Complete(); } } } }
<%@Page Language="VB" Debug="True"%>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>
<html>
<body>
<h2>Original DataSet that was read in from the database:</h2>
<asp:datagrid id="ShowMembersOriginal" runat="server"/>
<h2>DataSet after changes:</h2>
<asp:datagrid id="ShowMembersWorking" runat="server"/>
</body>
</html>
<script language="vb" runat="server">
Sub Page_Load()
Dim strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\edward\Association.mdb"
Dim strSelect = "SELECT * FROM Members"
Dim objDataSet As New DataSet()
Dim objConnect As New OleDbConnection(strConnect)
Dim objDataAdapter As New OleDbDataAdapter(strSelect, objConnect)
objDataAdapter.Fill(objDataSet, "TheMembers")
objDataSet.AcceptChanges()
Dim objTable As DataTable = objDataSet.Tables("TheMembers")
ShowMembersOriginal.DataSource = objTable.DefaultView
ShowMembersOriginal.DataBind()
objTable.Rows(0).Delete()
objTable.Rows(1)("LastName") = "-NewLastName-"
objTable.Rows(2).Delete()
objTable.Rows(3).Delete()
objTable.Rows(4)("Contributions") = "9999"
objTable.Rows.RemoveAt(5)
objTable.Rows(5)("FirstName") = "-NewFirstName-"
Dim objValsArray(2) As Object
objValsArray(0) = 0
objValsArray(1) = "NewOneFirstName"
objValsArray(2) = "NewOneLastName"
objTable.Rows.Add(objValsArray)
Dim objTransaction As OleDbTransaction
Dim objCommandBuilder As New OleDbCommandBuilder(objDataAdapter)
objDataAdapter.DeleteCommand = objCommandBuilder.GetDeleteCommand()
objDataAdapter.InsertCommand = objCommandBuilder.GetInsertCommand()
objDataAdapter.UpdateCommand = objCommandBuilder.GetUpdateCommand()
objConnect.Open()
objTransaction = objConnect.BeginTransaction()
objDataAdapter.DeleteCommand.Transaction = objTransaction
objDataAdapter.InsertCommand.Transaction = objTransaction
objDataAdapter.UpdateCommand.Transaction = objTransaction
objDataAdapter.Update(objDataSet, "TheMembers")
ShowMembersWorking.DataSource = objTable.DefaultView
ShowMembersWorking.DataBind()
'objTransaction.Commit()
objTransaction.RollBack()
End Sub
</script>