如何使用foreach将datagridview记录插入mysql数据库

时间:2015-12-05 09:23:33

标签: c# mysql .net

此代码运行良好dataGridview中的值也会成功保存,但在数据库中保存输入的值后,还会在tabl中添加空行

    private void save_Click(object sender, EventArgs e)
    {
       foreach (DataGridViewRow row in dataGridView1.Rows)// picks data from dataGridview                
        {

            try   // MySql connection
            {
                string MyConnectionString = "Server=localhost; Database=markcreations; Uid=root; Pwd=admin";
                MySqlConnection connection = new MySqlConnection(MyConnectionString);
                MySqlCommand cmd = new MySqlCommand();
                cmd = connection.CreateCommand();
                cmd.Parameters.AddWithValue("@invoice", row.Cells["Invoice"].Value);
                cmd.Parameters.AddWithValue("@jobOrder", row.Cells["jobOrder"].Value);
                cmd.Parameters.AddWithValue("@dateTime", row.Cells["Date"].Value);
                cmd.Parameters.AddWithValue("@clientCode", row.Cells["Client Code"].Value);
                cmd.Parameters.AddWithValue("@clientName", row.Cells["Client Name"].Value);

                cmd.CommandText = "INSERT INTO record(invoice, jobOrder, dateTime, clientCode, clientName)VALUES(@invoice, @jobOrder, @dateTime, @clientCode, @clientName)";
                connection.Open();
                cmd.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        MessageBox.Show("Records inserted.");
    }

2 个答案:

答案 0 :(得分:1)

看起来你在DataGridView中有一个空行,可能是最后一行,它用于一个新行。该行对每列都有空值,如果你不检查它,它会插入NULL。

您可以使用row.IsNewRow来检查新行。

答案 1 :(得分:0)

  

感谢好友,它有效。

     

这是正确的代码以及row.IsNewRow

    private void button5_Click(object sender, EventArgs e)
    {
        string MyConnectionString = "Server=localhost; Database=markcreations; Uid=root; Pwd=admin";
        MySqlConnection connection = new MySqlConnection(MyConnectionString);

        foreach (DataGridViewRow row in dataGridView1.Rows)                   
        {
         try
            {
                MySqlCommand cmd = new MySqlCommand();
                cmd = connection.CreateCommand();
                if (row.IsNewRow) continue;
                cmd.Parameters.AddWithValue("@invoice", row.Cells["Invoice"].Value);
                cmd.Parameters.AddWithValue("@jobOrder", row.Cells["jobOrder"].Value);
                cmd.Parameters.AddWithValue("@dateTime", row.Cells["Date"].Value);
                cmd.Parameters.AddWithValue("@clientCode", row.Cells["Client Code"].Value);
                cmd.Parameters.AddWithValue("@clientName", row.Cells["Client Name"].Value);
                cmd.Parameters.AddWithValue("@jobName", row.Cells["Job Name"].Value);
                cmd.Parameters.AddWithValue("@flexQuality", row.Cells["Flex Quality"].Value);
                cmd.Parameters.AddWithValue("@sizeLength", row.Cells["Size Length"].Value);
                cmd.Parameters.AddWithValue("@sizeWidth", row.Cells["Size Width"].Value);
                cmd.Parameters.AddWithValue("@rate", row.Cells["Rate"].Value);
                cmd.CommandText = "INSERT INTO record(invoice, jobOrder, dateTime, clientCode, clientName, jobName, flexQuality, sizeLength, sizeWidth, rate)VALUES(@invoice, @jobOrder, @dateTime, @clientCode, @clientName, @jobName, @flexQuality, @sizeLength, @sizeWidth, @rate)";
                connection.Open();
                cmd.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        MessageBox.Show("Records inserted.");
    }