将读取器项存储在数组中

时间:2012-10-30 07:08:32

标签: vb.net

Dim myReader As OleDbDataReader
Dim Index As Integer
Dim status As Array
Index = 0
cmd.CommandText = "SELECT CPALLOCATIONTIME from RECORDMASTER where ID='" & TxtID.Text & "'"
cmd.CommandType = CommandType.Text
myReader = cmd.ExecuteReader()

Do While myReader.Read()
     status(Index) = myReader.Item(0)
     Index = Index + 1
Loop

myReader.Close()

If (Index = 2) Then
If ((status(0) = "Fp" Or status(0) = "Op") And status(1) = "OXp") Then
    qText = TxtSTS.Text + "X"
    Update = True
    ApplicationStatus = 2
ElseIf ((status(0) = "Fp" Or status(0) = "Op") And status(1) = "FXp") Then
    qText = TxtSTS.Text + "X"
    Update = True
    ApplicationStatus = 2
End If

有人可以帮助我status(Index) = myReader.Item(0),给出转化错误

3 个答案:

答案 0 :(得分:1)

  1. 您希望在添加元素时,您的阵列成长。这不是阵列的用途。请改用List(Of T)。 (有关确切语法,请参阅examples on MSDN。)

  2. 确保从阅读器读取的数据具有正确的数据类型。你有两种方法:

    1. 投了它(例如DirectCast(myReader(0), String))或
    2. (更好)使用已经返回正确数据类型的reader方法,例如myReader.GetString(0)

答案 1 :(得分:0)

尝试

status(Index) = myReader.Item(0).ToString

答案 2 :(得分:0)

Redim status(0) ' < -- declare like this

然后在循环

Redim preserve status(index)
status(Index) = myReader("Column_name")
index = index + 1