Search code examples
odbcinformix

IBM Informix ODBC driver C# - The cursor has been previously released and is unavailable


Trying to switch database using Database (database name); query and then execute a query on that database in two separate queries.

However, I got this error: The cursor has been previously released and is unavailable. What does this mean?

Here is the code:

using (OdbcCommand command = new OdbcCommand(null, odbcConnection))
{
    if (switchDBName != null)
    {
        command.CommandText = "Database " + switchDBName + ";";
        command.ExecuteNonQuery();
        Console.WriteLine("Switched to database: " + switchDBName + " Successfully.");
    }
    command.CommandTimeout = 0;
    command.CommandText = query;
    using (OdbcDataReader datareader = command.ExecuteReader())
    {
        DataTable resultDT = new DataTable();
        resultDT.Load(datareader);
        Console.WriteLine("ExecuteODBCQuery -- Finished. There are " + resultDT.Rows.Count + " rows.");
        return resultDT;
    }
}

Solution

  • If I'm following the logic correctly:

    • you have two different database connections;
    • you create a cursor-based statement using one connection;
    • you switch to the other database connection;
    • you get an error indicating that the cursor is not available on the current connection
      • albeit with not wholly appropriate wording in the error message.

    A prepared statement or a cursor-based statement is associated with a connection. The same statement will not work for another connection; you have to recreate the cursor or prepared statement for each database connection.

    You must know which connection was used for each prepared statement or cursor-based statement. You can switch connections between uses, but when you try to use a statement, the connection must be the same as was in use when it was prepared.