简体   繁体   English

如何在C#中将DataGridView多行数据保存到MySQL数据库中

[英]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. 我需要将所有数据保存到MySQL数据库中。 But it's only saving selected rows data only in DataGridView. 但这仅将选定的行数据保存在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循环在DataGridView中一一获取所有行的值,然后将其插入。

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. 如果要保存Gridview中的所有行,请遍历它并选择要保存的列值。

Also if you want to save/update to the database, you should use ExecuteNonQuery . 另外,如果要保存/更新到数据库,则应使用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. 尊敬的mbharanidharan88user3501749 :感谢您的快速支持。 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 🙂 如果有什么问题让我知道🙂

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#如何将多行从Datagridview保存到一个数据库 - C# How to Save multiple rows from Datagridview to One Database 如何将数据从DataGridView保存到数据库? C#Winforms - How to save data from a DataGridView into database? c# winforms 如何在C#中使用datagridview将数据保存到数据库 - How to save data to database using datagridview in C# c#将数据从datagridview保存到数据库 - c# save data from datagridview to database 如何将datagridview批量数据保存到sql数据库,而datagridview数据与图像绑定到c#中的SQL数据库 - how to save datagridview bulk data to sql database while the datagridview data binded with images to SQL database in c# C#如何将文本框中的数据插入到datagridview,然后将datagridview数据保存到数据库中? - C# How to Insert data from text boxes to datagridview and then save the datagridview data into database? 如何在ASP.NET C#中将多个gridview行数据保存到数据库 - How to save multiple gridview row data to database in asp.net c# 如何使用 C# 从 SQL Server 数据库中逐行检索数据到 datagridview - How to retrieve data into a datagridview row by row from a SQL Server database using C# c# - 如何为datagridview实现更新按钮并使用dataHandler类(数据访问类)将值保存到数据库 - how to implement update button for datagridview and save values to database using a dataHandler class(data access class) c# 如何使用C#在mysql数据库中搜索数据并在DataGridView中添加搜索数据? - How to search data in mysql database and add the search data in DataGridView using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM