Parameterized DB2 Query From .NET

回眸只為那壹抹淺笑 提交于 2019-12-01 17:28:25

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 I'm guessing right at what you're trying to do you want to do this:

You want to add ONE parameter, and need to change the VALUE of the parameter in the loop.

var db2Cmd = new OdbcCommand("INSERT INTO presnlats (LAT) VALUES (@Lat)", db2Conn);
db2Cmd.Parameters.AddWithValue("@Lat", 0);


for (int j = 0; j < reader.FieldCount; ++j)
{
   db2Cmd.Parameters["@Lat"].Value = reader[j];
   Console.Out.WriteLine(db2Cmd.ExecuteNonQuery());
}

Added

You only have one placeholder (@Lat) for your parameter in the command, so you should be adding only one parameter. Your code is adding a new parameter for every object in the reader. Not one of those parameters would be named "@Lat" unless youre reader is returning a value of @Lat.

I'm still pretty positive that you need one parameter (@Lat) and need to modify the value of the parameter in the loop.

Clarifying the syntax of using parameteized queries, consider this statement:

cmd.CommandText = "Insert Into Person (FirstName, LastName) Values (@fName, @lName)

In the above statement, @fName and @lName are NOT parameters. They are placeholders for parameters.

You need to then explicitly add the paramaters using the following rules:

  • The parameters must be named exactly the same as the placeholders
  • The parmaters must be added in the right order.

So a more full example would be

cmd.CommandText = "Insert Into Person (FirstName, LastName) Values (@fName, @lName)

cmd.Parameters.AddWithValue("@fName", "David"); // This line, in this context, says "Repalce the parameter palceholder from the previous line with this actual parameter. cmd.Parameters.AddWithValue("@lName", "Stratton"); // similarly, this replaces the @lname placeholder.

Then if I have a datareader that has a bunch of names, I can repleatedly assign the VALUE fromthe reader to the VALUE of the parameter.

while (myReader.Read()) { cmd.Parameters["@fName'].Value = myReader.GetString("FirstNameField"); cmd.Parameters["@lName'].Value = myReader.GetString("LastNameField"); cmd.ExecuteNonQuery();

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!