连接当前状态仍然打开 [英] Connection current state still open

查看:62
本文介绍了连接当前状态仍然打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个用于运行SQL查询的方法。



问题是,即使我一直关闭与数据库的连接,我仍然会收到一个错误,即连接的当前状态仍然是打开的。这导致我的整个应用程序崩溃



我不知道为什么会发生这种情况,因为此代码在另一个Web应用程序中100%运行而没有收到此错误。 />


任何见解都会非常有用



这是一个代码示例



Hello all,

I have a method that i use to run SQL queries.

The problem is that even though i close the connection to the database all the time i keep getting an error that the current state of the connection is still open. That causes my whole application to break

I don't know why this is happening as this code worked 100% in another web application with out getting this error.

Any insight will be very helpful

Here is a code sample

private static String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnection"].ConnectionString;
    
private static SqlConnection SQLConnection = new SqlConnection(connectionString);

public static void runSQL(String SQL)
    {
        SQLCommand = new SqlCommand(SQL, SQLConnection);

        SQLConnection.Open();
        SQLCommand.ExecuteNonQuery();
        SQLConnection.Close();
    }

推荐答案

一对建议......



1.不要使用静态变量,否则它将保持完整。

2.放试试 catch finally 块。

3。关闭 finally 块内的连接。



更新代码以反映这些更改,然后重试。
Couple of Suggestions...

1. Don't use Static Variables, otherwise it would remain as intact.
2. Put try, catch and finally blocks in your code.
3. Close the connection inside the finally block.

Update your code to reflect these changes and try again.


http://stackoverflow.com/questions/814206 / getting-db-connection-through-singleton-class [ ^ ]



这可能是您问题的解决方案。
http://stackoverflow.com/questions/814206/getting-db-connection-through-singleton-class[^]

this might be a solution for your issue.


这篇关于连接当前状态仍然打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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