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

问题描述

大家好,



我有一个用于运行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();
    }

推荐答案



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

05-29 18:41
查看更多