Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top