已打开与此命令关联的DataReader,必须先关闭该命令[英] already an open DataReader associated with this Command which must be closed first

问题描述

你好,

我得到错误:
"已经有一个打开的 DataReader 与此命令关联,必须
先关门"

谢谢
巴特
------------------------------------------
导入 System.Data.sqlclient
导入 System.Data
....
....
sql = "SELECT count(*) FROM pc where lokl=''"&w&"'';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar

sql = "SELECT pcnr FROM pc where lokl=''"&w&"'';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteReader

对于 j = 1 到 recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNonQuery()
下一个

dtreader.Close()
---------------------------------------------------------------

推荐答案

创建另一个用于运行插入的命令对象:

sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = 新的 SqlCommand(sql, oConnection)
comd1.ExecuteNonQuery()
--
伊利亚胡·戈尔丁,
软件开发人员顾问
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"巴特"<b@sdq.dc 写在消息里
新闻:英国**************@TK2MSFTNGP02.phx.gbl...
你好,

我得到错误:
"已经有一个打开的 DataReader 与此命令关联
必须
先关门"

谢谢
巴特
------------------------------------------
导入 System.Data.sqlclient
导入 System.Data
...
...
sql = "SELECT count(*) FROM pc where lokl=''"&w&"'';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar

sql = "SELECT pcnr FROM pc where lokl=''"&w&"'';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteReader

对于 j = 1 到 recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd.ExecuteNonQuery()
下一个

dtreader.Close()
---------------------------------------------------------------



您好,谢谢回复

我这样做了:

sql = "SELECT count(*) FROM pc where lokl=''"&w&"'';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar

sql = "SELECT pcnr FROM pc where lokl=''"&w&"'';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteReader

对于 j = 1 到 recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = 新的 SqlCommand(sql, oConnection)
comd1.ExecuteNonQuery()
下一个
dtreader.Close()

但仍然出现同样的错误:comd1.ExecuteNonQuery()
"伊利亚胡·戈尔丁"<RE**************************@mMvVpPsS.orgschree f in
贝里希特新闻:OB*************@TK2MSFTNGP02.phx.gbl...
为运行插入创建另一个命令对象:

sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = 新的 SqlCommand(sql, oConnection)
comd1.ExecuteNonQuery()
--
伊利亚胡·戈尔丁,
软件开发人员顾问
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


需要重新构造代码,关闭之前的第一个datareader
开始在同一连接上使用另一个.只能有一个请求
当时提供,直到第一个阅读器关闭它不会让任何人
否则运行.

--
伊利亚胡·戈尔丁,
软件开发人员顾问
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"巴特"<b@sdq.dc 写在消息里
新闻:Og**************@TK2MSFTNGP02.phx.gbl...
您好,谢谢回复

我这样做了:

sql = "SELECT count(*) FROM pc where lokl=''"&w&"'';"
comd = New SqlCommand(sql, oConnection)
recpc = comd.ExecuteScalar

sql = "SELECT pcnr FROM pc where lokl=''"&w&"'';"
comd = New SqlCommand(sql, oConnection)
dtreader = comd.ExecuteReader

对于 j = 1 到 recpc
dtreader.Read()
ww = dtreader.GetValue(0)
sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = 新的 SqlCommand(sql, oConnection)
comd1.ExecuteNonQuery()
下一个
dtreader.Close()

但仍然出现同样的错误:comd1.ExecuteNonQuery()
"伊利亚胡·戈尔丁"<RE**************************@mMvVpPsS.orgschree f in
贝里希特新闻:OB*************@TK2MSFTNGP02.phx.gbl...
为运行插入创建另一个命令对象:

sql = "INSERT INTO mytable (field1 values(" & ww & ")"
comd1 = 新的 SqlCommand(sql, oConnection)
comd1.ExecuteNonQuery()
--
伊利亚胡·戈尔丁,
软件开发人员顾问
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin




本文地址:https://www.itbaoku.cn/post/1050421.html