MySQL的异常不正确的整数值:从asp.net web应用程序“@column”列 [英] mysql exception incorrect integer value : '@column' for column from asp.net web application
问题描述
我试图更新表使用mysql数据库从ASP.NET Web页面创建。虽然插入数据表从网页
个MySqlException:不正确的整数值:'@时代列年龄在第1行的这个例外时
i am trying to update a table created using mysql database from an ASP.NET Web page. While inserting data into table from web page mysqlexception : incorrect integer value :'@Age' for column Age at row 1 this exception occurs.
我尝试不使用身边的列名单引号查询,但在那种情况下不能正常工作 年龄不能为空异常发生的
i tried not using single quotes around column name in query but that too not working in that case Age cannot be null exception is occurring
这里是DML查询code片段我使用:
here is the code snippet for DML query i am using :
MySqlCommand cmd = new MySqlCommand("Insert into personal(PAN,PName,Age)values('@PAN','@PName','@Age')",con);
cmd.Parameters.AddWithValue("@PAN",SqlDbType.VarChar).Value= panBox.Text;
cmd.Parameters.AddWithValue("@PName", SqlDbType.VarChar).Value=nameBox.Text;
cmd.Parameters.AddWithValue("@Age",MySqlDbType.Int32).Value=Convert.ToInt32(ageBox.Text);
cmd.ExecuteNonQuery();
cmd.Dispose();
我是初学者有人可以帮助我的情况?
i am a beginner can someone help me with the situation?
推荐答案
两个错误。
第一招:参数占位符不应该用单引号。否则,他们变成文字字符串,并不再承认
First one: The parameters placeholder shouldn't be enclosed in single quotes. Otherwise they becomes literal strings and are not recognized anymore
第二个:AddWithValue有两个参数。第一个是该参数的名称,第二个是值不数据库类型。
Second one: AddWithValue takes two parameters. The first one is the name of the parameter, the second one is the value not the database type.
MySqlCommand cmd = new MySqlCommand(@"Insert into personal(PAN,PName,Age)
values(@PAN,@PName,@Age)",con);
cmd.Parameters.AddWithValue("@PAN",panBox.Text);
cmd.Parameters.AddWithValue("@PName", nameBox.Text);
cmd.Parameters.AddWithValue("@Age",Convert.ToInt32(ageBox.Text));
cmd.ExecuteNonQuery();
当然,这是如果 ageBox
是空的抛出异常的危险。我不知道你的要求,但如果年龄字段是必须是有效的号码,然后一点错误检查可以添加到prevent平庸的错误。
Of course this is at risk of throwing an exception if the ageBox
is empty. I don't know your requirements but if the Age field is required to be a valid number then a bit of error checking could be added to prevent banal errors.
int ageValue;
if(!Int32.TryParse(ageBox.Text, out ageValue))
{
MessageBox.Show("Please type a valid value for Age!");
return;
}
.... insert code follows...
这篇关于MySQL的异常不正确的整数值:从asp.net web应用程序“@column”列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!