2010-10-28 64 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