将SQL结果保存为Lst?

时间:2011-01-25 15:22:30

标签: .net asp.net vb.net

在我的数据库中,一个用户可以有多个订单。我想获得用户的所有订单并将其保存在列表中。

这就是我现在正在做的事情

Public Function GetUserOrders(ByVal userID As Integer) As DataSet
        Dim sqlDA As New SqlDataAdapter
        Dim ds As New DataSet
        Dim myCommand As New Data.SqlClient.SqlCommand("sp_GetUserOrders", myConnection)
        ' Mark the Command as a SPROC
        myCommand.CommandType = CommandType.StoredProcedure
        Dim muserID As New SqlParameter("@userID", SqlDbType.Int)
        muserID.Value = userID
        myCommand.Parameters.Add(muserID)
        Try
            ' Open the connection and execute the Command
            myConnection.Open()
            sqlDA.SelectCommand = myCommand
            sqlDA.Fill(ds)

        Catch ex As Exception

        Finally
            myConnection.Close()
        End Try

        Return ds
    End Function

但我似乎无法弄清楚如何将结果分配到列表中。

Dim orderList As New ListItemCollection()
orderlist = GetUserOrders()

不起作用,请帮助。

1 个答案:

答案 0 :(得分:0)

我明白了。

我在getuserorders函数中设置了列表。

Dim dr As SqlDataReader
        Dim orderList As New ListItemCollection()
        Try
            ' Execute the command
            myConnection.Open()
            dr = myCommand.ExecuteReader()

            If Not dr.Read() Then
                Return Nothing
            Else
                While dr.Read()
                    orderList .Add(dr("orderCode"))
                End While
            End If
            dr.Close()
        Finally
            myConnection.Close()
        End Try