2012-07-25 89 views
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存储到数组中?如何将sql查询值存储到数组中

回答

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 

编辑:但是,而不是返回数组,我会返回一个整数的泛型列表(如果你只想要回产品ID)或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是THEI您的标签control.The的d String.Join方法将返回用逗号分隔的ProductId的一串像"1,2,6,7"

+0

如何检索从所有的值列表到label.text? – FredHomme 2012-07-25 15:15:40

+0

'虽然reader.Read() Sum.Text =(读卡器( “产品ID”)) 端,而 循环while reader.NextResult()' – FredHomme 2012-07-25 15:29:37

+0

有一个在循环而 – FredHomme 2012-07-25 15:30:00

相关问题