从 dataGridView INTO 数据库中插入列值 table
INSERT column Values from dataGridView INTO database table
如下所示,我有一个 dataGridView 与来自数据库的 tb2 table 绑定,
如何将(res 列)数据插入 tb table?
预期结果
如果您将 access 用作数据库
保存 datagridview 的前 3 个单元格上面的查询仅适用于 3 及其倍数但是这样我可以
OleDbCommand command = new OleDbCommand (@"insert into tb
(hb,urea,ca)
Values
(@hb,@urea,ca)", database.con);
int RowInsert = dataGridView1.RowCount / 3;
int i = 0;
int control =1;
while (true)
{
command.Parameters.AddWithValue("@hb", $"{dataGridView1.Rows[i].Cells[2].Value}");
command.Parameters.AddWithValue("@urea", $"{dataGridView1.Rows[i+1].Cells[2].Value}");
command.Parameters.AddWithValue("@ca", $"{dataGridView1.Rows[i+2].Cells[2].Value}");
command.ExecuteNonQuery();
if (RowInsert==control)
{
break;
}
i += 3;
control++;
}
MessageBox.Show("Success", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
如下所示,我有一个 dataGridView 与来自数据库的 tb2 table 绑定,
如何将(res 列)数据插入 tb table?
预期结果
如果您将 access 用作数据库
保存 datagridview 的前 3 个单元格上面的查询仅适用于 3 及其倍数但是这样我可以
OleDbCommand command = new OleDbCommand (@"insert into tb
(hb,urea,ca)
Values
(@hb,@urea,ca)", database.con);
int RowInsert = dataGridView1.RowCount / 3;
int i = 0;
int control =1;
while (true)
{
command.Parameters.AddWithValue("@hb", $"{dataGridView1.Rows[i].Cells[2].Value}");
command.Parameters.AddWithValue("@urea", $"{dataGridView1.Rows[i+1].Cells[2].Value}");
command.Parameters.AddWithValue("@ca", $"{dataGridView1.Rows[i+2].Cells[2].Value}");
command.ExecuteNonQuery();
if (RowInsert==control)
{
break;
}
i += 3;
control++;
}
MessageBox.Show("Success", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);