Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

c# - How to stop ASP.NET web application from keeping SQL connection open when all connections are closed after use?

I've been fighting with this problem for a significant period of time now but can't seem to find any way to permanently fix it.

In our internal CRM system (ASP.NET WebForm, .NET Framework 4.5) we interact with our production database through a connection string in the backend of the code as well as in the Web.Config file (I should point out I took over the department a few months and inherited a legacy codebase from many years ago with a lot of problems and holes).

This issue comes up now and again pretty much every day. The only solution I can find is to edit a comment in the code and resave it. This stops the issue temporarily but then it happens again.

I get this error message:

The connection was not closed. The connection's current state is open.

This is despite the fact that I have included a condition to close the connection state before each .Open() function.

public DataSet RunProcedureQuery(SqlConnection sqlconnection, string procedurename, SqlParameter[] parameters)
       {
            //if conenction already open close
            if (sqlconnection.State == ConnectionState.Open)
           {
             sqlconnection.Close();
           }

            sqlconnection.Open();

            SqlCommand cmdgeneraldata = new SqlCommand(procedurename, sqlconnection);
            cmdgeneraldata.CommandType = CommandType.StoredProcedure;
            cmdgeneraldata.CommandTimeout = 100000;

           

            for (int i = 0; i <= parameters.GetUpperBound(0); i++)
            {
                cmdgeneraldata.Parameters.Add(parameters[i]);
            }

            SqlDataAdapter da = new SqlDataAdapter(cmdgeneraldata);

            DataSet data = new DataSet();
            da.Fill(data);
            sqlconnection.Close();
            return data;
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...