将所有datagridview值插入mysql数据库

时间:2018-03-28 07:44:47

标签: c# mysql sql datagridview sql-insert

我有一个按钮,它将数据添加到我的datagridview,我想将所有添加的数据插入到datagridView到mysql数据库,但它只插入1行数据

这是表格的结构:

        //Construct Columns
        dataGridView2.ColumnCount = 7;
        dataGridView2.Columns[0].Name = "Pig ID";
        dataGridView2.Columns[1].Name = "Weight";
        dataGridView2.Columns[2].Name = "Price";
        dataGridView2.Columns[3].Name = "Flavor";
        dataGridView2.Columns[4].Name = "F-Fee";
        dataGridView2.Columns[5].Name = "Internal";
        dataGridView2.Columns[6].Name = "I-Fee";

这是我插入数据的代码:

string constring = "datasource=localhost;port=3306;username=root";
        string query = "insert into maasinhondb.orderdetails_table (order_id, pig_id, weight, resell_price, fname, fprice, iname, iprice) values ('" + this.txtOID.Text + "',@pig_id,@weight,@resell_price,@fname,@fprice,@iname,@iprice);";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(query, conDataBase);
        MySqlDataReader myReader;

        for (int i = 0; i < dataGridView2.Rows.Count; i++)

            try
            {
                cmdDataBase.Parameters.AddWithValue("@pig_id", dataGridView2.Rows[i].Cells[0].Value);
                cmdDataBase.Parameters.AddWithValue("@weight", dataGridView2.Rows[i].Cells[1].Value);
                cmdDataBase.Parameters.AddWithValue("@resell_price", dataGridView2.Rows[i].Cells[2].Value);
                cmdDataBase.Parameters.AddWithValue("@fname", dataGridView2.Rows[i].Cells[3].Value);
                cmdDataBase.Parameters.AddWithValue("@fprice", dataGridView2.Rows[i].Cells[4].Value);
                cmdDataBase.Parameters.AddWithValue("@iname", dataGridView2.Rows[i].Cells[5].Value);
                cmdDataBase.Parameters.AddWithValue("@iprice", dataGridView2.Rows[i].Cells[6].Value);

                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();
                MessageBox.Show("Information Successfully Saved", "Confirmation Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

                while (myReader.Read())
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

请帮忙,我该怎么办?

2 个答案:

答案 0 :(得分:1)

你可以使用

  

cmdDataBase.Parameters.Clear()

答案 1 :(得分:0)

谢谢大家,我找到了答案:

for (int i = 0; i < dataGridView2.Rows.Count; i++)
        {
            string constring = "datasource=localhost;port=3306;username=root";
            string query = "insert into maasinhondb.orderdetails_table (order_id, pig_id, flavor_id, internal_id) values ('" + this.txtOID.Text + "', @pigid,@flavorid,@internalid);";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand(query, conDataBase);
            MySqlDataReader myReader;

                cmdDataBase.Parameters.AddWithValue("@pigid", dataGridView2.Rows[i].Cells[0].Value);
                cmdDataBase.Parameters.AddWithValue("@flavorid", dataGridView2.Rows[i].Cells[4].Value);
                cmdDataBase.Parameters.AddWithValue("@internalid", dataGridView2.Rows[i].Cells[7].Value);
                //cmdDataBase.Parameters.Clear();

                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();

                while (myReader.Read())
                {

                }
                conDataBase.Close();
        }