gpt4 book ai didi

c# - MySQL ODBC参数问题

转载 作者:行者123 更新时间:2023-11-29 05:39:16 27 4
gpt4 key购买 nike

好的,所以我在使用此函数时遇到了一个奇怪的错误。它在说:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com