Solved

Reading Data From Excel to SQL in C#

Posted on 2011-03-07
3
410 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
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

Independent Software Vendors: 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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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