内容简介:翻译自:https://stackoverflow.com/questions/1535994/asp-net-odbc-query-with-parameters
请帮帮我,我不知道下面的代码有什么问题:
OdbcConnection conn = new OdbcConnection(connString);
String query = "INSERT INTO customer (custId, custName, custPass, "+
"custEmail, custAddress, custAge) VALUES (" +
"@ID, @Name, @Pass, @Email, @Address, @Age)";
OdbcCommand exe = new OdbcCommand(query, conn);
exe.Parameters.Add("@ID", OdbcType.UniqueIdentifier).Value = id;
exe.Parameters.Add("@Name", OdbcType.VarChar).Value = name;
exe.Parameters.Add("@Pass", OdbcType.VarChar).Value = pass;
exe.Parameters.Add("@Email", OdbcType.VarChar).Value = email;
exe.Parameters.Add("@Address", OdbcType.VarChar).Value = address;
exe.Parameters.Add("@Age", OdbcType.Int).Value = age;
conn.Open();
exe.ExecuteNonQuery(); // ERROR [07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 6.
这段代码抛出了太少的参数.我尝试执行查询时出错.数据库很好,当我将值硬编码到查询中而不是使用参数时,它工作正常.
谢谢.
来自MSDN:
When CommandType is set to Text, the .NET Framework Data Provider for ODBC does not support passing named parameters to an SQL statement or to a stored procedure called by an OdbcCommand. In either of these cases, use the question mark (?) placeholder. For example:
SELECT * FROM Customers WHERE CustomerID = ?
将您的查询重写为
OdbcConnection conn = new OdbcConnection(connString);
String query = "INSERT INTO customer (custId, custName, custPass, "+
"custEmail, custAddress, custAge) VALUES (" +
"?, ?, ?, ?, ?, ?)";
参数顺序计数!
编辑:参数可以这样添加:
OdbcCommand exe = new OdbcCommand(query, conn);
exe.Parameters.Add("ID", OdbcType.UniqueIdentifier).Value = id;
exe.Parameters.Add("Name", OdbcType.VarChar).Value = name;
exe.Parameters.Add("Pass", OdbcType.VarChar).Value = pass;
exe.Parameters.Add("Email", OdbcType.VarChar).Value = email;
exe.Parameters.Add("Address", OdbcType.VarChar).Value = address;
exe.Parameters.Add("Age", OdbcType.Int).Value = age;
翻译自:https://stackoverflow.com/questions/1535994/asp-net-odbc-query-with-parameters
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Java内部DNS查询实现和参数设置
- Mysql 参数化查询 in 和 like
- REST API设计优秀实践之参数与查询的使用
- MySQL体系结构与参数文件及查询优化器详解
- ThinkPHP 5.1.25 发布,改进查询参数绑定和浮点型支持
- ThinkPHP 5.1.25 发布,改进查询参数绑定和浮点型支持
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
PHP高级程序设计
Kevin McArthur / 汪泳 等 / 人民邮电出版社出版 / 2009.7 / 45.00元
今天,PHP已经是无可争议的Web开发主流语言。PHP 5以后,它的面向对象特性也足以与Java和C#相抗衡。然而,讲述PHP高级特性的资料一直缺乏,大大影响了PHP语言的深入应用。 本书填补了这一空白。它专门针对有一定经验的PHP程序员,详细讲解了对他们最为重要的主题:高级面向对象、设计模式、文档、测试和标准PHP库等内容。同时,为适应目前Web开发的新趋势,作者还全面探讨了MVC架构和Z......一起来看看 《PHP高级程序设计》 这本书的介绍吧!