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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:12:28+00:00 2026-06-07T04:12:28+00:00

I was searching the web for a while now. But didn’t find any clear

  • 0

I was searching the web for a while now. But didn’t find any clear answer to my question. Whether when connecting to a database I should use “using” or I can just go with try-catch-finally? What I mean is:

I don’t know if I should call the dispose method each time I finish interacting with the database or just close the connection.

    static public List<Category> GetAll()
    {
        List<Category> CategoryList;

        try
        {
            BaseDAO.Dbconn.Open();
            BaseDAO.SetCommand(BaseDAO.CommandAction.Read, "SELECT * FROM Categories");

            CategoryList = new List<Category>();

            using (DbDataReader reader = BaseDAO.Dbcmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    int ID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                    string Name = reader.GetString(reader.GetOrdinal("CategoryName"));
                    string Description = reader.GetString(reader.GetOrdinal("Description"));
                    CategoryList.Add(new Category(ID, Name, Description));
                }
            }
            return CategoryList;
        }
        catch (Exception ex)
        {
            BaseDAO.Dbconn.Dispose();
            throw ex;
        }
        finally { BaseDAO.Dbconn.Close(); }

    }

The “Dbconnection” is static not sure if this is a good solution as well…

I am getting to know ADO and just wanted to know what’s the best answer for this kind of question.

  • 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-07T04:12:29+00:00Added an answer on June 7, 2026 at 4:12 am

    It doesn’t make any difference – whatever you prefer. The using clause gets turned into a try-finally by the compiler anyway.

    EDIT I just noticed you are only calling Dispose if an exception occurs. You should move this into the finally clause as well. Dispose will call Close, so you don’t need to specifically call it if you don’t want to.

    Further edit Given that, as you say, your DbConnection is static, then you don’t want to call Dispose, but you do need to call Close. However, there should not be any need to use a static DbConnection – connection pooling will take care of efficiently handling the connections. Your code should create a new connection instance every time.

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

Sidebar

Related Questions

I've been searching for a while now and can't seem to find an answer
I have been searching the web for an answer now for quite a while,
I've spent hours searching the web for an answer to this question... Here's what
I've been searching the web for a while now and still can't seem to
I've been searching the web (and S.O.) for a while before asking this question.
I've been searching for a while for an answer to my problem but so
I've searching for this for a while now online (Google, and StackOverflow), but haven't
While searching the web, I found this code: $apprequest_url =https://graph.facebook.com/ . /apprequests?ids=USERID_1,USERID_2,USERID_3 . &message='INSERT_UT8_STRING_MSG'
When searching the web it is easy to find this way to print out
What is default storage class of a global variable? While searching on web I

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.