C# MySQL ODBC参数问题

C# MySQL ODBC参数问题,c#,mysql,parameters,null,odbc,C#,Mysql,Parameters,Null,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是否确实得到了一个值。如果我给你一个答复,就在 command.Parameters.Add("@Co

好的,这个函数有一个奇怪的错误。 它说:

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是否确实得到了一个值。如果我给你一个答复,就在

command.Parameters.Add("@CommentNumber", commentnumber);"
第1行返回1(这是正确的)

publicstringcommenter(stringcommentnumber、stringpostnumber、stringcommentname、stringcommentemail、stringcomment){
OdbcConnection=新的OdbcConnection();
connection.ConnectionString=“服务器=;”
+“数据库=;”
+“uid=;”
+“密码=;”
+“驱动程序={MySQL ODBC 5.1驱动程序}”;
string CommandText=“插入注释(CommentNumber、PostNumber、Name、Email、PostTime、Comment)值(@CommentNumber、@PostNumber、@Name、@Email、@PostTime、@Comment)”;
connection.Open();
试一试{
OdbcCommand=newodbccommand(CommandText,connection);
command.Parameters.Add(“@CommentNumber”,CommentNumber);
command.Parameters.Add(“@PostNumber”,PostNumber);
command.Parameters.Add(“@Name”,commentname);
command.Parameters.Add(“@Email”,commentemail);
添加(“@PostTime”,DateTime.Now);
command.Parameters.Add(“@Comment”,Comment);
command.ExecuteNonQuery();
command.Dispose();
command=null;
}
捕获(例外情况除外){
//回复。写下(“出现问题,请联系技术支持。”);
抛出新异常(例如ToString(),ex);
Response.End();
}
最后{
connection.Close();
}
返回“成功!”;
}

字段编号是什么类型的?。
指定要添加命令的参数的类型。参数第二个参数是
OdbcType
,而不是值。尝试使用
.AddWithValue()

ODBC使用
作为持卡人。由于您在原始sql字符串中使用了
@CommandNumber
,因此MySQL实际上将其解释为未定义的服务器端变量,因此出现了“不能为null”错误

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!";

}