如何绑定通过ODBC C#参数? [英] How to bind parameters via ODBC C#?
问题描述
我需要绑定从C#ODBC查询参数。这是样板code,但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.
<一个href=\"http://msdn.microsoft.com/en-us/library/vstudio/system.data.odbc.odbccommand.parameters%28v=vs.100%29.aspx\">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();
您也有另外一个问题,用户词是每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屋!