Dim times() As String
For i = 0 To events.GetLength(1)
ReDim Preserve times(i + 1)
SQLQuery("Select event_entry.Time from event, event_entry, swimmer, users where event_entry.eventID = event.EventID and event_entry.SwimmerID = swimmer.SwimmerID and users.userid = swimmer.userid and users.username = '" & CurrentUser & "' and event.stroke = '" & events(0, Math.Max(0, i - 1)) & "' and event.Distance = '" & events(1, Math.Max(0, i - 1)) & "' order by event_entry.time asc limit 1")
While reader.Read()
**times(i) = reader.GetDateTime(0).ToString**
End While
reader.Close()
Next
这会在突出显示的行上抛出IndexOutOfRangeException,我不知道为什么。我之前做过类似的事情并且工作得很好。 正如您所看到的,我已将时间声明为未定义的数组,然后将其大小更改为适合。 在第一次迭代中抛出异常。任何帮助将不胜感激
下面的代码是我做过类似事情的地方,它完美无缺。该代码还显示了读者是什么以及如何声明和使用它。
While reader.Read
ReDim Preserve events(1, count)
events(0, count) = reader.GetString(0)
events(1, count) = reader.GetInt16(1)
count += 1
End While
Public connection As New MySqlConnection("server=*****; user id=root; password=*****; database=******; Convert Zero DateTime = True")
Public CurrentUser As String
Public command As New MySqlCommand
Public Poisition As String
Public reader As MySqlDataReader
Sub SQLQuery(ByVal stm As String)
command.CommandText = stm
reader = command.ExecuteReader
End Sub
更新: 从DATETIME列检索数据时似乎只是一个问题。有谁知道为什么???