繁体   English   中英

从 dataGridView INTO 数据库表中插入列值

[英]INSERT column Values from dataGridView INTO database table

如下所示,我有一个 dataGridView 与数据库中的 tb2 表绑定,

如何将( res 列)数据插入 tb 表?

在此处输入图像描述

预期结果

在此处输入图像描述

如果您将 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);

暂无
暂无

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

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