繁体   English   中英

在VB.NET中的对象中放置多个行值

[英]Put multiple row values in object in VB.NET

我有一个存储过程,该过程通常会返回多行,例如:

ID     Type    Price
1234    A      2.260
1234    B      2.690
1234    C      2.990
1234    D      2.690
1234    D      2.790
1234    D      2.650
1234    D      2.680

我想输出每种类型的最新值。 在我的数据读取器中,我有:

While dr.Read
    result.price.TypeA= dr("price")
    result.price.TypeB= dr("price")
    result.price.TypeC= dr("price")
    result.price.TypeD= dr("price")
End While

和我的查询看起来像:

select  sm_id,
        type,
        price
from    ** WITH (NOLOCK)
where   id = @id
order by id desc

我不确定如何将所有结果存储到对象中,以便可以在前端访问它们。

这可能不是最高效的查询,但它将按照您的描述来完成

select t1.id, t1.t, t1.maxDt, t2.price
from 
    (select id, [TYPE] t, MAX(ud) maxDt 
     from dbo.a it
     where id =1 
     group by id, [type]) t1 
         left join
     dbo.a t2 on t1.id = t2.id and t1.t = t2.[type] and t1.maxDt = t2.ud

现在,将结果存储在对象中并显示

Public Class MyClass
    Public Property Id As Integer
    Public Property [Type] As String
    Public Property [Date] As DateTime
    Public Property Price As Decimal
End Class

. . . . . 

Dim myList As New List(Of MyClass)()
While dr.Read()
    Dim item As New MyClass()
    item.Id = dr("id")
    item.[Type] = dr("t")
    item.[Date] = dr("maxDt")
    item.Price = dr("price")
    myList.Add(item)
End While

myDataGrid.DataSource = myList

这是您大约需要的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM