简体   繁体   English

参数化DB2 Query From .NET

[英]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: 我试图使用客户端访问ODBC驱动程序使用以下代码从.NET运行针对DB2数据库的参数化查询:

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: 执行时,抛出OdbcException

ERROR [42S22] [IBM][iSeries Access ODBC Driver][DB2 UDB]SQL0206 - Column @LAT not in specified tables. 错误[42S22] [IBM] [iSeries Access ODBC驱动程序] [DB2 UDB] SQL0206 - 列@LAT不在指定的表中。

The internets seem to imply that parameterized queries are supported by the client access ODBC driver, but this error seems to indicate otherwise. 互联网似乎暗示客户端访问ODBC驱动程序支持参数化查询,但此错误似乎表明不是这样。 Is there anything wrong with the supplied code? 提供的代码有什么问题吗?

Have you tried using ? 你试过用过吗? as the placeholder instead of @LAT? 作为占位符而不是@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. 这是MS Access在使用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. 您只需要确保您的Parameters.AddWithValue()语句与INSERT语句中的字段列表的顺序相同。 First parameter passed to AddWithValue() doesn't seem to matter, although by convention I make it the same as the field name. 传递给AddWithValue()的第一个参数似乎并不重要,尽管按惯例我将它与字段名称相同。

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. 您想要添加一个参数,并且需要更改循环中参数的VALUE。

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. 在命令中只有一个占位符 (@Lat)用于参数,因此您应该只添加一个参数。 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. 除非读者返回@Lat值,否则这些参数中没有一个会被命名为“@Lat”。

I'm still pretty positive that you need one parameter (@Lat) and need to modify the value of the parameter in the loop. 我仍然非常肯定您需要一个参数(@Lat)并且需要修改循环中参数的值。

Clarifying the syntax of using parameteized queries, consider this statement: 澄清使用参数化查询的语法,请考虑以下语句:

cmd.CommandText = "Insert Into Person (FirstName, LastName) Values (@fName, @lName) cmd.CommandText =“插入Person(FirstName,LastName)值(@fName,@ list)

In the above statement, @fName and @lName are NOT parameters. 在上面的语句中,@ fName和@lName不是参数。 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. 必须以正确的顺序添加parmaters。

So a more full example would be 所以一个更完整的例子就是

cmd.CommandText = "Insert Into Person (FirstName, LastName) Values (@fName, @lName) cmd.CommandText =“插入Person(FirstName,LastName)值(@fName,@ list)

cmd.Parameters.AddWithValue("@fName", "David"); 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. //在这个上下文中,这一行表示“使用此实际参数从前一行重新生成参数palceholder。cmd.Parameters.AddWithValue(”@ lName“,”Stratton“); //同样,这将替换@lname占位符。

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. 然后,如果我有一个具有一堆名称的datareader,我可以重新将读取器的VALUE分配给参数的VALUE。

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM