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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:07:46+00:00 2026-05-31T19:07:46+00:00

If methods/functions I’m going to call involve the need of an open SqlConnection, I

  • 0

If methods/functions I’m going to call involve the need of an open SqlConnection, I will open this up in the method which is calling the function. For example:

protected static void btnSubmit(){
   conn.Open();
   myMethod(someParam, conn);
   conn.Close();
}

protected static void myMethod(object someParam, SqlConnection conn){
   //Some SQL commands etc here..
}

I do this so that I:

  • Only ever open and close 1 SqlConnection per process

However, would it be better to structure my code like so:

protected static void btnSubmit(){
   myMethod(someParam);
}

protected static void myMethod(object someParam){
   SqlConnection conn = New SqlConnection(".....");
   conn.Open();
   //Some SQL commands etc here..
   conn.Close();
}

The advantage I see of structuring it this way is:

  • I don’t have to pass an extra parameter for each method
  • If later down the line the method no longer has a SQL command, there is not an unused parameter being called each time

The disadvantage I see to this, is:

  • If myMethod is a recursive method, then when it calls itself its going to be opening another SqlConnection, and so on, and so on..
  • If btnSubmit is calling multiple methods which all require a SqlConnection, each one is going to open and close a new connection.

What is the best way of doing this, and which is most commonly practised?

  • 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-05-31T19:07:47+00:00Added an answer on May 31, 2026 at 7:07 pm

    ADO.NET uses connection pooling, so it automatically reuses existing opened connections, even when you think that you are opening a new connection. Having that in mind, there is really no reason to pass a connection through your code (as a parameter). This will make your code much cleaner, with the same performance as when you were passing the connection as a parameter.

    More details here

    Also (and this is really important), please, use the “using” keyword. That way, you will not have to deal with closing the connection and cleanup, because your code as it is written now doesn’t deal with closing the connections, so in a case of some exception you might end up with hitting connection limit on your server. Go with something like this:

    using(var connection = new SqlConnection(<connection_string>))
    {
      connection.Open();
      using(var command = connection.CreateCommand())
      {
    
      }
    }
    

    As you can see, there is no need to call connection.Close() or deal with exceptions and closing the connection in your finally block, because that is a “job” for the “using” block.

    Also, one important note…transactions are not passed via connection polling, so if you want to keep your transaction across method calls, you will have to pass your connection (and that is the only reason I can think of why you should do that).

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

Sidebar

Related Questions

I have 2 functions/methods in the same controller in CodeIgniter like this: public function
does anybody know how to call C++ functions or methods via JavaScript. Need scripting
if I have methods like function getVar1(){ return $this->var1; } function getVar2(){ return $this->var2;
Possible Duplicate: Calling virtual method in base class constructor Calling virtual functions inside constructors
How do I call class methods from functions within the class? My code is:
Any helper class anywhere which wrapps kernel32 APIs, with all functions-methods and structures? Or
Is it possible to add methods to functions? For example: <? function func(){ ;
Not many are aware of this feature, but Python's functions (and methods) can have
This might sound dumb but why some functions/methods in Objective-C use parentheses rather than
I'm trying to write a jQuery plugin that will provide additional functions/methods to the

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.