简体   繁体   English

在已经打开的DataReader的连接字符串中出现问题

[英]Having issue in connection string of already an open DataReader

There is already an open DataReader associated with this Command which must be closed first. 已经有一个与此命令关联的打开DataReader,必须先关闭它。

I m facing this issue when same person open the same page at same time on different system. 当同一个人在不同的系统上同时打开同一页面时,我面临这个问题。 I have searched a lot on this but found no successful solution. 我已经搜索了很多,但没有找到成功的解决方案。

I have tired : 我累了:

  1. MultipleActiveResultSets = true in connection string MultipleActiveResultSets = true连接字符串中的MultipleActiveResultSets = true
  2. Increasing Connection waiting time 增加连接等待时间
  3. Verified all connection are closed 已验证所有连接已关闭

This issue comes only when above condition created. 仅在创建上述条件时才会出现此问题。 Kindly let me know solution which really works 请告诉我真正有效的解决方案

this my connection function which im using 这个我正在使用的连接功能

public DataSet SelectDs(string str)
{
    DataSet ds = new DataSet();

    if (con.State == ConnectionState.Closed)
    {
        con.ConnectionString = ConStr;
        con.Open();
    }

    cmd.CommandText = str;
    cmd.Connection = con;
    cmd.CommandTimeout = 12000;
    adpt.SelectCommand = cmd;
    adpt.Fill(ds);

    con.Close();
    return ds;
}

It is a mortal sin to use a global connection object in that way. 以这种方式使用全局连接对象是一种致命的罪。 It is bad (very bad) in WinForms applications, but in ASP.NET is deadly. 它在WinForms应用程序中很糟糕(非常糟糕),但在ASP.NET中是致命的。 (as you have discovered) (正如你发现的那样)

The usage pattern for a disposable object (and an expensive one like the connection) is 一次性物体(和连接物之类的昂贵物体)的使用模式是

CREATE, OPEN, USE, CLOSE, DESTROY

The Connection Pooling mechanism exist to make easier the usage of this pattern. 存在连接池机制以使该模式的使用更容易。
Instead you try to work against it and you pay the consequences. 相反,你试图反对它,你付出了后果。

Your code should be rewritten as 您的代码应该重写为

public DataSet SelectDs(string str)
{
    DataSet ds = new DataSet();

    using(SqlConnection con = new SqlConnection(constring))  // CREATE
    using(SqlCommand cmd = new SqlCommand(str, con))         // CREATE
    {
        con.Open();    // OPEN
        cmd.CommandTimeout = 12000;
        using(SqlAdapter adpt = new SqlAdapter(cmd))   // USE
             adpt.Fill(ds);

        return ds;
    }  // CLOSE & DESTROY 
}

How about putting inside a Using statement like 如何放入像这样的Using语句

    using(SqlConnection connection = new SqlConnection("connection string"))
{

connection.Open();

using(SqlCommand cmd = new SqlCommand("SELECT * FROM SomeTable", connection))
{
    using (SqlDataReader reader = cmd.ExecuteReader())
    {
        if (reader != null)
        {
            while (reader.Read())
            {
                //do something
            }
        }
    } // reader closed and disposed up here

   } // command disposed here

 } //connection closed and disposed here

in finally clause use this 在finally子句中使用此

if (readerObj.IsClosed == false)
{
  readerObj.Close();
}

I think you should also dispose your command object before returning dataset. 我想你也应该在返回数据集之前处理你的命令对象。

try cmd.Dispose() after con.close() 在con.close()之后尝试cmd.Dispose()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM