1
Dim sql As String = "Select ProductID From OrderDetail Order By ProductID Desc"
    Dim command As New SqlCommand(sql, connection)
    Dim reader1 As SqlDataReader = command.ExecuteReader()

如何将我检索到的所有 productid 存储到数组中?

4

2 回答 2

2
Dim list As New List(Of Integer)

Using reader As SqlDataReader = command .ExecuteReader()
    While reader.Read()
        list.Add(reader.GetInt32(reader.GetOrdinal("ProductID")))
    End While
End Using
'check  list.ToArray() now

编辑:但是,我不会返回一个数组,而是返回一个通用整数列表(如果您只想返回 ProductId)或ProductClass对象列表

Private Function GetProductIDs() As IList(Of Integer)

    Dim list As New List(Of Integer)
    Dim conStr = "write your connection string here"

    Using connection As New SqlConnection(conStr )
        Dim sql As String = "Select ProductID From OrderDetail Order By ProductID Desc"
        Dim command As New SqlCommand(sql, connection)
        Using reader As SqlDataReader = command.ExecuteReader()
            While reader.Read()
                list.Add(reader.GetInt32(reader.GetOrdinal("ProductID")))
            End While
        End Using
    End Using

    Return list

End Function

编辑 2根据评论, 要检索放入标签的文本,您可以这样做

Dim str As String
str = String.Join(",", GetProductIDs())
Label1.Text=str;

假设Label1是您的标签控件的ID。String.Join该方法将返回一个由逗号分隔的ProductId字符串,例如"1,2,6,7"

于 2012-07-25T15:11:23.337 回答
0
SQLdr = SQLCmd.ExecuteReader 'Gets Data

While dr.Read() 'While Data is Present        
      MsgBox(dr("Column Name")) 'Show data in a Message Box
End While

Loop While SQLdr.NextResult() 'Move to the Next Record

http://www.daniweb.com/software-development/vbnet/code/216920/sql-in-vb.net

于 2012-07-25T15:11:40.053 回答