Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:26:48+00:00 2026-06-14T04:26:48+00:00

I have an old app that I’m supporting and I ran across the following

  • 0

I have an old app that I’m supporting and I ran across the following Shared SQLConnection:

    private static SqlConnection cnSQL = new SqlConnection(ConfigurationManager.ConnectionStrings["SQL"].ConnectionString.ToString());
    private static SqlCommand cmdSQL = new SqlCommand();

Now this connection is opened and closed everytime it’s needed, but it still get a sporatic error, and I believe this is because its being shared accross users (being static). Is my assumption correct? I believe I am much better off creating a new connection inside each method that needs it instead of having one per class. Or can I just remvoe the static option, and keep one connection per page and not have to worry about cross user contamination?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:26:49+00:00Added an answer on June 14, 2026 at 4:26 am

    Static members are shared between all the objects and methods of your code in the actual instance of your application but in no case between different users.

    I would make the connection string static but not the connection itself. As you say, open a new connection in each method. Connection pooling will ensure that the physical connections will be kept open.

    public static readonly string ConnectionString connString =
        ConfigurationManager.ConnectionStrings["SQL"].ConnectionString.ToString();
    

    …

    private void SomeMethod()
    {
        using (var conn = new SqlConnection(connString)) {
            string sql = "SELECT ...";
            using (var cmd = new SqlCommand(sql, conn)) {
                ...
            }
        }
    }
    

    Make sure you are embedding the code in using-statements. That way Resources are released even when an exception should occur.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an old iOS app that I never distributed and am now trying
I have an old asp app I am moving to a new server with
I have a regular old iPhone app that has a main view controller that
I have an old Facebook app that installs on Facebook Pages. The tab page
I have an old post Re-Installing IPhone App From Inside The App That old
I do have an old app that refuses to work on Android 4.1 devices.
I have an old vb6 app that I'm in charge of maintaining that saves,
We have an old legacy app that we need to reverse engineer how it
I have an old FBML app that uses fb:submit. When I was trying to
I have an old app I made for myself that I haven't used in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.