如何通过 ODBC C# 绑定参数? [英] How to bind parameters via ODBC C#?

查看:43
本文介绍了如何通过 ODBC C# 绑定参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在来自 C# 的 ODBC 查询上绑定参数.这是示例代码,但 VS 告诉我缺少一个参数.

I need to bind parameters on ODBC query from C#. This is the sample code, but VS tells me that there's one parameter missing.

OdbcCommand cmd = conn.CreateCommand();

cmd.CommandText = "SELECT * FROM user WHERE id = @id";
cmd.Parameters.Add("@id", OdbcType.Int).Value = 4;
OdbcDataReader reader = cmd.ExecuteReader();

在 ODBC 上绑定值的语法是什么?

What is the syntax for binding values on ODBC?

推荐答案

Odbc 不能使用命名参数.这意味着命令字符串为每个参数使用占位符,并且该占位符是一个问号,而不是参数名称.

Odbc cannot use named parameters. This means that the command string uses placeholders for every parameter and this placeholder is a single question mark, not the parameter name.

OdbcCommand.Parameters

然后需要按照它们在命令字符串中出现的顺序添加集合中的参数

Then you need to add the parameters in the collection in the same order in which they appear in the command string

OdbcCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM [user] WHERE id = ?";
cmd.Parameters.Add("@id", OdbcType.Int).Value = 4;
OdbcDataReader reader = cmd.ExecuteReader();

您还有另一个问题,USER 字是每个 MS Access 数据库的保留关键字,如果您想将其用作字段名或表名,则需要用方括号将每个引用括起来.如果可能,我强烈建议更改该表名,因为您会经常遇到此问题.

You have also another problem, the USER word is a reserved keyword per MS Access Database and if you want to use that as field name or table name then it is required to enclose every reference with square brackets. I strongly suggest, if it is possible, to change that table name because you will be hit this problem very often.

这篇关于如何通过 ODBC C# 绑定参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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