[英]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. 尊敬的mbharanidharan88和user3501749 :感谢您的快速支持。 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.