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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:24:07+00:00 2026-05-28T02:24:07+00:00

I made a class for DB connection like this: public class DbHelper : IDisposable

  • 0

I made a class for DB connection like this:

public class DbHelper : IDisposable
{
private bool disposed;
public DbHelper()
{
    disposed = false;
}

public static SqlConnection ConnectionSender()
{
    var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbname"].ConnectionString);
    return conn;
}

public System.Int32 ExecuteNonQuerySender(SqlCommand cmd)
{
    System.Int32 result;
    using (var conn = ConnectionSender())
    {
        conn.Open();

        cmd.Connection = conn;

        result = cmd.ExecuteNonQuery();
    }
    return result;
}

~DbHelper()
{
    Dispose(false);
}

public void Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
    if (!disposed)
    {
        if (disposing)
        {
        }
    }
    disposed = true;
}
}

I call this class using method:

var cmdParent = new SqlCommand { CommandText = sqlQuery };
using (var helper = new DbHelper())
{
            dt = helper.ExecuteNonQuerySender(cmdParent);
}

This works fine but my server is overloaded and I suspect about DB leaking. DB connection objects should be closed and disposed in this code. Did I do something wrong? (programmatically)
I know there are also professional ways to solve DB connection but i want to try this code.

  • 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-28T02:24:07+00:00Added an answer on May 28, 2026 at 2:24 am

    First off, it looks like you didn’t give us the whole DbHelper class. You don’t show the DataTableSender method, but you use it in your example.

    Secondly, assuming that there isn’t anything unexpected in the rest of DBHelper, nothing in this class needs any of the dispose logic. You can remove the IDisposable implementation entirely, along with the finalizer, the Dispose method, and the disposed variable, and it will make no difference (other than allowing you to remove the using clauses when using the class). I’d recommend doing that and making the class a static class (requires making the ExecuteNonQuerySender method static also).

    Finally, there is nothing here which is leaking connections, however callers could be ‘leaking’ connections if they call ConnectionSender and never close/dispose the connection that they get back.

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

Sidebar

Related Questions

I made a class that derives from Component: public class MyComponent: System.ComponentModel.Component { }
I have an asynchronous socket listener class like this: using System; using System.Collections.Generic; using
I have a simple question. I have a model that looks like this: public
I made a class from Linq to SQL Clasees with VS 2008 SP1 Framework
I made a class library, and put it in GAC. It has one C#
I've made a class which inherits from UserControl, but which I only want to
I have a class made up of several fields, and I have several constructors.
I have made a class which a form can inherit from and it handles
I have made an class which conforms to the NSCoding protocol and does all
I made a form class in c# that has a devexpress grid, a label

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.