将存储过程结果传递给VB.net中的List

时间:2015-07-06 15:59:29

标签: arrays vb.net list

我有一个返回电子邮件地址,密码和应用ID的当前存储过程。

ALTER PROCEDURE [dbo].[ClientLogin_AppIdPasswordRequest]
    -- Add the parameters for the stored procedure here
    @Email varchar(100)=''
AS
BEGIN
    Select tblApp.EmailAddress, tblApp.Password, tblApp.ID from tblApp 
    where (@Email=EmailAddress)
END

问题是电子邮件可能有多个与之关联的应用ID。我将此信息传递给VB.net的当前方式如下,但只返回第一个应用程序ID。

        Try
            Dim oDR As SqlDataReader
            Dim oCMD As New SqlCommand
            Dim sEmail As String
            Dim sPassword As String
            Dim sID As List(Of String)
            oCMD.Parameters.AddWithValue("@Email", Email)
            oCMD.CommandTimeout = 0

            oDR = ExecReader(oCMD, "ClientLogin_AppIdPasswordRequest")
            If oDR.Read Then
                'Found
                sEmail = CNull(oDR("EmailAddress"))
                sPassword = oDR("Password")

                ' Should populate an array or list but only returns first id
                sID = oDR("ID")

                ' Do stuff here
            End If

            oDR.Close()
            oCMD.Dispose()
        Catch ex As Exception
            ' Catch
        End Try

如何获取与传入的电子邮件相关联的所有应用ID的结果列表?

2 个答案:

答案 0 :(得分:0)

假设您对ExecReader的调用负责处理连接数据库的详细信息并传递正确的参数,那么您可以将代码更改为

Dim dt = New DataTable()
oDR = ExecReader(oCMD, "ClientLogin_AppIdPasswordRequest")
dt.Load(oDR)
If dt.Rows.Count > 0 Then
    'Found

    ' Now you have a DataTable where every DataRow represent one record
    ' retrieved by you procedure
    For Each dr in dt.Rows
       sEmail = CNull(dr("EmailAddress"))
       sPassword = dr("Password")
       sID = dr("ID")
       ' do your stuff with this sets of data,

    Next
End If
' The datatable dt is still available for further processing here

如果您需要将已找到的行保留在内存中,以便在关闭连接后对它们执行进一步操作,则可以使用此方法。相反,如果您在关闭连接之前立即处理所有内容,则可以使用 直接在DataReader

返回的记录上循环
oDR = ExecReader(oCMD, "ClientLogin_AppIdPasswordRequest")
while oDR.Read

   sEmail = CNull(oDR("EmailAddress"))
   sPassword = oDr("Password")
   sID = oDR("ID")

End While

答案 1 :(得分:0)

这是基本的想法:

    If Not oRD Is Nothing Then

        If oRD.HasRows Then

            While oRD.Read

                'your working here

            End While

        End If

    End If