Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
235 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 2000 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

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

916 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