C# 连接必须有效并打开Mysql

标签 c# mysql

我的代码有问题吗?我已经尝试了其他代码,但问题仍然相同。我已经解决这个错误几个星期了,但我不知道如何解决它。而且我已经尝试了其他一些代码,但问题仍然是相同的。

我想将 dataGrid 中的多行保存到我的数据库中。

这是我用来保存多行的代码

    private void button1_Click(object sender, EventArgs e)
    {

        MySqlConnection conString = new MySqlConnection("datasource = localhost; port = 3306; Initial catalog = dbnewsystem; username = root;password = 1234");
        MySqlCommand command1 = new MySqlCommand("INSERT INTO purchaseorder (orNo, ProdNo, Quantity, total)" +
        "VALUES(@ORNo,@ProductNo,@quantity,@total )", conString);
        command1.Parameters.AddWithValue("@ORNo", dataGridView1.Rows.Count);
        command1.Parameters.AddWithValue("@ProductNo", dataGridView1.Rows.Count);
        command1.Parameters.AddWithValue("@quantity", dataGridView1.Rows.Count);
        command1.Parameters.AddWithValue("@total", textBox6.Text);


        conString.Open();
        command1.ExecuteNonQuery();
        command1.Connection = conString;
        conString.Close();
        command1.CommandType = CommandType.StoredProcedure;
        command1.CommandText = "pos_save";

        if (command1.ExecuteNonQuery() == 1)
        {
            MessageBox.Show("saved");
        }
        else
        {
            MessageBox.Show("Sorry Nothing to be Update");
        }
        conString.Close();

    }

最佳答案

改变顺序

    command1.Connection = conString;
    command1.ExecuteNonQuery();

关于C# 连接必须有效并打开Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42613136/

相关文章:

mysql - Enable_broker 在 mysql 5.1 或 5.6 中不起作用

php - 保护网站的最佳方法和途径

c# - checked 属性为 false 时,单选按钮默认被选中?

c# - 在 .NET Core 中使用 System.IO.Enumeration 自定义目录枚举

MySQL 例程不工作

php - Group-Concat 和 GROUP BY 的问题

MySQL 中值不等于

c# - 使用 ASP.NET MVC 应用程序从 HTTPS 重定向

c# - 使用 Selenium for C# 登录 Facebook

c# - 将字符串拆分为数组,删除空格