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

时间:2009-02-11 07:54:46

标签: excel ado.net schema

我正在尝试使用datatable从excel文件中读取数据。

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

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

1 个答案:

答案 0 :(得分:0)

以下函数将返回给定位置的表名(例如,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