我在删除数据库中的数据时遇到问题。当我单击删除按钮时,弹出一个错误,指出无法将“System.Windows.Forms.TextBox”类型转换为“System.IConvertible”类型。任何人都可以解决这个问题吗?这是我第一次遇到这个错误。
下面的代码正在调用我的数据库中的存储过程。
这是我使用的代码
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();
}
最佳答案
您应该使用转换器Convert.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();
}
关于c# - 使用MySQL中的存储过程删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42865536/