1

在告诉你问题之前,请原谅我的英语不好,我只知道基本的我有一个带有查询的函数并且我想返回它,我正在这样做:

Public Function obtenerDataAjuste(ByVal id As Integer)
     Dim ajuste As New Ajuste
     Dim cmd as New SqlCommand("usp_obtenerDataAjuste",cn.getCN")
     cmd.CommandType = CommandType.StoredProcedure
     cn.getCN().Open()
     Dim dr as SqlDataReader
     dr = cmd.ExecuteReader
     Dim ajustelist As New List(Of Ajuste)
     While dr.Read
         ajuste.pesomix = dr("PesoMix")
         ajuste.pesoprod = dr("PesoProd")
         ajuste.unidad = dr("UNIDAD_EMPAQUE")
     End While
     cn.getCN().Close()
     Return ajustelist.ToArray
End Function

但我认为是错误的,因为我在后面的代码中调用了这个函数:

Dim opd As New OPDAO
    For Each objAjuste In opd.obtenerDataAjuste(CInt(Session("idop")))
        Dim pesomix As Decimal = objAjuste.pesomix
        Dim pesoprod As Decimal = objAjuste.pesoprod
        Dim empaque As Integer = objAjuste.unidad

        'Reajuste unidad ajustado
        Dim unidadajustado As Double = pesomix / pesoprod
        Session("undajustado") = Convert.ToInt32(unidadajustado)

        'Reajuste paquete ajustado
        Dim paqueteajustado As Double = Session("undajustado") / empaque
        Session("pqtajustado") = paqueteajustado

    Next

它对我不起作用,它返回最后一条记录。任何人都可以帮助我吗?谢谢!

4

2 回答 2

2

您没有在列表中添加任何内容。您还需要在循环Ajuste初始化一个新对象。While dr.Read

这就是为什么你现在只看到最后一个值的原因,因为你不断地覆盖你在 while 循环之外Ajuste初始化的对象。

  Dim ajustelist As New List(Of Ajuste)
         While dr.Read
             Dim ajuste As New Ajuste       // <-- add this
             ajuste.pesomix = dr("PesoMix")
             ajuste.pesoprod = dr("PesoProd")
             ajuste.unidad = dr("UNIDAD_EMPAQUE")
             ajustelist.Add(ajuste)         // <-- and this
         End While
于 2012-09-26T17:27:09.207 回答
1

每次通过 For Each 循环,您将覆盖您之前放入 Session("undajustado") 的内容,这将在完成后成为循环中的最后一项。

在能够帮助你之前,我需要知道你想要完成什么。

于 2012-09-26T17:26:45.120 回答