문제

I'm trying to get a DataTable filled with Data from a firebird database. But it fails at the point da.Fill(ds, "Result");

The error message is:

System.InvalidOperationException: Execute requires the Command object to have a Transaction object when the Connection object assigned to the command is in a pending local transaction. The Transaction property of the Command has not been initialized.

try
{
   FbTransaction transaction = Fbconnection.BeginTransaction();
   FbCommand command = new FbCommand(Fbsql, Fbconnection, transaction);
   FbDataAdapter da = new FbDataAdapter(Fbsql, Fbconnection);
   DataSet ds = new DataSet();
   da.Fill(ds, "Result");
   DataTable dt = ds.Tables["Result"];
   this.CloseConnection();
   return dt;
}

Any help on how to get this working is appreciated.

도움이 되었습니까?

해결책

Taking a closer look at the code, you are creating a FbCommand and a transaction that you aren't actually using. Now when you execute the FbDataAdapter, the Firebird .NET provider notices the connection is already using a transaction and throws the exception.

You need to change your code either to:

FbTransaction transaction = Fbconnection.BeginTransaction();
FbCommand command = new FbCommand(Fbsql, Fbconnection, transaction);
FbDataAdapter da = new FbDataAdapter(command);

Or without using an explicit separate transaction:

FbDataAdapter da = new FbDataAdapter(Fbsql, Fbconnection);

BTW: I'd strongly suggest you apply using to objects that are disposable, to be sure resources are cleaned up properly.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top