我正在创建一个打开SQL阅读器并加载“Strapping Chart”的例程(这将查找与容器的特定容量相关的预定义数字)。在表单加载时,我想填充列表,这些数字不会改变。 (VolCnt,卷)。我想根据volumecount检索相关的卷 这就是我所拥有的:
Public HltVolVals As List(Of HLTVolValChart)
Public Class HLTVolValChart
Public Property VCnt As Double
Public Property Volume As Double
Public Sub New(ByVal n_Vcnt As Double, ByVal n_Volume As Double)
Vcnt = n_Vcnt
Volume = n_Volume
End Sub
End Class
Public Sub PopulateHLTVolVals()
Dim sqlstr As String = "SELECT VCnt, Volume FROM Vol_Strap WHERE Vessel = 'HLT' ORDER BY Volume"
Dim CN As New SqlConnection
Dim Reader As SqlClient.SqlDataReader
Dim SQL As New SqlCommand
CN.ConnectionString = Connectionstr
CN.Open()
SQL.Connection = CN
SQL.CommandText = sqlstr
Reader = SQL.ExecuteReader
While Reader.Read
Dim Vol As Double = CDbl(Reader.GetValue(1))
Dim VCnt As Double = CDbl(Reader.GetValue(0))
Dim NewHLTVolValChartItem As List(Of HLTVolValChart) = New List(Of HLTVolValChart)
NewHLTVolValChartItem.Add(New HLTVolValChart(VCnt, Vol))
End While
Reader.Close()
CN.Close()
CN.Dispose()
End Sub
Public Function ListHLTVals(ByVal CurCnt As Double) As Double
Dim QVol = From _HLTVol In HltVolVals
Where _HLTVol.VCnt >= CurCnt
Select _HLTVol.Volume Take 1
Return QVol.First
End Function
我遇到的问题(我认为)是在循环记录时我没有在HLTVolValChart中创建多个值。但是,我不确定实现这一目标的正确方法。 任何帮助将不胜感激。
答案 0 :(得分:0)
使NewHLTVolValChartItem
成为一个类变量。在循环中不要重新初始化NewHLTVolValChartItem
变量。每次迭代都会创建一个新列表 - 只需删除该部分。
Private NewHLTVolValChartItem As New List(Of HLTVolValChart)
Public Sub PopulateHLTVolVals()
Dim sqlstr As String = "SELECT VCnt, Volume FROM Vol_Strap WHERE Vessel = 'HLT' ORDER BY Volume"
Dim CN As New SqlConnection
Dim Reader As SqlClient.SqlDataReader
Dim SQL As New SqlCommand
CN.ConnectionString = Connectionstr
CN.Open()
SQL.Connection = CN
SQL.CommandText = sqlstr
Reader = SQL.ExecuteReader
While Reader.Read
Dim Vol As Double = CDbl(Reader.GetValue(1))
Dim VCnt As Double = CDbl(Reader.GetValue(0))
NewHLTVolValChartItem.Add(New HLTVolValChart(VCnt, Vol))
End While
Reader.Close()
CN.Close()
CN.Dispose()
End Sub