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;
}
}
If I'm following the logic correctly:
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.