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 9207879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:29:11+00:00 2026-06-18T00:29:11+00:00

I want to re-use the same SQLConnection accros different methods within the class. What

  • 0

I want to re-use the same SQLConnection accros different methods within the class. What I’m doing right now (testing only) is creating and opening a connection in the constructor:

SQLConnection Connection;

Constructor(string connection_string)
{
    this.Connection = new SqlConnection(connection_string);
    this.Connection.Open();
}

then I use “this.Connection” inside methods and finally use this.Connection.Close() and Dispose() at the end when the object is not needed anymore. From what I know it would be cleaner to use ‘using’ inside each method like this (the constructor would only set the connection_string):

using (SqlConnection connection = new SqlConnection(connection_string)) {
 connection.Open(); ...
}

Because of connection pooling, only one connection is actually used, despite the above ‘using’ line being placed in multiple methods (e.g. when they are called one after another), correct? However, wouldn’t this create many SQLConnection instances where only one would be needed? E.g.:

MyClass obj(some_string);
obj.Method1(); // calls 'using SqlConnection connection = new SqlConnection'
obj.Method2(); // calls 'using SqlConnection connection = new SqlConnection'
obj.Method3(); // calls 'using SqlConnection connection = new SqlConnection'

So what’s the proper, optimal way to share the SQLConnection?

  • 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-18T00:29:13+00:00Added an answer on June 18, 2026 at 12:29 am

    You are correct about all your statements. However you are missing one important point: creating lots of instances of a type in .NET is not necessarily a bad thing!

    • You should not create a single connection instance in the constructor
    • You should create a connection locally, when needed
    • It is best practice to use the using paradigm when creating disposable objects
    • You should make a habit of calling Dispose on disposable objects in a try...finall block (if not using using) – in case your specific scenario doesn’t land itself well for the use of usings; for instance using asynchronous methods)
    • Finally, you should not keep a SQL connection open for longer than you need it to be. Again, just take advantage of connection pooling in the ADO.NET provider for SQL Server

    Now, the reason it’s not a problem to create lots of instances of the Connection type is because creating objects in the .NET CLR is optimized (fast memory allocation and object instantiation) and relatively pain free (no need to worry about releasing memory thanks to Garbage Collection).. You also have the benefits of connection pooling in case of the ADO.NET providers so really, you shouldn’t concern yourself with managing the number of instances of this Type.

    It should be obvious that in other cases (such as heavy/large objects) creating lots of them can have an impact on memory pressure and performance. So always asses the situation as best as you can..

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

Sidebar

Related Questions

I want to use the same validation type (but with different options based on
Now I want to use a serversocket to listen port x, in the same
Curious situation: public class MyTextBox : TextBox { // I want use the same
I want to use same div tag for different page displaying using html. how
i want to use same command for another form in java me. i only
I want to use the same ant script to do a build in either
I want to use the same style in many listviews. And in my style
i have a variable $cartTotal in cart.php page i want to use that same
I want to use regular expression same way as string.Format. I will explain I
i want to use focus function in jquery to return to same area of

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.