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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:38:41+00:00 2026-05-14T00:38:41+00:00

ok now i am using the SQL database to get the values from different

  • 0

ok now i am using the SQL database to get the values from different tables… so i make the connection and get the values like this:

DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["XYZConnectionString"].ConnectionString;
connection.Open();
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Machines", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

sqlCmd.Parameters.AddWithValue("@node", node);
sqlDa.Fill(dt);
connection.Close();

so this is one query on the page and i am calling many other queries on the page.

So do i need to open and close the connection everytime…???

also if not this portion is common in all:

DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["XYZConnectionString"].ConnectionString;
connection.Open();

can i like put it in one function and call it instead.. the code would look cleaner…
i tried doing that but i get errors like:

Connection does not exist in the current context.

any suggestions???

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-05-14T00:38:41+00:00Added an answer on May 14, 2026 at 12:38 am
    1. You can definitely share the “open connection” code, no reason to duplicate it.
    2. With the SQL Server provider for ASP.NET, there is very little overhead with “closing” the connection every time. It just returns the connection to your process’ connection pool (see here), so opening the next connection will use very little overhead. I think it is good practice to close the connection after each operation
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using Linq to SQL for some time now and I find it
Is there similar component for Delphi like dictionary of Python? I'm now using TStringList
Right now I am using this to export a repeater (with multiple nested repeaters)
Right now we are using PostgreSQL 8.3 (on Linux) as a database backend to
Right now I am using a list, and was expecting something like: verts =
I am using a simple form and submitting my textbox values to database, <body>
I am using a ASP .NET ListView with a MS SQL Database. I use
I'm now using ListItem() for Json format result, but the generated Json text has
Running a rails site right now using SQLite3. About once every 500 requests or
.NET developers out there! Need your opinion here! I am now using Visual Assist

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.