从网格视图向数据库插入多个数据

本文关键字:数据 插入 数据库 网格 视图 | 更新日期: 2023-09-27 18:15:56

我已经有一些数据到网格视图。现在我想在gridview中插入多行,怎么做呢?

如果我执行foreach循环,那么它将计算所有已经存在的行并多次插入数据。相反,我想只进入新行

下面是我的代码
    private void userInsert()
    {
        if (MessageBox.Show("Do you want to add the new  data ?", "Confirm ", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
        {
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            try
            {
                foreach (GridViewRow dRow in userDataGridView.Rows)
                {
                    cmd.CommandText = string.Format("insert into users(first_name,last_name,default_rate,default_location,spi_user_id,nickname) values('{0}','{1}',{2},{3},{4},'{5}')", userDataGridView.CurrentRow.Cells[0].Value.ToString(), userDataGridView.CurrentRow.Cells[1].Value.ToString(), userDataGridView.CurrentRow.Cells[2].Value.ToString(), locationID2ComboBox.SelectedValue, userDataGridView.CurrentRow.Cells[4].Value.ToString(), userDataGridView.CurrentRow.Cells[5].Value.ToString());
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
                MessageBox.Show("Your data has been added successfully ", "Saved info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
                userSelect();
            }
        }
    }

从网格视图向数据库插入多个数据

要么跟踪插入了哪些行(例如可以使用Tag属性完成),要么修改SQL查询以检查数据是否已经存在

if not exists (select top 1 * from users u where u.first_name = '{0}' and u.last_name = '{1}')
   insert into users(first_name,last_name,default_rate,default_location,spi_user_id,nickname) 
   values('{0}','{1}',{2},{3},{4},'{5}'

可能需要更多的参数唯一性检查。我还建议对SqlCommand使用参数。

如果您想使用Tag属性(仅当它是Winforms项目!):

foreach (GridViewRow dRow in userDataGridView.Rows)
{
    var check = dRow.Tag as bool? ?? false; //was row already processed?
    if (check)
    {
        continue;
    }
    cmd.CommandText = string.Format("insert into users(first_name,last_name,default_rate,default_location,spi_user_id,nickname) values('{0}','{1}',{2},{3},{4},'{5}')", userDataGridView.CurrentRow.Cells[0].Value.ToString(), userDataGridView.CurrentRow.Cells[1].Value.ToString(), userDataGridView.CurrentRow.Cells[2].Value.ToString(), locationID2ComboBox.SelectedValue, userDataGridView.CurrentRow.Cells[4].Value.ToString(), userDataGridView.CurrentRow.Cells[5].Value.ToString());
    con.Open();
    cmd.ExecuteNonQuery();
    dRow.Tag = true;
}

什么是用户id,谁生成的?

如果新用户的用户id为0,则它是简单的

foreach (GridViewRow dRow in userDataGridView.Rows)
                {
                  if(userDataGridView.CurrentRow.Cells[<user id index>]==0)
                       {
                        //add into DB 
                        }
                }

希望对您有所帮助

您可以使用此事件添加新记录:

private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
     //e.Row.Cells[0].Value <-- Use 'e.Row' argument to access the new row
}