2

I am trying to insert data from exel files to Microsoft SQL server local db table. I can show a dataGridview with the excel data in a windows form application but I cant figure out how to put the data into a table.

try
{   
    System.Data.OleDb.OleDbConnection cnn;
    System.Data.DataSet DtSet;
    System.Data.OleDb.OleDbDataAdapter cmd;

    cnn = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\\CGData\\user\\Desktop\\Book1.xls';Extended Properties = Excel 8.0");
    cmd = new System.Data.OleDb.OleDbDataAdapter("select * from [sheet1$]", cnn);
    cmd.TableMappings.Add("Table", "TestTable");
    DtSet = new System.Data.DataSet();
    cmd.Fill(DtSet);
    dataGridView1.DataSource = DtSet.Tables[0];
    cnn.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

What Else do I need to do to copy the Data into a localDB Database table?

Rand Random
  • 7,300
  • 10
  • 40
  • 88
asmo dias
  • 37
  • 3

1 Answers1

0

You already have a datatable (DtSet.Tables[0]), all you need is to bulkcopy that datatable to SQL. check this answer: Insert entire DataTable into database at once instead of row by row?

fra
  • 186
  • 1
  • 12