mysql odbc参数,MySQL ODBC参数问题

Okay, so I'm getting a weird error with this function.

It's saying:

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

But I can verify that the variable commentnumber is indeed getting a value. If I put a Response.Write right before the

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

line I get 1 returned (which is correct).

public string commenter(string commentnumber, string postnumber, string commentname, string commentemail, string comment) {

OdbcConnection connection = new OdbcConnection();

connection.ConnectionString = "server=

;"

+ "database=;"

+ "uid=;"

+ "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 uses ? for placholders. Since you're using @CommandNumber in the raw sql string, it's actually being interpreted by MySQL as an undefined server-side variable, hence the "cannot be null" error.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值