Solved

Reading Data From Excel to SQL in C#

Posted on 2011-03-07
3
394 Views
Last Modified: 2013-12-17
Hi,

i am trying to read Data from an Excel sheet into a SQL Database.
The attached code works, except for the Selection string. It does not allow me to use the "Print Area" as a selection source.

Does anybody know how i can use the Print Area as a selection source?

Thanks
OleDbConnection DBConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + @"c:/temp/lpa.xls" + ";" 
            + "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"");
        DBConnection.Open();

        string SQLString = @"Select * FROM [RR Door LH$.Print_Area]";
        OleDbCommand DBCommand = new OleDbCommand(SQLString, DBConnection);
        IDataReader DBReader = DBCommand.ExecuteReader();

        using (IDataReader DBReader = DBCommand.ExecuteReader())
        {
            string SQLCOnnectionString = @"Data Source=localhost\SQLEXPRESS; Initial Catalog=Test; Integrated Security=True";

            using (SqlBulkCopy bulkcopy = new SqlBulkCopy(SQLCOnnectionString))
            {
                bulkcopy.DestinationTableName = "ExcelData";
                bulkcopy.WriteToServer(DBReader);
            }
            DBReader.Close();
        }

        GridView1.DataSource = DBReader;
        GridView1.DataBind();

        DBReader.Close();
        DBConnection.Close();

Open in new window

0
Comment
Question by:ktpoitm
3 Comments
 
LVL 13

Expert Comment

by:agarwalrahul
Comment Utility
0
 
LVL 1

Author Comment

by:ktpoitm
Comment Utility
Thanks agarwalrahul,

i looked at the links you posted and it is not really addressing the issue.
Both links use the the following select statement:

"select * from [Sheet1$]", oconn"

This select statement will select everything in sheet one. I tried it, but it takes a long time for the query. I would like to just query the Print Area instead of the whole sheet.
0
 
LVL 3

Accepted Solution

by:
Sebastian_OH earned 500 total points
Comment Utility
You can use the following select query:

"Select * FROM [Sheet1$Print_Area]"

That should do the trick
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now