我正在创建一个打开SQL阅读器并加载"Strapping Chart“的例程(这将查找与特定容器体积相关的预定义数字)。在表单加载时,我想填充列表,这些数字将不会改变。(VolCnt,音量)。我想根据卷计数来检索相关的卷。
这就是我所拥有的:
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中创建多个值。然而,我不确定实现这一点的正确方法。任何帮助都将不胜感激。
发布于 2015-08-23 02:07:17
将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 Subhttps://stackoverflow.com/questions/32159167
复制相似问题