views:

337

answers:

1

I am trying to read data from excel files using datatable.

The command "select * from [Sheet1$]" works fine but if the excel file has sheet with different name it gives error.

So now I need know how can I find the table names available in a schema using ADO.Net.

A: 

Following Function will return the table name at the given position(eg, excel sheet) or return blank

Private Function GetTableName(ByVal ConnectionString As String, ByVal TableNumber As Integer) As String
    Dim i As Integer
    Dim dtXlsSchema As DataTable
    Dim myConn As New OleDbConnection
    myConn.ConnectionString = ConnectionString
    myConn.Open()
    dtXlsSchema = myConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, _
                      New Object() {Nothing, Nothing, Nothing, "TABLE"})


    If TableNumber > dtXlsSchema.Rows.Count Then
        Return ""
    Else
        Return dtXlsSchema.Rows(TableNumber - 1).Item("Table_Name").ToString
    End If
End Function
Sachin