c# - 使用MySQL中的存储过程删除数据

标签 c# mysql

我在删除数据库中的数据时遇到问题。当我单击删除按钮时,弹出一个错误,指出无法将“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/

相关文章:

SQL 回退行?

java - 无法将 Java ap 连接到在线 MySQL 数据库 - 错误的 URL

MySql与Delphi直连

c# - WPF 将控件的图像复制到位图

MySQL SELECT 编码

c# - 如何正确测试 NHibernate FetchMode.Eager?

c# - 如果您使用和/或 (||/&&),.net 会停止检查 IF 语句的其余部分吗?

limit - 所有具有 limit1 的表的 MySQL 转储

c# - Web APi OData V4 问题 "The entity ' ' 没有定义键

c# - Entity Framework 在调用 Any 或 Count 后抛出 NotSupportedException