我有一個問題填充集合類與數據庫中的值。每次循環訪問WHILE DR.READ循環中的記錄時,最後一個記錄將寫入集合中的所有其他項目。我的returnVal集合有幾個相同的項目,儘管循環顯示每個單獨的記錄被添加到returnVal中。謝謝你的幫助。試圖查詢數據庫
Public Shared Function getStuff(ByVal sb As StringBuilder) As System.Collections.Generic.List(Of Minutes)
Dim returnVal As New System.Collections.Generic.List(Of Minutes)
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
cmd.Connection = conn
cmd.CommandText = sb.ToString
Try
conn.Open()
dr = cmd.ExecuteReader
While dr.Read
Dim _minutes As New Minutes
_minutes.Minutes = dr("minutes")
_minutes.MinutesId = dr("minutesId")
returnVal.Add(_minutes)
End While
Catch ex As Exception
Dim _minutes As New Minutes
_minutes.Minutes = ex.ToString
_minutes.MinutesId = 0
returnVal.Add(_minutes)
End Try
conn.Close()
Return returnVal
End Function
這是我的分鐘級
Imports Microsoft.VisualBasic
Public Class Minutes
Private Shared _minutesId As Integer
Private Shared _minutes As String
Public Property MinutesId() As Integer
Get
Return _minutesId
End Get
Set(ByVal value As Integer)
_minutesId = value
End Set
End Property
Public Property Minutes() As String
Get
Return _minutes
End Get
Set(ByVal value As String)
_minutes = value
End Set
End Property
Public Shared Function getStuff(ByVal sb As StringBuilder) As System.Collections.Generic.List(Of Minutes)
Return MinutesDA.getStuff(sb)
End Function
Public Shared Function modify(ByVal sb As StringBuilder) As String
Return MinutesDA.modify(sb)
End Function
Public Shared Property Id() As Integer
Get
Return MinutesDA.Id
End Get
Set(ByVal value As Integer)
MinutesDA.Id = value
End Set
End Property
Public Shared Property Index() As Integer
Get
Return MinutesDA.Index
End Get
Set(ByVal value As Integer)
MinutesDA.Index = value
End Set
End Property
End Class
的代碼看起來是正確的,是你肯定的是,返回的值是不相同的,並且,你是正確迭代返回的集合? – Oded 2010-10-28 20:46:00
當我逐步瀏覽並將鼠標懸停在正在存儲到_minutes的值上時,我可以看到正在存儲唯一記錄,但是一旦它添加了另一條記錄,第一條記錄將被寫入returnVal – PaulR 2010-10-28 20:48:17
是否有關於您的分鐘類的內容共享?換句話說,你確定它每次都創建一個新的空對象,而不是每次迭代修改同一個對象? – Beth 2010-10-28 20:53:07