【问题标题】:An item with the same key has already been added error even when i clear即使我清除了具有相同密钥的项目也已添加错误
【发布时间】:2017-05-06 06:00:02
【问题描述】:

我在 FormMain_load 子程序中有这行代码。

在第一次加载时,程序运行良好。然后我尝试打开另一个表单并再次调用 FormMain。这次,表单显示错误,已添加具有相同键的项目。我试图在 sub 的第一行清除字典,但它仍然显示相同的错误。然后我也尝试在重新添加之前清除字典,但仍然失败。奇怪的是,在错误发生之前,消息框显示字典计数为 0。

    query = "SELECT * FROM transaction WHERE isPaid=0"
    If myConn.State = ConnectionState.Closed Then
        myConn.Open()
    End If
    If myCommand Is Nothing Then
        myCommand = New MySqlCommand(query, myConn)
    Else
        myCommand.CommandText = query
    End If
    myDataReader = myCommand.ExecuteReader
    If myDataReader.HasRows Then
        While myDataReader.Read()
            transactionUnpaid.Enqueue(myDataReader("transaction_id"))
        End While
        myDataReader.Close()
        roomList.Clear()
        MsgBox(roomList.Count())
        For Each a As Integer In transactionUnpaid
            query = "SELECT * FROM room_ol WHERE transaction_id=" & a
            If myConn.State = ConnectionState.Closed Then
                myConn.Open()
            End If
            If myCommand Is Nothing Then
                myCommand = New MySqlCommand(query, myConn)
            Else
                myCommand.CommandText = query
            End If
            myDataReader = myCommand.ExecuteReader
            myDataReader.Read()
            roomList.Add(myDataReader("room_id"), myDataReader("transaction_id"))
            myDataReader.Close()
        Next
    End If
    If Not myDataReader.IsClosed Then
        myDataReader.Close()
    End If

【问题讨论】:

    标签: vb.net winforms


    【解决方案1】:

    感谢FabioJensBugs。所以问题是我没有清除 transactionUnpaid Queue,当我再次调用 FormMain 时,它会将一些重复的项目排入队列(队列中允许这样做),并且它多次插入了一些键,因此出现了错误。

    【讨论】:

      猜你喜欢
      • 2017-01-02
      • 2017-08-02
      • 1970-01-01
      • 2011-01-27
      • 2018-10-03
      • 1970-01-01
      • 2012-05-17
      相关资源
      最近更新 更多