{
//do something here
ts.Complete();
}
方法二:將connection的modifier屬性改爲public,然後操作connection,如下:
table2TableAdapter.Connection = connection;
// Start a local Transaction
SqlTransaction transaction = connection.BeginTransaction();
table1TableAdapter.MyAdapter.InsertCommand.Transaction = transaction;
table2TableAdapter.MyAdapter.InsertCommand.Transaction = transaction;
try
{
// Update Database
table1TableAdapter.Update(dataSet1.Table1);
table2TableAdapter.Update(dataSet1.Table2);
// Commit Changes to database
transaction.Commit();
}
//more code here
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
using System.Data.Common;
namespace CDSafe.DBUtilities
{
/// <summary>
/// a helper class when u are using a dataset's data adapter.
/// it use the reflection to add the transaction into the data adpater's connection
/// </summary>
public sealed class DataAdapterHelper
{
/// <summary>
/// begin the transaction
/// </summary>
/// <param name="tableAdapter">the first data adapter in the transaction</param>
/// <param name="isolationLevel">the isolation level of the transaction</param>
/// <returns> a transaction object, use to add another data adapter into the same transaction</returns>
public static DbTransaction BeginTransaction(object tableAdapter,IsolationLevel isolationLevel)
{
Type adapterType = tableAdapter.GetType();
DbConnection connection = GetAdpaterConnection(tableAdapter);
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
DbTransaction transaction = connection.BeginTransaction(isolationLevel);
SetTransaction(tableAdapter, transaction);
return transaction;
}
/// <summary>
/// begin the transaction
/// </summary>
/// <param name="tableAdapter">the first data adapter in the transaction</param>
/// <returns>a transaction object, use to add another data adapter into the same transaction</returns>
public static DbTransaction BeginTransaction(object tableAdapter)
{
return BeginTransaction(tableAdapter, IsolationLevel.ReadCommitted);
}
/// <summary>
/// use the reflection to get the table adapter's connection object
/// </summary>
/// <param name="tableAdapter"></param>
/// <returns>the connection object</returns>
private static DbConnection GetAdpaterConnection(object tableAdapter)
{
Type adapterType = tableAdapter.GetType();
PropertyInfo connectionProperty = adapterType.GetProperty("Connection",
BindingFlags.NonPublic | BindingFlags.Instance);
DbConnection connection = (DbConnection)connectionProperty.GetValue(tableAdapter, null);
return connection;
}
/// <summary>
/// attach the connection which contains a transaction on the data adapter
/// </summary>
/// <param name="tableAdapter"></param>
/// <param name="connection"></param>
private static void SetConnection(object tableAdapter, DbConnection connection)
{
Type type = tableAdapter.GetType();
PropertyInfo connectionProperty = type.GetProperty("Connection", BindingFlags.NonPublic | BindingFlags.Instance);
connectionProperty.SetValue(tableAdapter, connection, null);
}
/// <summary>
/// set transaction on the other data adapter
/// </summary>
/// <param name="tableAdapter"></param>
/// <param name="transaction"></param>
public static void SetTransaction(object tableAdapter, DbTransaction transaction)
{
Type adapterType = tableAdapter.GetType();
PropertyInfo commandsProperty = adapterType.GetProperty("CommandCollection",
BindingFlags.NonPublic | BindingFlags.Instance);
DbCommand[] commands = (DbCommand[])commandsProperty.GetValue(tableAdapter, null);
foreach (DbCommand command in commands)
{
command.Transaction = transaction;
}
PropertyInfo adpterProperty = adapterType.GetProperty("Adapter",
BindingFlags.NonPublic | BindingFlags.Instance);
DbDataAdapter dataAdapter = (DbDataAdapter)adpterProperty.GetValue(tableAdapter, null);
if (dataAdapter.InsertCommand != null)
{
dataAdapter.InsertCommand.Transaction = transaction;
}
if (dataAdapter.DeleteCommand != null)
{
dataAdapter.DeleteCommand.Transaction = transaction;
}
if (dataAdapter.UpdateCommand != null)
{
dataAdapter.UpdateCommand.Transaction = transaction;
}
if (dataAdapter.SelectCommand != null)
{
dataAdapter.SelectCommand.Transaction = transaction;
}
SetConnection(tableAdapter, transaction.Connection);
}
}
}
該helper class使用範例如下:
TableAdapter2 adapter2 = new TableAdapter2();
DbTransaction trans = DataAdapterHelper.BeginTransaction(adapter1);
DataAdapterHelper.SetTransaction(adapter2, trans);
adapter1.Insert("1", "2");
adapter2.Insert("3", "4", null);
trans.Commit();