来自 .NET 的参数化 DB2 查询
我正在尝试使用客户端访问 ODBC 驱动程序使用以下代码从 .NET 对 DB2 数据库运行参数化查询:
var db2Cmd = new OdbcCommand("INSERT INTO presnlats (LAT) VALUES (@LAT)", db2Conn);
db2Cmd.Parameters.AddWithValue("@LAT", insertValue);
Console.Out.WriteLine(db2Cmd.ExecuteNonQuery());
执行时,抛出 OdbcException
:
错误 [42S22] [IBM][iSeries Access ODBC 驱动程序][DB2 UDB]SQL0206 - 列 @LAT 不在指定表中。
互联网似乎暗示参数化查询是客户端访问 ODBC 驱动程序支持,但此错误似乎另有说明。提供的代码有什么问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你尝试过使用吗?作为占位符而不是@LAT?
这是使用 OdbcConnection / OdbcCommand 时 MS Access 所需要的。
您只需要确保您的Parameters.AddWithValue() 语句与INSERT 语句中的字段列表的顺序相同。传递给 AddWithValue() 的第一个参数似乎并不重要,尽管按照惯例我将其与字段名称相同。
Have you tried using ? as the placeholder instead of @LAT?
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.
如果我猜对了你想要做什么,你想要这样做:
你想添加一个参数,并且需要更改循环中参数的值。
已添加
您的参数只有一个占位符 (@Lat)
在命令中,因此您应该只添加一个参数。
您的代码正在为中的每个对象添加一个新参数
读者。这些参数中没有一个会被命名为“@Lat”,除非您的读者返回@Lat 值。
我仍然非常肯定您需要一个参数(@Lat)并且需要修改循环中参数的值。
为了阐明使用参数化查询的语法,请考虑以下语句:
cmd.CommandText = "Insert Into Person (FirstName, LastName) Values (@fName, @lName)
在上面的语句中,@fName 和 @lName 不是参数。它们是占位符 参数
然后,您需要使用以下规则显式添加参数:
因此更完整的示例是
cmd.CommandText = "Insert。进入 Person (FirstName, LastName) Values (@fName, @lName)
cmd.Parameters.AddWithValue("@fName", "David"); // 在此上下文中,此行表示“用此实际参数替换上一行中的参数 palceholder。
cmd.Parameters.AddWithValue("@lName", "Stratton"); // 类似地,这会替换 @lname 占位符。
然后,如果我有一个包含一堆名称的数据读取器,我可以将读取器中的值重复分配给参数的值。
while (myReader.Read())
{
cmd.Parameters["@fName'].Value = myReader.GetString("FirstNameField");
cmd.Parameters["@lName'].Value = myReader.GetString("LastNameField");
cmd.ExecuteNonQuery();
}
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.
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:
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();
}