简体   繁体   中英

How to save DataGridView multiple row data in to MySQL database in c#

I have multiple row data in a single column. I need to save all data in to a MySQL DB. But it's only saving selected rows data only in DataGridView.

Below is my sample code.

private void button1_Click(object sender, EventArgs e)
{
    string price = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();

    string Query = "INSERT INTO db1.table1 (price) VALUES ('"+ price +"');";


    MySqlConnection myConn = new MySqlConnection(MySQLConn);
    MySqlCommand MySQLcmd = new MySqlCommand(Query, myConn);
    MySqlDataReader myReader;
    try
    {
        myConn.Open();
        myReader = MySQLcmd.ExecuteReader();
        while (myReader.Read())
        {

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

Appreciate any help 🙂 Thank you!

One way is to use Foreach loop to get all rows value one by one in DataGridView and then insert them.

foreach (DataGridViewRow row in dataGridView1.Rows)                   
{ 
    string constring = "Connection String";
    using (MySqlConnection con = new MySqlConnection(constring))
    {
        using (MySqlCommand cmd = new MySqlCommand("INSERT INTO db1.table1 (price) VALUES (@price", con))
        {
            cmd.Parameters.AddWithValue("@price", row.Cells["ColumnName"].Value);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

If you want to save all rows from your Gridview then loop through it and pick up the column value to save.

Also if you want to save/update to the database, you should use ExecuteNonQuery . Finally, dispose of the objects you're creating and the reason for using .

using (MySqlConnection myConn = new MySqlConnection(MySQLConn))
{
    myConn.Open();

    MySqlCommand MySQLcmd = new MySqlCommand(Query, myConn);
    MySqlParameter priceParameter = new MySqlParameter("@price");
    MySQLcmd.Parameters.Add(priceParameter);

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
       var price = row.Cells["PRICE_COLUMN_NAME"].Value;

       MySQLcmd.Parameters["@price"].Value = price;

       MySQLcmd.ExecuteNonQuery();
     }
}

Dear mbharanidharan88 and user3501749 , Thanks for quick support. With your sup I fond a new code. Below is my full working code (for me).

private void button1_Click(object sender, EventArgs e)
    {
       try
        {
            string MySQLConn = "datasource=localhost;port=3306;username=root;password=root;";
            MySqlConnection myConn = new MySqlConnection(MySQLConn);
            myConn.Open();

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string price = dataGridView1.Rows[i].Cells[3].Value.ToString();
                string Query = "INSERT INTO db1.table1 (price) VALUES ('"+ price + "');";
                MySqlCommand MySQLcmd = new MySqlCommand(Query, myConn);
                MySQLcmd.ExecuteNonQuery();

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

If anything wrong let me know 🙂

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM