Reputation: 2448
I have find this How to bind parameters via ODBC C#? example of how parametrization via ODBC in C#, but in my code CommandText stays with "?" and returns error - whitch is expected.
Here is my code:
string variable = SVariablaKlas.ToUpper(); //this is simple var like 123
Conn odbc = new Conn();
odbc.db.Open(); //this works fine -> connection is established
OdbcCommand dbCommand = odbc.db.CreateCommand();
dbCommand.CommandText = "";
if (a)
{
dbCommand.CommandText = "SELECT col1 FROM TableName WHERE ID = ?";
} else if (b)
{
dbCommand.CommandText = "SELECT col2 FROM TableName WHERE ID = ?";
}
dbCommand.Parameters.ADD("@ID", OdbcType.Char).Value = variable;
//or if I try this: ...Parameters.ADD(new OdbcParameter(string.Empty, variable);
OdbcDataReader dbReader = dbCommand.ExecuteReader();
...
In both my cases of "Parameters.ADD", my dbCommand.CommandText would be "SELECT col1 FROM TableName WHERE ID = ?" and then I would get error: ERROR [HY000] near "WHERE": syntax error (1)
Any idea where is problem?
For database I am using SQLite3 and ODBC driver SQLite3 ODBC Driver, version 0.9993.00.00
Upvotes: 0
Views: 370
Reputation: 6222
Your SQL should be
dbCommand.CommandText = "SELECT col2 FROM TableName WHERE ID = @ID";
since your parameter is named as "@ID"
Upvotes: 2