There's a good article (and code) in CodeProject on how to extend a typed dataset to include transactions without moving them to a distributed transaction when using TransactionScope.
Free information. Use the transaction pane with the method added to the partial class to modify the underlying SqlCommand objects to participate in the same transaction.
using (SqlTransaction transaction = connection.BeginTransaction())
{
adapter1.EnlistTransaction(transaction);
adapter2.EnlistTransaction(transaction);
adapter1.Update(table1);
adapter2.Update(table2);
transaction.Commit();
}
Sample code for the adapter from the link:
public partial class [TableAdapterName]
{
public void EnlistTransaction(System.Data.SqlClient.SqlTransaction transaction)
{
System.Data.SqlClient.SqlTransaction _transaction;
if (this._transaction != null)
{
throw new System.InvalidOperationException
("This adapter has already been enlisted in a transaction");
}
else
{
this._transaction = transaction;
Adapter.UpdateCommand.Transaction = _transaction;
Adapter.InsertCommand.Transaction = _transaction;
Adapter.DeleteCommand.Transaction = _transaction;
}
}
}
source
share