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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:52:16+00:00 2026-06-12T15:52:16+00:00

My web application has the following class: public class DatabaseProvider { private readonly IDbConnection

  • 0

My web application has the following class:

public class DatabaseProvider {
    private readonly IDbConnection _cn;

    public DatabaseProvider(IDbConnection cn) {
        _cn = cn;
    }

    public void ExecuteNonQuery(string query) {
        // Execute the query
        var cmd = _cn.CreateCommand();
        cmd.CommandText = query;
        cmd.ExecuteNonQuery();
    }
}

Where the IDbConnection dependency is declared using the following code (with Microsoft Unity):

container.RegisterType<IDbConnection>(new InjectionFactory(c => {
    var cn = new SqlConnection("...");
    cn.Open();
    return cn;
}));

This allows me to swap out the connection to SQLite like so:

container.RegisterType<IDbConnection>(new InjectionFactory(c => {
    var cn = new SQLiteConnection("Data Source=:memory:;Version=3;");
    cn.Open();
    return cn;
}));

I use SQLite when testing and the connection must remain open otherwise the data is lost. Therefore I can’t just open and close the connection when needed.

I have always opened the connection to the database when it was needed and was wondering whether this will scale for large applications?

I’d appreciate any advice on how this can be improved. 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-06-12T15:52:17+00:00Added an answer on June 12, 2026 at 3:52 pm

    As a general rule in ADO.NET the connection must be closed as soon as possible.

    Connections are re-used from connection pool and are scarce resource.

    The recommended way to do this is with using () {} construct.

    SQL Server Connection Pooling (ADO.NET)

    We strongly recommend that you always close the connection when you are finished using it so that the connection will be returned to the pool. You can do this using either the Close or Dispose methods of the Connection object, or by opening all connections inside a using statement in C#, or a Using statement in Visual Basic. Connections that are not explicitly closed might not be added or returned to the pool. For more information, see using Statement (C# Reference) or How to: Dispose of a System Resource (Visual Basic) for Visual Basic.

    So, I suggest that you make a interface IDbProvider and then make 2 implementations: SqlServerProvider and SQLiteProvider and handle opening and closing connections inside them. Then use Unity to bind one provider or another.

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

Sidebar

Related Questions

My Application has the following structure: myproject (primary Silverlight project) myproject.Web (website for the
We have the following setting: There is a web application which has a jboss
My web appkication has the following set up.. WCF application hosted on IIS7 Basic
Guys, can anyone explain the following scenario: 1) Web application has module1.jar in its
I have the following setup: Web.config has customErrors mode=Off Global.Application_Error() event calls a custom
My web application has got a lot of service tables/entities, such as payment_methods ,
A 3rd party web application has a cross-scripting security issue. There is one page
The JavaScript in my web application has grown into one huge file. I want
Is the Application variable shared when the web application has multiple role instances in
A specific page in my web application has a URL www.example.com/Test/Index However, I want

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.