C# 在MySQL中使用存储过程删除数据
我在删除数据库中的数据时遇到问题。当我单击“删除”按钮时,弹出一个错误,提示无法将“System.Windows.Forms.TextBox”类型转换为“System.IConvertible”。有人能解决这个问题吗?这是我第一次遇到这个错误 下面的代码正在调用数据库中的存储过程 这是我使用的代码C# 在MySQL中使用存储过程删除数据,c#,mysql,C#,Mysql,我在删除数据库中的数据时遇到问题。当我单击“删除”按钮时,弹出一个错误,提示无法将“System.Windows.Forms.TextBox”类型转换为“System.IConvertible”。有人能解决这个问题吗?这是我第一次遇到这个错误 下面的代码正在调用数据库中的存储过程 这是我使用的代码 private void button3_Click(object sender, EventArgs e) { MySqlParameter[] pms = new MyS
private void button3_Click(object sender, EventArgs e)
{
MySqlParameter[] pms = new MySqlParameter[1];
pms[0] = new MySqlParameter("uProdNo", MySqlDbType.Int32);
pms[0].Value = ProdNo;
MySqlCommand command = new MySqlCommand();
command.Connection = conString;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "pos_delete";
command.Parameters.AddRange(pms);
conString.Open();
if (command.ExecuteNonQuery() == 1)
{
dt.Rows.Clear();
MessageBox.Show("Deleted");
}
else
{
MessageBox.Show("Sorry nothing to be deleted");
}
conString.Close();
}
如何将文本框指定给整数参数
提供as
Convert.ToInt32(ProdNo.Text)
您应该使用converterConvert.ToInt32(ProdNo.Text)
如果不工作,请使用此Convert.ToDecimal(ProdNo.Text)
所以,最终的代码是
private void button3_Click(object sender, EventArgs e)
{
MySqlParameter[] pms = new MySqlParameter[1];
pms[0] = new MySqlParameter("uProdNo", MySqlDbType.Int32);
pms[0].Value = Convert.ToInt32(ProdNo.Text);
MySqlCommand command = new MySqlCommand();
command.Connection = conString;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "pos_delete";
command.Parameters.AddRange(pms);
conString.Open();
if (command.ExecuteNonQuery() == 1)
{
dt.Rows.Clear();
MessageBox.Show("Deleted");
}
else
{
MessageBox.Show("Sorry nothing to be deleted");
}
conString.Close();
}
可能是打字错误:应该是
ProdNo.Text
,转换为number@dlatikay非常感谢。