【问题标题】:Storing reader item in array将阅读器项目存储在数组中
【发布时间】:2012-10-30 07:08:31
【问题描述】:
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),在转换时出错

【问题讨论】:

    标签: vb.net


    【解决方案1】:
    1. 您希望数组在添加元素时增长。这不是数组的用途。请改用List(Of T)。 (具体语法请参见examples on MSDN。)

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

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

    【讨论】:

      【解决方案2】:
      Redim status(0) ' < -- declare like this
      

      然后循环

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

      【讨论】:

        【解决方案3】:

        试试

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

        【讨论】:

          猜你喜欢
          • 2014-03-31
          • 1970-01-01
          • 2017-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多