如何避免 MSSql 2000 中出现错误“已存在与此命令关联的打开 DataReader”
Posted
技术标签:
【中文标题】如何避免 MSSql 2000 中出现错误“已存在与此命令关联的打开 DataReader”【英文标题】:How to avoid Error "There is already an open DataReader associated with this Command" in MSSql 2000 【发布时间】:2014-08-26 10:21:35 【问题描述】:我正在使用 SqlBulkCopy 将数据复制到同一数据库中的 2 个单独的表中。
obj_Command.CommandText = "Select * from tmpInvDtlMast where InvNo='111'"
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(con1)
bulkCopy.DestinationTableName = "tmpInvDtlMast"
Try
bulkCopy.WriteToServer(obj_Command.ExecuteReader())
Application.DoEvents()
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
bulkCopy.Close()
End Try
End Using
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(con1)
bulkCopy.DestinationTableName = "InvDtlMast"
Try
bulkCopy.WriteToServer(obj_Command.ExecuteReader())
Application.DoEvents()
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
bulkCopy.Close()
End Try
End Using
在第二次批量上传时出现此错误
已经有一个打开的 DataReader 与此命令关联
我已尝试使用我的连接字符串“MultipleActiveResultSets=true”
con1.ConnectionString = "Data Source=" & SqlServer & ";Initial Catalog=" & Database & ";Persist Security Info=True;User ID=" & User & ";Password=" & Pass & ";MultipleActiveResultSets=true"
在谷歌搜索更多解决方案后,我知道 MultipleActiveResultSets=true 不支持 MSSql 2000。在 Sql2000 中有什么办法可以解决这个问题。请帮忙
我正在使用 vb.net 2008
【问题讨论】:
【参考方案1】:作为一种解决方法,尝试复制连接和命令:
obj_Command.CommandText = "Select * from tmpInvDtlMast where InvNo='111'"
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(con1)
bulkCopy.DestinationTableName = "tmpInvDtlMast"
Try
bulkCopy.WriteToServer(obj_Command.ExecuteReader())
Application.DoEvents()
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
bulkCopy.Close()
End Try
End Using
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(**con2**)
bulkCopy.DestinationTableName = "InvDtlMast"
Try
bulkCopy.WriteToServer(**obj_Command2**.ExecuteReader())
Application.DoEvents()
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
bulkCopy.Close()
End Try
End Using
【讨论】:
@SajithaRathnayake 请分享您的解决方案【参考方案2】:我自己解决了我的问题。我所做的是删除
obj_Command.ExecuteReader()
带有新的 SqlDataReader 实例。
obj_Command.CommandText = "Select * from tmpInvDtlMast where InvNo='111'"
Dim dr as SqlDataReader()
dr=obj_Command.ExecuteNoneQuery()
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(con1)
bulkCopy.DestinationTableName = "tmpInvDtlMast"
Try
bulkCopy.WriteToServer(dr)
Application.DoEvents()
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
dr.Close()
End Try
End Using
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(con1)
bulkCopy.DestinationTableName = "InvDtlMast"
Try
bulkCopy.WriteToServer(dr)
Application.DoEvents()
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
dr.Close()
End Try
End Using
【讨论】:
以上是关于如何避免 MSSql 2000 中出现错误“已存在与此命令关联的打开 DataReader”的主要内容,如果未能解决你的问题,请参考以下文章
北亚数据恢复MSSQL 2000 附加数据库提示“错误 823”的数据恢复案例