c# – 带参数的ASP.NET ODBC查询

栏目: ASP.NET · 发布时间: 5年前

内容简介:翻译自: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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Head First jQuery

Head First jQuery

Ryan Benedetti , Ronan Cranley / O'Reilly Media / 2011-9 / USD 39.99

Want to add more interactivity and polish to your websites? Discover how jQuery can help you build complex scripting functionality in just a few lines of code. With Head First jQuery, you'll quickly g......一起来看看 《Head First jQuery》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具