将Access数据库中的记录存储在数组中

时间:2013-11-07 03:45:14

标签: asp.net vb.net ms-access-2010

有没有办法从MS Access数据库中获取记录,并将它们存储在一个数组中? 如果更容易的话,我也愿意接受有关如何以另一种方式做到这一点的任何建议。

1 个答案:

答案 0 :(得分:1)

由于数组具有静态大小,我决定使用字符串列表,因为除了主键之外,我的数据库中的所有字段类型都设置为Text。我的代码如下(万一有人关心):

Imports System.Data.OleDb

Dim dbConnection As OleDbConnection
Dim dbCommand As OleDbCommand
Dim dbReader As OleDbDataReader
Dim sqlString As String
Dim path As String = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" & _
        Server.MapPath("database.accdb")
Dim lstRecords As List(Of String)

Sub page_load()
    lstRecords = New List(Of String)
    Try
        dbConnection = New OleDbConnection(path)
        dbConnection.Open()
        sqlString = "SELECT * FROM table"
        dbCommand = New OleDbCommand(sqlString, dbConnection)
        dbReader = dbCommand.ExecuteReader()
        While dbReader.Read()
            lstRecords.Add(dbReader("record1") + "," + dbReader("record2"))
        End While
        dbReader.Close()
    Finally
        dbConnection.Close()
    End Try
End Sub