2010-10-28 78 views
0

我有一個問題填充集合類與數據庫中的值。每次循環訪問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 
+0

的代碼看起來是正確的,是你肯定的是,返回的值是不相同的,並且,你是正確迭代返回的集合? – Oded 2010-10-28 20:46:00

+0

當我逐步瀏覽並將鼠標懸停在正在存儲到_minutes的值上時,我可以看到正在存儲唯一記錄,但是一旦它添加了另一條記錄,第一條記錄將被寫入returnVal – PaulR 2010-10-28 20:48:17

+3

是否有關於您的分鐘類的內容共享?換句話說,你確定它每次都創建一個新的空對象,而不是每次迭代修改同一個對象? – Beth 2010-10-28 20:53:07

回答

0

問題在於Minutes類中的字段是共享的。將有一個該類的所有實例共享的字段實例。從田地移除「共享」:

Private _minutesId As Integer 

Private _minutes As String 

這將存儲值對每個實例分別

0

要確認你得到新的值或相同的值試試這個:

Dim i as Integer 
    i = 0 
    While dr.Read 

     Dim _minutes As New Minutes 
     _minutes.Minutes = dr("minutes") 
     ' _minutes.MinutesId = dr("minutesId") 
     _minutes.MinutesId = i 
     i = i + 1 
     returnVal.Add(_minutes) 
    End While 

此之後,你可以看到,如果MinuteId是不同的。

N.B.在VB6中,您必須在循環結束時將_minutes設置爲Nothing來獲取新實例。但是我不會認爲VB.NET中會出現這種情況。

+0

實際上所有這一切都是讓我的ID是唯一的,在幾分鐘內的內容仍然被覆蓋 – PaulR 2010-10-29 20:21:55

+0

沒有閱讀答案 - 我說使用它來檢查,如果項目獲得列表中的同一對象。 – 2010-10-30 04:45:46