SqlDataAdapter & IDbTransaction

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi;

If I do a IDbConnection.BeginTransaction(), will that transaction be used
when I call SqlDataAdapter.Fill() if I use the same connection?
 
Hi Dave,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know if the transaction
will be automatically applied to SqlDataAdapter after BeginTransaction is
called. If there is any misunderstanding, please feel free to let me know.

As far as I know, it will not be automatically applied. If you call Fill
directly after using BeginTransaction, the following exception will be
thrown.

An unhandled exception of type 'System.InvalidOperationException' occurred
in system.data.dll

Additional information: Execute requires the command to have a transaction
object when the connection assigned to the command is in a pending local
transaction. The Transaction property of the command has not been
initialized.

So we have to get the transaction object like:

SqlTransaction t = this.sqlConnection1.BeginTransaction();

And then assign it to the Transaction property of the DataAdapter's
SelectCommand.

sda.SelectCommand.Transaction = t;

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top