简体   繁体   中英

How to insert DataGridView data in to table of access database using c#?

My program first import data from excel to datagridview , then create button create a table of datagridview in access database, then insert button insert all the data of datagridview in to that table.

My query is working fine for some excel data but not for some biggest data which has 1000 of rows of data, I want to change my query which insert all type of data into data base, here is my query:

private void Create_Click(object sender, EventArgs e)
    {
        List<string> queries = new List<string>();
       queries.Add("CREATE TABLE " + textBox2.Text + " ( [" + DataGridView1.Columns[0].Name + "] Text, [" + DataGridView1.Columns[1].Name + "] Text, [" + DataGridView1.Columns[2].Name + "] Text, [" + DataGridView1.Columns[3].Name + "] Text, [" + DataGridView1.Columns[4].Name + "] Text, [" + DataGridView1.Columns[5].Name + "] Text, [" + DataGridView1.Columns[6].Name + "] Text)"); 


 private void btn_insert_Click(object sender, EventArgs e)
    {List<string> queries = new List<string>();
        foreach (DataGridViewRow row in DataGridView1.Rows)
        { if (!row.IsNewRow)
            {queries.Add("INSERT Into " + textBox2.Text + " (" + DataGridView1.Columns[0].Name + "," + DataGridView1.Columns[1].Name + "," + DataGridView1.Columns[2].Name + "," + DataGridView1.Columns[3].Name + "," + DataGridView1.Columns[4].Name + "," + DataGridView1.Columns[5].Name + "," + DataGridView1.Columns[6].Name + " ) values ('" + row.Cells[0].Value.ToString() + "','" + row.Cells[1].Value.ToString() + "','" + row.Cells[2].Value.ToString() + "','" + row.Cells[3].Value.ToString() + "','" + row.Cells[4].Value.ToString() + "','" + row.Cells[5].Value.ToString() + "','" + row.Cells[6].Value.ToString() + "')"); }
        }
        (ExecuteBatchUpdate(queries.ToArray()))
List<string> queries = new List<string>();

foreach (DataGridViewRow row in DataGridView1.Rows)
{
    if (!row.IsNewRow)
    {
        queries.Add("Insert Into employee (empid,empname,sal) values ('" + row.Cells[0].Value.ToString() + "','" + row.Cells[1].Value.ToString() + "','" + row.Cells[2].Value.ToString() + "')");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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