标题表明我试图将查询存储到二维数组中。我一定做错了,因为看起来它只是在(0,0)到(0,5)列中存储数组的最后一行(我猜那只是第一列lol)
在建议我使用列表之前,我的下一步是随机化数组,以便在每次调用时输出不同的内容。那部分我已经想通了,但我仍然磕磕绊绊地对这个读取数组胡说八道。
以下是我目前的情况:
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "C:\Users\Ashleysaurus\Documents\test.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
Dim Str As String
Dim dr As OleDbDataReader
Dim myArray(2, 5) As String
myConnection.Open()
Str = "SELECT Question, Answer1, Answer2, Answer3, Answer4, CorrectAnswer FROM Critters;"
Dim cmd As OleDbCommand = New OleDbCommand(Str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
Dim i As Integer = 0
myArray(i, 0) = CType(dr("Question"), String)
myArray(i, 1) = CType(dr("Answer1"), String)
myArray(i, 2) = CType(dr("Answer2"), String)
myArray(i, 3) = CType(dr("Answer3"), String)
myArray(i, 4) = CType(dr("Answer4"), String)
myArray(i, 5) = CType(dr("CorrectAnswer"), String)
i = i + 1
End While
myConnection.Close()
答案 0 :(得分:1)
你有一个简单的错误。变量i的声明和初始化应该在循环之外
Dim i As Integer = 0
While dr.Read()
....
但是我更喜欢使用像这样的代码的DataTable
Dim dt = new DataTable()
dt.Load(cmd.ExecuteReader)
现在您可以使用DataTable或多或少像二维数组
Dim question as string = dt.Rows(0)(0)
for each row as DataRow in dt.Rows
Console.WriteLine("Question = " & row(0).ToString())
Next