0

我正在尝试使用 vb.net 编写一个 mdb 到 CVS 转换器。我想使用 oledbdata 阅读器获取列标题名称,这是我到目前为止的代码

 Public Cheadernames As Array
    Public firstRead As Boolean = False
    Public columnCount

    Dim connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strfilename
    Dim queryString = "SELECT * FROM products"
    Dim connection As New OleDbConnection(connectionstring)
    Dim command As New OleDbCommand(queryString, connection)
    connection.Open()
    Dim reader As OleDbDataReader = command.ExecuteReader()
    While reader.Read
        If firstRead Then
            columnCount = reader.FieldCount()
            firstRead = False
            For i = 0 To columnCount - 1
                Cheadernames(i) = reader.GetName(i) ' i am getting an error with this line
                Console.WriteLine(Cheadernames(i))
            Next
        End If

所以我试图将所有列标题名称放在一个数组中,但注释行给了我这个错误“对象变量或未设置块变量”

4

1 回答 1

3

您的 Cheadernames 变量是一个空数组,没有元素也没有类型。那是错误的:您几乎不应该将变量或方法参数声明为“As Array”。相反,你想要这样的东西:

Private HeaderHames() As String
Private firstRead As Boolean = True
Private columnCount As Integer

Dim connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strfilename
Dim queryString = "SELECT * FROM products"
Using connection As New OleDbConnection(connectionstring), _ 
      command As New OleDbCommand(queryString, connection)

    connection.Open()
    Using reader As OleDbDataReader = command.ExecuteReader()
        While reader.Read
            If firstRead Then
                columnCount = reader.FieldCount()
                HeaderNames = New String(columnCount-1)
                firstRead = False
                For i = 0 To columnCount - 1
                    HeaderNames(i) = reader.GetName(i) 
                    Console.WriteLine(HeaderNames(i))
                Next
            End If
            '...
         End While
     End Using
 End Using
于 2012-08-08T04:25:33.390 回答