"Invalid attempt to read when no data is present.",但 "hasrows" 为真
"Invalid attempt to read when no data is present.", but "hasrows" is true
我想知道是否有人可以阐明为什么我在这段代码中没有获取数据:
Private Sub RecoverUnsentOrderToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RecoverUnsentOrderToolStripMenuItem.Click
' Find orders for this branch with status = "1" - created but not acked from the server
Dim myxDBReader As SqlDataReader
Dim myxDBcmd As SqlCommand
Dim query As String
query = "select * from orders where branch = @branch and status = 1;"
myxDBCmd = New SqlCommand(query, myDBCnn)
myxDBcmd.Parameters.Add("@branch", SqlDbType.VarChar).Value = BranchCode
myxDBReader = myDBCmd.ExecuteReader
If myxDBReader.HasRows Then
Do While myxDBReader.Read
Stop
Loop
End If
BranchCode
和我的数据库连接是 public 变量。当我 运行 这段代码时,它到达“停止”并停止,但是当我尝试使用结果时,例如在立即 window 中尝试 ? myxdbreader(0).tostring
,我收到“没有数据时无效的读取尝试”异常。当我将鼠标悬停在 myxdbreader
上查看结果时,我得到了一个行列表,但看不到其中的数据。
这是一个相当大的(对我来说,但不是很大)VB 应用程序,它可以毫无问题地执行各种查询和检索数据。该代码是从代码的另一部分复制并粘贴的,它运行良好。数据库连接是单一的,在应用程序启动时打开并根据需要传递。应用程序的另一部分使用相同的数据库连接毫无问题地写入此“订单”table。
我有另一个 toolstripmenu 函数,除了查询之外,它在每个方面都是相同的,在这种情况下它只是
select * from linkstatus where id=1
并且有同样的问题 - 在 do while dbreader.read
循环内停止,因此它显然找到了一行,但不允许我以通常的方式访问数据。
因为到了“Stop”,我知道HasRows
确实是真的,而且好像读到了第一行。我唯一能看到的不同之处在于,这段代码是 运行 来自我今天添加到表单中的菜单,而所有其余代码都是 运行 来自主窗体。
我到处查找该错误消息,似乎是因为人们没有执行“读取”。
这是 vb.net 来自 Visual Studio 2019,访问 SQL Server 2018。
您正在使用 Public
连接对象,正如评论所述,这不是可行的方法。但最重要的是,请注意只有一个 SqlDataReader 可以与一个 SqlConnection 相关联,从而加剧了单个共享连接的问题。
Only one SqlDataReader per associated SqlConnection may be open at a time, and any attempt to open another will fail until the first one is closed. Similarly, while the SqlDataReader is being used, the associated SqlConnection is busy serving it until you call Close.
您可能已经有一个开放的 Reader,这就是您看到另一个数据集的结果的原因。
正如@jmcilhenny、@Jeroen Mostert 和@Jimi 在评论中指出的,其根本原因是使用单个 public 数据库连接。我现在已经更改了代码,以便每次我需要使用专用连接访问它时它都会连接和断开与数据库的连接,现在它可以正常工作。
我想知道是否有人可以阐明为什么我在这段代码中没有获取数据:
Private Sub RecoverUnsentOrderToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RecoverUnsentOrderToolStripMenuItem.Click
' Find orders for this branch with status = "1" - created but not acked from the server
Dim myxDBReader As SqlDataReader
Dim myxDBcmd As SqlCommand
Dim query As String
query = "select * from orders where branch = @branch and status = 1;"
myxDBCmd = New SqlCommand(query, myDBCnn)
myxDBcmd.Parameters.Add("@branch", SqlDbType.VarChar).Value = BranchCode
myxDBReader = myDBCmd.ExecuteReader
If myxDBReader.HasRows Then
Do While myxDBReader.Read
Stop
Loop
End If
BranchCode
和我的数据库连接是 public 变量。当我 运行 这段代码时,它到达“停止”并停止,但是当我尝试使用结果时,例如在立即 window 中尝试 ? myxdbreader(0).tostring
,我收到“没有数据时无效的读取尝试”异常。当我将鼠标悬停在 myxdbreader
上查看结果时,我得到了一个行列表,但看不到其中的数据。
这是一个相当大的(对我来说,但不是很大)VB 应用程序,它可以毫无问题地执行各种查询和检索数据。该代码是从代码的另一部分复制并粘贴的,它运行良好。数据库连接是单一的,在应用程序启动时打开并根据需要传递。应用程序的另一部分使用相同的数据库连接毫无问题地写入此“订单”table。
我有另一个 toolstripmenu 函数,除了查询之外,它在每个方面都是相同的,在这种情况下它只是
select * from linkstatus where id=1
并且有同样的问题 - 在 do while dbreader.read
循环内停止,因此它显然找到了一行,但不允许我以通常的方式访问数据。
因为到了“Stop”,我知道HasRows
确实是真的,而且好像读到了第一行。我唯一能看到的不同之处在于,这段代码是 运行 来自我今天添加到表单中的菜单,而所有其余代码都是 运行 来自主窗体。
我到处查找该错误消息,似乎是因为人们没有执行“读取”。
这是 vb.net 来自 Visual Studio 2019,访问 SQL Server 2018。
您正在使用 Public
连接对象,正如评论所述,这不是可行的方法。但最重要的是,请注意只有一个 SqlDataReader 可以与一个 SqlConnection 相关联,从而加剧了单个共享连接的问题。
Only one SqlDataReader per associated SqlConnection may be open at a time, and any attempt to open another will fail until the first one is closed. Similarly, while the SqlDataReader is being used, the associated SqlConnection is busy serving it until you call Close.
您可能已经有一个开放的 Reader,这就是您看到另一个数据集的结果的原因。
正如@jmcilhenny、@Jeroen Mostert 和@Jimi 在评论中指出的,其根本原因是使用单个 public 数据库连接。我现在已经更改了代码,以便每次我需要使用专用连接访问它时它都会连接和断开与数据库的连接,现在它可以正常工作。