1

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?

3 Answers 3

1

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

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

2 Comments

I probably didn't read carefully enough. You wish to read from excel and insert into another DB, maybe SQL Server and not insert records in Excel file. To read excel files i prefer to use ExcelDataReader from exceldatareader.codeplex.com
thank you! but on first line it says "Lightweight and fast library written in C# for reading Microsoft Excel files ('97-2007)." I have excel 03/07/10 files.. Does ExcelDataReader support excel 2010 files?
1

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

1 Comment

this is useful solution for insert? I didnt tired it? Are you sure is your code more good performance than from classic for loop on Datatable(like ı write up) ??
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.