【问题标题】:'Data reader' closed but error says not“数据阅读器”已关闭,但错误提示未关闭
【发布时间】:2017-06-12 16:35:54
【问题描述】:

程序能够运行。但是当点击功能按钮时,错误说数据读取器没有关闭,我实际上是这样做的。1

有什么解决办法吗?

错误:已经有一个打开的 DataReader 与此命令关联,必须先关闭。

 Else
                    If checkoutdate.Value >= checkindate.Value Then

                        cmdsearch.CommandText = "SELECT * FROM [Reservations] where [ReservationID] = " & reservationidlbl.Text
                        cmdupdate.CommandType = CommandType.Text
                        cmdsearch.Connection = cnnoledb
                        Dim read3 As OleDbDataReader = cmdsearch.ExecuteReader()

                        If checkindate.Value & checkoutdate.Value >= read3(5) & read3(6) Then
                            If read3(5) & read3(6) <= checkindate.Value & checkoutdate.Value Then
                                cmdupdate.CommandText = "INSERT INTO [Reservations] (ReservationID, [RoomNo], CustomerName, [IC/Passport], ContactNo, [CheckIn_Date], [CheckOut_Date], RoomType, Deposit, ReservationDate,[Status]) values ( '" & reservationidlbl.Text & "' , '" & roomtxt.Text & "', '" & nametxt.Text & "', '" & passporttxt.Text & "', '" & contacttxt.Text & "','" & checkindate.Text & "','" & checkoutdate.Text & "','" & roomtype2 & "','" & deposittxt.Text & "','" & DateAndTime.Now.ToString & "', '" & status & "')"
                                cmdupdate.CommandType = CommandType.Text
                                cmdupdate.Connection = cnnoledb
                                cmdupdate.ExecuteNonQuery()

                                MsgBox("Reservation made.")
                            Else
                                MsgBox("This room is reserved for the specified date.")
                            End If
                            read3.Close()
                        Else
                            MsgBox("This room is reserved for the specified date.")
                        End If
                        read3.Close()

【问题讨论】:

  • 如果您不发布有问题的代码,要弄清楚您做错了什么有点困难。
  • End If之后插入这个:If read3.IsClosed = False Then read3.Close()
  • 请添加您收到的准确错误消息。
  • @muffi 我做了,但还是一样。
  • @ChrisDunaway 完成。

标签: database vb.net ms-access reader


【解决方案1】:

很明显,您在某处创建了另一个数据读取器,但忽略了关闭。这说明了为什么您应该始终使用 Using 块来创建此类对象,因为您不能忽略关闭它们,例如

Using myDataReader = myCommand.ExecuteReader()
    'Read data here.
End Using 'Data reader is implicitly closed here.

【讨论】:

    【解决方案2】:

    您对cmdsearchcmdupdate 使用相同的cnnoledb 对象。那不行。在整个应用程序中重复使用一个连接对象并不是一个好习惯。您需要为每次调用数据库创建一个新的连接对象,而只是共享连接字符串。

    此外,MS Access 在保持打开从表中读取的连接的同时插入表是一个非常糟糕的主意。这不是 Sql Server;并发不是 Access 的强项。最好是 1) 遍历整个 DataReader 并保留集合中插入的信息,然后在第一个连接完成时插入它,或者 2) 将所有这些组合起来,而不是一个同时执行 INSERT 和选择(是的,this is possible)。

    最后...字符串连接?真的吗?我以为我们已经过去了。现在每个人都应该知道如何使用参数化查询了。它应该是他们教给你的第一件事,也是你在为自己学习时遇到的第一件事。 真的很糟糕使用字符串连接将值放入这样的查询中......即使对于 Access。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      相关资源
      最近更新 更多