esausilva
esausilva

Reputation: 2116

C# Prepared Statement and multiple queries

Is it possible to execute multiple queries with the same prepared statement (same OdbcCommand object)?

Below is the code I have and is throwing the following exception: System.Data.Odbc.OdbcException was caught Message=ERROR [07006] [IBM][CLI Driver] CLI0102E Invalid conversion. SQLSTATE=07006

 ...
        odbcConnection = myConnection.getOdbcConnection();
        odbcConnection.Open();
        odbcCommand = odbcConnection.CreateCommand();
        odbcTrans = odbcConnection.BeginTransaction(IsolationLevel.ReadCommitted);
        odbcCommand.Transaction = odbcTrans;
try{
    odbcCommand.CommandText = queryStatement1();
            odbcCommand.Parameters.AddWithValue("?ID1", parameter1);
            odbcCommand.Parameters.AddWithValue("?ID2", parameter2);
    ...
    odbcCommand.Parameters.AddWithValue("?ID11", parameter3);
    odbcCommand.Prepare();
            odbcCommand.ExecuteNonQuery();


    odbcCommand.CommandText = queryStatement2();
    odbcCommand.Parameters.AddWithValue("?ID1", parameter4);
            odbcCommand.Parameters.AddWithValue("?ID2", parameter5);
    ...
    odbcCommand.Parameters.AddWithValue("?ID13", parameter6);
    odbcCommand.Prepare();
            odbcCommand.ExecuteNonQuery();
    odbcTrans.Commit();
} catch(Exception e){ ... }
...

Same OdbcCommand object, two different queries...

Upvotes: 2

Views: 2811

Answers (1)

esausilva
esausilva

Reputation: 2116

Found the answer. I just needed to clear the command parameters right before assigning a different query string to the command

...
odbcCommand.ExecuteNonQuery();

            odbcCommand.Parameters.Clear();
            odbcCommand.CommandText = queryStatement2();
...

Upvotes: 2

Related Questions