简体   繁体   English

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

[英]Multiple data insertion into database from grid view

I already have some data into the grid view. 我已经有一些数据进入网格视图。 Now I want to insert multiple rows into gridview then how to do it? 现在我想在gridview中插入多行,然后怎么做?

If I go for foreach loop then it will take count of all the rows already there and insert the data multiple times. 如果我进行foreach循环,那么它将对已经存在的所有行进行计数,并多次插入数据。 Instead I want to into new rows only 相反,我只想进入新行

Below is my Code 下面是我的代码

    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();
            }
        }
    }

Either keep track which rows were inserted (may be done using Tag property for example, or modify your SQL query to check if data is already present 跟踪插入了哪些行(例如,可以使用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}'

you may need more argument checks for uniqueness. 您可能需要更多的参数检查以确保唯一性。 Also I suggest using parameters with SqlCommand . 我也建议对SqlCommand使用参数。

If you want use Tag property (only when it is Winforms project!): 如果要使用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;
}

What is user id and who generate it ? 什么是用户ID?谁生成的?

If user id is 0 for new user its simple 如果新用户的用户ID为0,则简单

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

                }

hope this help 希望这个帮助

You can use this event to add new record: 您可以使用此事件添加新记录:

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

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

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