Marina said:
Assign the transaction to the
UpdateCommand.Transaction,DeleteCommand.Transaction and
InsertCommand.Transaction properties of the adapter.
If you are using a commandbuilder, then just assign the transaction to
the
SelectCommand.Transaction.
But how, if is SqlAdapter generated by VS2005b2 for StronglyTyped dataset,
which
keep back SqlCommands in own class?
Example:
Have: Generated VS2005b2 partial classes, ADataTable and ATableAdapter,
BDataTable and BTableAdapter
Necessary: Update ADataTable and BDataTable with SqlAdapter.Update in
transaction context
I think do partial class for ATableAdapter and BTableAdapter, which
implemets Transaction property:
public partial class ATableAdapter
{
private SqlTransaction transaction;
public SqlTransaction Transaction
{
get { return transaction; }
set
{
transaction = value;
if( Adapter.InsertCommand != null )
{
Adapter.InsertCommand.Transaction = transaction;
}
if( Adapter.DeleteCommand != null )
{
Adapter.DeleteCommand.Transaction = transaction;
}
if( Adapter.UpdateCommand != null )
{
Adapter.UpdateCommand.Transaction = transaction;
}
for( int i = 0; i < this.CommandCollection.Length; i = i + 1 )
{
if( this.CommandCollection
!= null )
{
(System.Data.SqlClient.SqlCommand) ( this.CommandCollection
).Transaction = transaction;
}
}
}
}
}
And also for BTableAdapter...
Use it:
using(SqlConnection conn = new SqlConnection( "conn string" ))
{
SqlTransaction tran = conn.BeginTransaction();
ATableAdapter ata = new ATableAdapter();
ata.Connection = conn;
ata.Transaction = tran;
ata.Update( aTable );
BTableAdapter bta = new ATableAdapter();
bta.Connection = conn;
bta.Transaction = tran;
bta.Update( bTable );
tran.Commit();
}
Exception handling and rollbacking not include for simple
Sorry for my English