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

  • Home
  • SEARCH
  • 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 7006793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:31:24+00:00 2026-05-27T21:31:24+00:00

when we want to connect to database from visual studio should we always check(before

  • 0

when we want to connect to database from visual studio should we always check(before using another query) that if our connection is open,close it and again open it??

 if(connection is open){
  close connection;
   }  
  open connection for another use;
  • 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-27T21:31:25+00:00Added an answer on May 27, 2026 at 9:31 pm

    Assuming you’re using .NET, you should generally open and close the connection for each logical operation. If you’re doing multiple queries in the same code path (i.e. you know you’re about to do another query) then it makes sense to leave the connection open, but I wouldn’t leave it open “just in case”.

    Bear in mind that the connection pooling in .NET makes it reasonably cheap to “open a connection” as the network connection will still be up anyway. Typically you’d use something like:

    using (SqlConnection connection = new SqlConnection(...))
    {
        connection.Open();
        using (SqlCommand command = new SqlCommand(connection, sql))
        {
            // Execute the command etc
        }
    }
    

    The using statement will “close the connection” (read: return it to the connection pool) automatically when you’re done.

    Keeping each query (or set of queries) logically separate means it’s harder to end up with race conditions, threading issues etc. I wouldn’t recommend keeping a connection as a global variable or anything like that.

    Of course, if you’re using something like LINQ to SQL the connection handling is likely to be mostly done for you.

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

Sidebar

Related Questions

I am using Npgsql to connect to the postgresql database from visual studio 2010.
I want to connect from home using SQL Server 2005 to another PC. I
I want to connect to mysql database from my asp page.So for that as
I want to connect SQL database from R.Net. I am using R.Net from vb.net.
I want to connect to the access 2010 database from excel.I am using VBA.I
I'm using PDO for connect to the database in a system where I want
I want to check whether url is available from my database. I choose fopen
I want to connect to an online SQL database. to do that, I use
I connect a database on my server from the Android application and I want
I have Visual Studio 2008 running from Windows XP. Our server is Windows 2003

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.