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();
}