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 存储到数组中?
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 存储到数组中?
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"
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