[英]Input string was not in a correct format. C# error SQL database
我有一个非常大的问题。 我想在C#中从数据库sql中删除一行。 这是我的代码:
int x = Convert.ToInt32(dataGridView1.SelectedCells[0].Value);
cmd.Parameters.Clear();
cmd.CommandText = "delete from Table2 where Name=@N";
cmd.Parameters.AddWithValue("@N", x);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
最后我遇到了问题输入字符串的格式不正确。 帮我。 我在第一行中得到错误。
第一个问题:
dataGridView1.SelectedCells[0].Value
不是有效的整数值,因此Convert.ToInt32
失败。
你的意思是
string x = dataGridView1.SelectedCells[0].Value;
代替?
第二个问题:
您正在将“ Name
字段与整数值进行比较。 我假设Name is a _string_ value in the database (otherwise it's poorly named). When SQL looks for matching values, it will try to convert every value of
Name is a _string_ value in the database (otherwise it's poorly named). When SQL looks for matching values, it will try to convert every value of
in the database to a number. if ANY value in the
Name is a _string_ value in the database (otherwise it's poorly named). When SQL looks for matching values, it will try to convert every value of
Name的Name is a _string_ value in the database (otherwise it's poorly named). When SQL looks for matching values, it will try to convert every value of
in the database to a number. if ANY value in the
in the database to a number. if ANY value in the
名称”字段in the database to a number. if ANY value in the
不是有效数字,则查询将失败。
我读过null或空字符串将返回0(待确认)
这是我的猜测:
根据您所在国家/地区,应写上数字:
1.234
要么
1,234
解
轻松的解决方案:您只需执行以下操作即可:
Convert.ToInt32(dataGridView1.SelectedCells[0].Value.Replace(".",","));
好的解决方案:或使用Convert.ToInt32(String,IFormatProvider):
Convert.ToInt32(dataGridView1.SelectedCells[0].Value,CultureInfo.CurrentCulture)
编辑-1而不加评论,我很乐意提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.