Solved

Reading Data From Excel to SQL in C#

Posted on 2011-03-07
3
405 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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