ASP.NET ODBC查询与参数 [英] ASP.NET ODBC Query with parameters
问题描述
请帮帮我,我不知道有什么可以是错误的下面的code:
OdbcConnection康恩=新OdbcConnection(CONNSTRING);
查询字符串=INSERT INTO客户(客户ID,CUSTNAME,custPass,+
custEmail,custAddress,custAge)VALUES(+
@ID,@Name,@Pass,@Email,@address,@Age); exe文件的OdbcCommand =新的OdbcCommand(查询,康涅狄格州);
exe.Parameters.Add(@ ID,OdbcType.UniqueIdentifier).value的= ID;
exe.Parameters.Add(@名,OdbcType.VarChar).value的=名称;
exe.Parameters.Add(@通行证,OdbcType.VarChar).value的=通过;
exe.Parameters.Add(@电子邮件,OdbcType.VarChar).value的=电子邮件;
exe.Parameters.Add(@地址,OdbcType.VarChar).value的=地址;
exe.Parameters.Add(@时代,OdbcType.Int).value的年龄=;
conn.Open();
exe.ExecuteNonQuery(); //错误[07002] [微软] [ODBC Microsoft Access驱动程序]参数太少。预计6。
这code,当我试图执行查询抛出我参数太少。
错误。该数据库是好的,它工作正常,当我辛苦code值到一个查询,而不是使用参数。
感谢您。
从MSDN:
当CommandType设置为文本时,.NET Framework数据提供ODBC不支持将命名参数到一个SQL语句或由一个名为的OdbcCommand的存储过程。在这两种情况下,使用问号(?)占位符。例如:
块引用>SELECT * FROM客户WHERE客户id =?
重写查询以
OdbcConnection康恩=新OdbcConnection(CONNSTRING);
查询字符串=INSERT INTO客户(客户ID,CUSTNAME,custPass,+
custEmail,custAddress,custAge)VALUES(+
,,,,,)??????参数的顺序计数!
编辑:参数可以添加这种方式:
的OdbcCommand exe文件=新的OdbcCommand(查询,康涅狄格州);
exe.Parameters.Add(ID,OdbcType.UniqueIdentifier).value的= ID;
exe.Parameters.Add(名,OdbcType.VarChar).value的=名称;
exe.Parameters.Add(通行证,OdbcType.VarChar).value的=通过;
exe.Parameters.Add(电子邮件,OdbcType.VarChar).value的=电子邮件;
exe.Parameters.Add(地址,OdbcType.VarChar).value的=地址;
exe.Parameters.Add(时代,OdbcType.Int).value的年龄=;Please help me, I don't know what can be wrong with the following code:
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.
This code throws me
Too few parameters.
error when I am trying to execute query. The database is fine, it works fine when I hardcode values into a query, instead of using parameters.Thank you.
解决方案From 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 = ?
Rewrite your query to
OdbcConnection conn = new OdbcConnection(connString); String query = "INSERT INTO customer (custId, custName, custPass, "+ "custEmail, custAddress, custAge) VALUES (" + "?, ?, ?, ?, ?, ?)";
Order of Parameter counts!
EDIT: Parameter can be added this way:
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;
这篇关于ASP.NET ODBC查询与参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!