错误“已经存在与该命令相关联的打开的数据加载器,必须首先将其关闭。” [英] Error "there is already an open datareader associated with this command which must be closed first."

查看:47
本文介绍了错误“已经存在与该命令相关联的打开的数据加载器,必须首先将其关闭。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何克服此错误总是在同一进程多次点击时发生已经有一个与此命令关联的打开的数据读取器必须先关闭。



任何帮助将不胜感激。



提前付款



什么我试过了:



这个错误发生在我项目的多个地方。



这是我的代码:

How do i overcome this error always occurs at the time of multiple hits on same process "There is already an open Data Reader associated with this Command which must be closed first."

Any Help will be appreciated.

Thanks in Advance

What I have tried:

This error occurs at multiple places in my project.

This is my Code:

Shared Function GetTable(ByVal SqlString As String) As DataSet
Dim ds As New DataSet
If con.State = ConnectionState.Closed Then con.Open()
Using con
Dim da As New SqlClient.SqlDataAdapter(SqlString, con)
da.SelectCommand.CommandTimeout = 5000
da.Fill(ds)
End Using
Return ds

推荐答案

当您打开DataReader时,它会阻止关闭读取器之前的连接。

在代码中的某个地方,您已经从SQL命令创建了一个DataReader,并使用相同的连接对象开始读取 - 并且它将保持活动状态,直到读取器或连接对象关闭,即使使用它的方法已经完成执行。



查看代码,找到使用连接的所有位置,并确保关闭并处理你使用的一切:SqlCommand,SqlDataAdapter,SqlDataReader。



执行此操作的最佳方法是删除全局连接对象,并在每次要使用它时创建新连接:SqlConnections是一种稀缺资源,您不应该坚持使用它们,特别是如果它们是开放的,因为它们可能阻止数据库中的其他用户。
When you open a DataReader, it "blocks" the connection until the reader is closed.
Somewhere in your code you have created a DataReader from an SQL command, and started it reading using the same connection object - and it will remain active until the Reader ort the Connection object is closed, even if the method that uses it has finished executing.

Look at your code, find everywhere you use the connection, and make sure that you close and Dispose of everything you use: SqlCommand, SqlDataAdapter, SqlDataReader.

The best way to do this is to delete the global connection object, and create new connections each time you want to use it: SqlConnections are a scarce resource and you shouldn't hold on to them, particularly if they are open, as they may block other users from the DB.


这篇关于错误“已经存在与该命令相关联的打开的数据加载器,必须首先将其关闭。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆