2011-11-09 17 views
1

さて、この関数では奇妙なエラーが発生しています。MySQL ODBCパラメータの問題

Exception Details: System.Data.Odbc.OdbcException: ERROR [HY000] [MySQL][ODBC 5.1 Driver][mysqld-5.0.27-community-nt]Column 'CommentNumber' cannot be null 

をしかし、私は、変数commentnumberが実際の値を取得していることを確認することができます それは言っています。私の前にResponse.Writeを置くと、

command.Parameters.Add("@CommentNumber", commentnumber);" 

行が返されます(これは正しい)。

public string commenter(string commentnumber, string postnumber, string commentname, string commentemail, string comment) { 
OdbcConnection connection = new OdbcConnection(); 
connection.ConnectionString = "server=<address>;" 
+ "database=<database>;" 
+ "uid=<username>;" 
+ "password=<password>;" 
+ "DRIVER={MySQL ODBC 5.1 Driver}"; 
string CommandText = "INSERT INTO Comments (CommentNumber, PostNumber, Name, Email, PostTime, Comment) VALUES (@CommentNumber, @PostNumber, @Name, @Email, @PostTime, @Comment)"; 
connection.Open(); 
try { 
    OdbcCommand command = new OdbcCommand(CommandText, connection); 
    command.Parameters.Add("@CommentNumber", commentnumber); 
    command.Parameters.Add("@PostNumber", postnumber); 
    command.Parameters.Add("@Name", commentname); 
    command.Parameters.Add("@Email", commentemail); 
    command.Parameters.Add("@PostTime", DateTime.Now); 
    command.Parameters.Add("@Comment", comment); 
    command.ExecuteNonQuery(); 
    command.Dispose(); 
    command = null; 
} 
catch(Exception ex) { 
//  Response.Write("There's been a problem. Please contact technical support."); 
    throw new Exception(ex.ToString(), ex); 
    Response.End(); 
} 
finally { 
    connection.Close(); 
} 
return "Success!"; 

} 

答えて

2

ODBCはplacholdersのために?を使用しています。生のSQL文字列に@CommandNumberを使用しているので、実際にはMySQLによって定義されていないサーバ側変数として解釈されているため、「nullにはできません」というエラーが発生します。

0

どのようなタイプのフィールドがCommentNumberですか? コマンドを追加するパラメータのタイプを指定します。パラメータ

1

2番目のパラメータは、値ではなくOdbcTypeです。試してみてください.AddWithValue()