Solved

Reading Data From Excel to SQL in C#

Posted on 2011-03-07
3
397 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
ID: 35064497
0
 
LVL 1

Author Comment

by:ktpoitm
ID: 35070053
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
ID: 35138232
You can use the following select query:

"Select * FROM [Sheet1$Print_Area]"

That should do the trick
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

21 Experts available now in Live!

Get 1:1 Help Now