mekar10
mekar10

Reputation: 651

Read An Excel File With OLEDB?

Hi I am reading an excel file with oledb(The file has 100000 rows). I must read file quickly.

string conn;

                conn = ("Provider=Microsoft.ACE.OLEDB.12.0;" +
                ("Data Source=" + _filename + ";" +
                "Extended Properties=\"Excel 12.0;\""));
                OleDbConnection oleDBCon = new OleDbConnection(conn);
                oleDBCon.Open();
                DataTable dt = oleDBCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                string excelsheetname = dt.Rows[0].ItemArray[2].ToString();
                string SSQL = "SELECT * from [" + excelsheetname + "]";

                OleDbDataAdapter oleDA = new OleDbDataAdapter(SSQL, conn);
                DataSet ds = new DataSet();
                oleDA.Fill(ds);
                DataTable _DtTable = ds.Tables[0]; // or [ ds ]
                oleDBCon.Close();

and then in _DtTable with a for loop I am inserting these cells to DB.. How can I read this very large excel quickly? And insert to DB? I used Parallel.For but it is not true solution for me.. Any idea?

Upvotes: 1

Views: 6719

Answers (3)

Henrik Gering
Henrik Gering

Reputation: 1871

You could look at some of the ways the database can consume Excelfiles

Upvotes: 1

Digvijay
Digvijay

Reputation: 361

To add records to MyTable using ADO, you can use code similar to the following:

'Create a new connection object for Book1.xls
Dim conn As New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  "Data Source=C:\Book1.xls;Extended Properties=Excel 8.0;"
conn.Execute "Insert into MyTable (FirstName, LastName)" & _
  " values ('Bill', 'Brown')"
conn.Execute "Insert into MyTable (FirstName, LastName)" & _
  " values ('Joe', 'Thomas')"
conn.Close

This is from MSDN: http://support.microsoft.com/kb/247412

Upvotes: 1

Pranay Rana
Pranay Rana

Reputation: 176886

Make use of OpenXML of SQL to insert data in bulk which do fater work for you

here is code did by me for same work : Bulk Insertion of Data Using C# DataTable and SQL server OpenXML function

Upvotes: 1

Related Questions