Try
Dim conn As New MySql.Data.MySqlClient.MySqlConnection(myConnectionString)
conn.Open()
dbQuery = "select * from main_table"
myCommand.CommandText = dbQuery
myCommand.Connection = conn
Dim myReader As MySqlDataReader
myReader = myCommand.ExecuteReader()
While myReader.Read()
row_count = row_count + 1
End While
Catch ex As MySql.Data.MySqlClient.MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Close()
End Try
MessageBox.Show(row_count)
conn.Close()
当我运行上面的代码时,它会显示正确的行数。但是,我试图一次将数据放入一个arraylist中。使用下面的代码不会给我一个错误,但它只读取第一行然后停止执行。
Try
Dim conn As New MySql.Data.MySqlClient.MySqlConnection(myConnectionString)
conn.Open()
dbQuery = "select * from main_table"
myCommand.CommandText = dbQuery
myCommand.Connection = conn
Dim myReader As MySqlDataReader
myReader = myCommand.ExecuteReader()
While myReader.Read()
For x As Integer = 0 To myReader.FieldCount
col_array.Add(myReader.GetString(x))
Next
End While
Catch ex As MySql.Data.MySqlClient.MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Close()
End Try
conn.Close()
我不明白为什么它会在读完第一行后停止。
答案 0 :(得分:0)
评论如下:
从0到FieldCount
的迭代将抛出IndexOutOfRangeException
,它将在第一行之后中断。
For x As Integer = 0 To myReader.FieldCount
col_array.Add(myReader.GetString(x))
Next
将其更改为myReader.FieldCount - 1
结束以解决此问题。