Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized DB2 Query From .NET

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?

like image 244
Karmic Coder Avatar asked Mar 03 '10 20:03

Karmic Coder


1 Answers

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.

like image 78
tomfanning Avatar answered Sep 21 '22 20:09

tomfanning