Solved

How would you do a connection string in OLE DB and write a select * from Excel spreadsheet query?

Posted on 2006-06-22
2
226 Views
Last Modified: 2010-04-23
I figured out how to connect to the Excel spreadsheet in my code, but the only thing holding me back is how to write a query string in vb.net with OLE DB.  How would I do a simple select * from (excel spreadsheet) and how would I do a select column1 from (excel spreadsheet)?

0
Comment
Question by:VBBRett
2 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16961941
Select * From [Sheet1$]
Select Column1 From [Sheet1$]

Bob
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 16962338
here is a function that thakes two parameters (1) path to your excel file, 2) is optional - Spreadsheet name. If not specified, uses "Sheet1" by default)reads data from Excel into a DataTable:

Function ReadDataFromExcel(ByVal path As String, Optional ByVal sheet As String = "Sheet1") As System.Data.DataTable
        Dim ds As New DataSet
        Dim da As OleDbDataAdapter
        Dim conn As OleDbConnection
        Try

            conn = New OleDbConnection( _
                  "provider=Microsoft.Jet.OLEDB.4.0; " & _
                  "data source=" & path & "; " & _
                  "Extended Properties=Excel 8.0;")

            da = New OleDbDataAdapter("SELECT * FROM [" & sheet & "$]", conn)

            conn.Open()

            da.Fill(ds)
            ''get the name of the last column
            Dim lastCol As DataColumn
            lastCol = ds.Tables(0).Columns(ds.Tables(0).Columns.Count - 1)
            ''remove the last column from a DataTable
            ds.Tables(0).Columns.Remove(lastCol)

        Catch ex As Exception
            Console.Write(ex.Message)
        Finally
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
        End Try
        ReadDataFromExcel = ds.Tables(0)
    End Function
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

13 Experts available now in Live!

Get 1:1 Help Now