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?

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.