English 中文(简体)
如何切断被遗忘的联系?
原标题:How to close a forgotten connection?

我建立了联系和一个SqlReader,但想关闭这两人:

SqlConnection conn = new SqlConnection("connection info");
conn.Open();

string sql = "SQL command HERE";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();

现在,在试图再次实施法典时,总是给我这个错误:

System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.

link向我介绍了如何适当开放和关闭连接,但并未解释如何关闭连接。

我试图关闭这个板块,我尝试把服务器上的选项(认为无用)输入数据库。 我修改了该法典,只是要结束联系和读者(编辑和管理该守则,但问题在改用该守则之后依然存在)。

我如何结束这种“幽灵”联系? 是否有任何办法(维持和平部队)关闭所有运行的连接?


<>>[EDIT:] 我无法真正解决这一问题。 工作是将<代码>MultipleActiveResultSets=true添入显示链接

最佳回答

从所有答案来看,他们似乎告诉你如何避免这一问题。

如果我不错的话,你指的是客户(大计算机)和服务器( s服务器)都存在联系,因为你不愿关闭该服务器,而且你对此表示担忧。

Think of your connection to the server as a phone conversation. I could hang up on you, but it takes a few seconds for your phone to realize the connection is lost. You may sit there wondering if I ve hung up, or just stopped talking. You really don t know. This is what happens on the server when a connection isn t closed properly. (On older landlines, you could leave the phone off the hook and tied up the line indefinitely.)

通过关闭代码上的联系,你在关闭服务器之前,有效地告诉服务器关闭其连接点。 如果你FAIL要关闭汇合点,在方案退出时,或如果你再接一间,就会关闭,但服务器可以在那里安装开放式连接。 (想到有人在座,想“他只是hang在我身上吗?”)

如果我不犯错,你想要在服务器结束时关闭。 (将他们安置到“hang子”。)

在重新boot后,在您的末尾关闭<>。 它应当自己在服务器上明确。

然而,如果你想这样做的话,你可以使用这一信息在代码末的服务器上加以澄清:。 你们如何杀害与2005年服务器数据库的所有现有链接?

一种更为容易的方法是,如上所述,在服务器管理演播室里这样做:。 http://www.mikebevers.be/blog/2009/07/ric-open-sql-processes-linkions/

问题回答

I don t think you can access the ghost object, for future, just use using construct where it s possible:

using(SqlConnection conn = new SqlConnection("connection info"))
{
   conn.Open();

   string sql = "SQL command HERE";
   SqlCommand cmd = new SqlCommand(sql, con);
   SqlDataReader reader = cmd.ExecuteReader();
....
}

Wrap the creation in a using statement - this will always ensure the connection gets closed:

using(SqlConnection conn = new SqlConnection("connection info"))
{
   // your code
}

所有这些答复都告诉你如何避免这一问题,但它们没有解释问题是什么。

缩略语 详见本《博客。 从根本上说,如果你不关闭数据检索系统,那么它就会继续开放,致力于这一联系和指挥。

正如其他人所说的那样,最好确保你关闭一切资源。

using (SqlConnection connection = new SqlConnection("...")) {
    connection.Open();

    string sql = "SQL command HERE";

    using (SqlCommand cmd = new SqlCommand(sql, con))
    using (SqlDataReader reader = cmd.ExecuteReader()) {
            // do your stuff
    }
}

即便在你“混淆”或“处置”某种联系时,如果你在你的联系中明确显示,这种联系不会真正消失,也应当告诉真相。 但是,你可以这样做。

我知道这是个老员额,这可能有助于没有人。 但我看到了一个机会来表明我对这个问题的看法是错误的。

First, you are creating a SqlConnection named conn but in your SqlCommand named cmd you are calling con as your connection. This is a problem:

SqlConnection conn = new SqlConnection("connection info");
conn.Open();

string sql = "SQL command HERE";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();

因此,它给你留下错误:

System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.

第二,在你使用后关闭一个通道:

conn.Close();

第三,关闭一个SqlDataReader,你使用:

reader.Close();

但你只是把SqlDataReader分配给读者。 你们从来都不实际打开SqlDataReader。 开放使用:

reader.Read();

或:

while (reader.Read())
{
    // Code
}

现在是启动连接和关闭SqlDataReader的适当途径:

using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
    conn.Open();

    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = "SELECT * FROM TableName;";

        SqlDataReader reader = cmd.ExecuteReader();

        reader.Read();

        if (reader.HasRows)
        {
            strCol1 = reader.GetValue(0).ToString();
        }

        reader.Close();
  }

  conn.Close();
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签