Mak*_*Vi. 7 c# sql asp.net odbc
请帮帮我,我不知道下面的代码有什么问题:
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.
Run Code Online (Sandbox Code Playgroud)
Too few parameters.当我尝试执行查询时,此代码会引发错误.数据库很好,当我将值硬编码到查询中而不是使用参数时,它工作正常.
谢谢.
Art*_*hur 31
来自MSDN:
当CommandType设置为Text时,.NET Framework数据提供程序for ODBC不支持将命名参数传递给SQL语句或OdbcCommand调用的存储过程.在其中任何一种情况下,请使用问号(?)占位符.例如:
SELECT * FROM Customers WHERE CustomerID = ?
Run Code Online (Sandbox Code Playgroud)
将您的查询重写为
OdbcConnection conn = new OdbcConnection(connString);
String query = "INSERT INTO customer (custId, custName, custPass, "+
"custEmail, custAddress, custAge) VALUES (" +
"?, ?, ?, ?, ?, ?)";
Run Code Online (Sandbox Code Playgroud)
参数顺序计数!
编辑:参数可以这样添加:
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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16345 次 |
| 最近记录: |