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

C# 从网格视图向数据库中插入多个数据,c#,C#,我已经在网格视图中输入了一些数据。现在我想在gridview中插入多行,那么怎么做呢 如果我使用foreach循环,那么它将对已经存在的所有行进行计数,并多次插入数据。相反,我只想进入新行 下面是我的代码 private void userInsert() { if (MessageBox.Show("Do you want to add the new data ?", "Confirm ", MessageBoxButtons.YesNo,MessageBo

我已经在网格视图中输入了一些数据。现在我想在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();
            }
        }
    }

例如,可以使用标记属性跟踪插入的行,或者修改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}'
您可能需要更多的参数检查以获得唯一性。我还建议使用参数和ql命令

如果要仅在标记属性为Winforms project时使用它!:

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
}

请告诉我们你到目前为止做了什么?你的密码!!