来自 .NET 的参数化 DB2 查询 [英] Parameterized DB2 Query From .NET

查看:23
本文介绍了来自 .NET 的参数化 DB2 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用客户端访问 ODBC 驱动程序使用以下代码从 .NET 对 DB2 数据库运行参数化查询:

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

执行时,抛出OdbcException:

错误 [42S22] [IBM][iSeries Access ODBC 驱动程序][DB2 UDB]SQL0206 - 列 @LAT 不在指定表中.

ERROR [42S22] [IBM][iSeries Access ODBC Driver][DB2 UDB]SQL0206 - Column @LAT not in specified tables.

互联网似乎暗示客户端访问 ODBC 驱动程序支持参数化查询,但此错误似乎另有说明.提供的代码有什么问题吗?

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?

推荐答案

你用过吗?作为占位符而不是@LAT?

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

这是 MS Access 在使用 OdbcConnection/OdbcCommand 时所需要的.

This is what MS Access requires when using OdbcConnection / OdbcCommand.

您只需要确保您的 Parameters.AddWithValue() 语句与 INSERT 语句中的字段列表的顺序相同.传递给 AddWithValue() 的第一个参数似乎无关紧要,尽管按照惯例,我将其与字段名称相同.

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.

这篇关于来自 .NET 的参数化 DB2 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆