c# - MySQL 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 确实获得了一个值。如果我在

之前放置一个 Response.Write
command.Parameters.Add("@CommentNumber", commentnumber);"

行我得到 1 返回(这是正确的)。

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

}

最佳答案

ODBC 使用 ? 作为占位符。由于您在原始 sql 字符串中使用 @CommandNumber,它实际上被 MySQL 解释为未定义的服务器端变量,因此出现“不能为空”错误。

关于c# - MySQL ODBC参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8069539/

相关文章:

c# - 使用 Entity Framework 时出现数据库错误

c# - 使用 RedirectToAction 传递 View 模型

c# - Saxon XSLT 传递参数

html 传递一个链接并在链接内获取参数

mysql - 打开一条记录,更新它并在 MySQL 中作为新记录插入

sql - 无法识别函数中的参数

c# - 如何在 Linq 中使用 Sum()?

c# - 如何将子类属性映射到每个层次结构的表中的列?

php - 检查特定 id 的行是否出现在表格的前面

mysql - 从mysql表中删除重复行(稍微复杂)