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

查看:309
本文介绍了来自.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不在指定的表中。 / p>

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天全站免登陆