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

The Archive Base Latest Questions

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

I am trying to understand DbConnection and DbCommand, and the proper way to dispose

  • 0

I am trying to understand DbConnection and DbCommand, and the proper way to dispose those objects after use.

Following is the code snippet I have.
By using “using statement” on DbConnection and DbCommand, would it be sufficient? I am trying to prevent possible memory leak.

2nd question,

Do I have to Dispose DbCommand object?

thanks a lot

DbProviderFactory fac = DbProviderFactories.GetFactory(this.DatabaseProviderName);

using (DbConnection dbConn = fac.CreateConnection())
{
     dbConn.ConnectionString = this.ConnectionString;

     using (DbCommand comm = fac.CreateCommand())
     {
          comm.CommandText = "select * from aTable";
          comm.Connection = dbConn;
          DataTable targetTable = new DataTable();

          DbDataAdapter facDA = fac.CreateDataAdapter();
          facDA.SelectCommand = comm;
          facDA.Fill(targetTable);

          //assuming Adapter would open / close connection (right assumption?)

          //do something with the datatable
     }
}
  • 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:29:08+00:00Added an answer on May 14, 2026 at 12:29 am

    using using is the same as a try/finally block with dispose() called in finally.

    DbCommand should be wrapped in a using statement as it implements IDisposable.

    Note that DbCommand is actually an abstract class so you will need to either

    • derive from it
    • code to an interface (IDbCommand)
    • use one of the predefined derived classes such as SqlCommand.

    DbConnection is also an abstract class so you will need to do something similar as I have suggested above for DbCommand for this too.

    The general recommendation is that if an object implements IDisposable, it should be wrapped in a using statement such that Dispose() is called to free resources, even if an Exception is thrown within in the statement block. In your example then, a good practice would be to wrap each of the connection, command, DataTable and DbDataAdapter objects in a using statement.

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

Sidebar

Related Questions

Trying to understand the math of this code snippet. A token is provided which
Trying to understand what Sql Profiler means by emitting sp_reset_connection. I have the following,
After trying to understand why client code is not rendered in a page (injected
HI Trying to understand how __radd__ works. I have the code >>> class X(object):
Trying to understand Ruby a bit better, I ran into this code surfing the
Just trying to understand that - I have never used it before. How is
Trying to understand why this doesn't work. I keep getting the following errors: left
Trying to understand resources in java-land. I believe the following is true: Resources loaded
Trying to understand this binding process of the WPF. See the code at the
trying to understand how custom admin commands work, I have my project named mailing

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.