I am attempting to run a parameterized query against a DB2 database from .NET using the Client Access ODBC Driver using the following code:
var db2Cmd = new OdbcCommand("INSERT INTO presnlats (LAT) VALUES (@LAT)", db2Conn);
db2Cmd.Parameters.AddWithValue("@LAT", insertValue);
Console.Out.WriteLine(db2Cmd.ExecuteNonQuery());
When executed, an OdbcException
is thrown:
ERROR [42S22] [IBM][iSeries Access ODBC Driver][DB2 UDB]SQL0206 - Column @LAT not in specified tables.
The internets seem to imply that parameterized queries are supported by the client access ODBC driver, but this error seems to indicate otherwise. Is there anything wrong with the supplied code?
Have you tried using ? as the placeholder instead of @LAT?
var db2Cmd = new OdbcCommand("INSERT INTO presnlats (LAT) VALUES (?)", db2Conn);
db2Cmd.Parameters.AddWithValue("LAT", insertValue);
Console.Out.WriteLine(db2Cmd.ExecuteNonQuery());
This is what MS Access requires when using OdbcConnection / OdbcCommand.
You just need to make sure your Parameters.AddWithValue() statements are in the same order as the field list in the INSERT statement. First parameter passed to AddWithValue() doesn't seem to matter, although by convention I make it the same as the field name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With