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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:36:05+00:00 2026-05-16T11:36:05+00:00

I am designing a database wrapper for C#. Below are the two options I

  • 0

I am designing a database wrapper for C#.
Below are the two options I have:

Option A:

class DBWrapper:IDisposable
{
     private SqlConnection sqlConn;

     public DBWrapper()
     {
            sqlConn = new SqlConnection("my connection string");
            sqlConn.Open();
     }

     public DataTable RunQuery(string Sql)
     {
              implementation......
     }

     public Dispose()
     {
            if(sqlConn != null)
                   sqlConn.Close();
     }
}

Option B:

class DBWrapper
{
     public DBWrapper()
     {            
     }

     public DataTable RunQuery(string Sql)
     {
             SqlConnection sqlConn = new SqlConnection("my connection string");
             .....implementation......
             sqlConn.Close();               
     }   
}

For option A connection is opened when class is instantiated. So no matter how many times the caller calls RunQuery the connection is always ready. But If the application instantiates DBWrapper early in the application, the connection will be just opened and doing nothing until the application is finished. Also, it could have many DBWrapper instantiated during the execution. So, it’s kinda wasting resources.

For option B it doesn’t have the problem option A has, but the a new connection has to be opened and closed everytime the caller calls RunQuery. I am not sure how much it will hurt the performance.

Please share your expertise. Thank you for reading.

  • 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-16T11:36:06+00:00Added an answer on May 16, 2026 at 11:36 am

    For performance reasons, you’ll definitely not want to go with Option B (at least in the cases I experienced.)
    Let me suggest Option C:

    class DBWrapper:IDisposable { 
    
        private SqlConnection sqlConn;
    
        public void EnsureConnectionIsOpen()
        {
            if (sqlConn == null)
            {
                sqlConn = new SqlConnection("my connection string");
                sqlConn.Open();
            }
        }
    
        public DataTable RunQuery(string Sql)
        {
            EnsureConnectionIsOpen();
            implementation......
        }
    
        public Dispose()
        {
            if(sqlConn != null)
                sqlConn.Close();
        }
    }
    

    You might consider using the singleton pattern to make sure there is only one instance of your DBWrapper.

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

Sidebar

Related Questions

I have a simple mysqli wrapper class I use for my database operations. I
I am designing a database for a project. I have a table that has
I am designing a database for a web application and would like to have
I'm designing my database and LINQ To SQL ASP.NET web application. Imagine I have
I am designing a database for my cookbooks. I have created multiple tables in
I am designing a database with the code igniter datamapper. Imagine that I have
I am designing database tables for a master-detail scenario. The specific requirement is that
I'm designing a database to house scientific test data, using sqlalchemy. I've hit a
I'm designing a database application where data is going to change over time. I
I was designing a database for a site where I need to use a

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.