如何在不使用存储过程等的情况下通过Ado.net从模式中查找表名?

时间:2022-11-29 02:16:39

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

我试图使用datatable从excel文件中读取数据。

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

命令“select * from [Sheet1 $]”工作正常但如果excel文件具有不同名称的工作表,则会出错。

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

所以现在我需要知道如何使用ADO.Net找到模式中可用的表名。

1 个解决方案

#1


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

以下函数将返回给定位置的表名(例如,excel表)或返回空白

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

#1


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

以下函数将返回给定位置的表名(例如,excel表)或返回空白

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