2010-10-28 105 views
5

我有一个SQL查询:SQL查询错误 - “列不能为空”

String S = Editor1.Content.ToString(); 
    Response.Write(S);  
    string sql = "insert into testcase.ishan(nmae,orders) VALUES ('9',@S)"; 
    OdbcCommand cmd = new OdbcCommand(sql, myConn); 
      cmd.Parameters.AddWithValue("@S", S); 
      cmd.ExecuteNonQuery(); 

Error: Column 'orders' cannot be null at System.Data.Odbc.OdbcConnection.HandleError

+3

对我来说似乎不言自明 – 2010-10-28 10:37:00

+0

我已经在字符串中有价值,但它不是geting到sql查询 – Ishan 2010-10-28 10:38:49

+1

odbc - 这是由选择吗? – 2010-10-28 10:40:08

回答

4

manual

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 = ? 

The order in which OdbcParameter objects are added to the OdbcParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.

使用此:

string sql = "insert into testcase.ishan(nmae,orders) VALUES ('9', ?)"; 
OdbcCommand cmd = new OdbcCommand(sql, myConn); 
cmd.Parameters.AddWithValue("you_can_write_anything_here_its_ignored_anyway", S); 
cmd.ExecuteNonQuery(); 
+0

以及如何定义'?'的值。查询将如何知道是什么? – Ishan 2010-10-28 10:46:22

+0

@user:为集合添加一个参数。它会从它添加的顺序中知道:添加的第一个参数是第一个“?”等。它在我复制的引用中。 – Quassnoi 2010-10-28 10:48:23

+0

谢谢SIR!它的工作 – Ishan 2010-10-28 10:53:11

0

这对你很有帮助

cmd.Parameters.Add("@S", OdbcType.Char, S); 
+0

'System.Data.Odbc.OdbcParameterCollection.Add(string,System.Data.Odbc.OdbcType,int)'的最佳重载方法匹配有一些无效参数 – Ishan 2010-10-28 10:50:08