关于VS2005中自动生成TableAdapter的事务处理
这里有个方法 http://www.developpez.net/forums/viewtopic.php?p=2527088
看看源程序大家应该知道怎么做可以解决这个问题了.
using System;
using System.Data;
using System.Data.SqlClient;
![](/Images/OutliningIndicators/None.gif)
namespace DataAccess.DataSet1TableAdapters
![](/Images/OutliningIndicators/None.gif)
{
partial class Table1TableAdapter
{
public SqlDataAdapter MyAdapter
{
get
{
return this.Adapter;
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
partial class Table2TableAdapter
![](/Images/OutliningIndicators/InBlock.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
public SqlDataAdapter MyAdapter
![](/Images/OutliningIndicators/InBlock.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
get
{
return this.Adapter;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
// Here is the method to implement the Transaction:
![](/Images/OutliningIndicators/None.gif)
private void ExecuteTransaction()
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
// The Connection must be identical in order to apply Transaction
![](/Images/OutliningIndicators/InBlock.gif)
SqlConnection connection = table1TableAdapter.Connection;
table2TableAdapter.Connection = connection;
![](/Images/OutliningIndicators/InBlock.gif)
// Start a local Transaction
![](/Images/OutliningIndicators/InBlock.gif)
SqlTransaction transaction = connection.BeginTransaction();
![](/Images/OutliningIndicators/InBlock.gif)
table1TableAdapter.MyAdapter.InsertCommand.Transaction = transaction;
![](/Images/OutliningIndicators/InBlock.gif)
table2TableAdapter.MyAdapter.InsertCommand.Transaction = transaction;
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/InBlock.gif)
{
// Update Database
table1TableAdapter.Update(dataSet1.Table1);
table2TableAdapter.Update(dataSet1.Table2);
// Commit Changes to database
transaction.Commit();
}
![](/Images/OutliningIndicators/InBlock.gif)
catch (DataException ex)
![](/Images/OutliningIndicators/InBlock.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
// Roll back the transaction.
![](/Images/OutliningIndicators/InBlock.gif)
if (transaction != null)
![](/Images/OutliningIndicators/InBlock.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
transaction.Rollback();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("Message: {0}", ex.Message);
![](/Images/OutliningIndicators/InBlock.gif)
}
finally
{
![](/Images/OutliningIndicators/InBlock.gif)
// Close Conection
connection.Close();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
但是,这个写法,还是太傻瓜了,
在下抛一砖,希望能引玉无数.
看看源程序大家应该知道怎么做可以解决这个问题了.
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
在下抛一砖,希望能引玉无数.