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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:44:18+00:00 2026-06-02T20:44:18+00:00

I want to understand the difference between public DataTable ExectNonActQuery(string spname, SqlCommand command) {

  • 0

I want to understand the difference between

public DataTable ExectNonActQuery(string spname, SqlCommand command)
{
    using (DataTable dt = new DataTable())
    {
        cmd = command;
        cmd.Connection = GetConnection();
        cmd.CommandText = spname;
        cmd.CommandType = CommandType.StoredProcedure;
        da.SelectCommand = cmd;
        da.Fill(dt);
        return (dt);
    }
}

and

public DataTable ExectNonActQuery(string spname, SqlCommand command)
{
    DataTable dt = new DataTable();
    cmd = command;
    cmd.Connection = GetConnection();
    cmd.CommandText = spname;
    cmd.CommandType = CommandType.StoredProcedure;
    da.SelectCommand = cmd;
    da.Fill(dt);
    return (dt);
    }
}

I actually want to understand what is the benefit of creating a new object using “using” instead of creating it directly like this

DataTable dt = new 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-06-02T20:44:20+00:00Added an answer on June 2, 2026 at 8:44 pm

    using guarantees that the object is disposed at the end of the using statement

    you could call .Dispose() manually instead, but with using, it will be disposed even if you throw an exception

    And you are protected from your own mistakes, such as forgetting to call .Dispose, or reassigning the variable before calling Dispose on it.

    It’s all in the documentation:

    http://msdn.microsoft.com/en-us/library/yh598w02.aspx

    Edit: also, as dasblinkenlight explains in the other answer, disposing an object as you return it from a function is a bad idea

    However, in the special case of DataTable, DataSet and DataView you do not really need to dispose those objects anyway, so in this particular example it is safe to ignore that DataTable is an IDisposable.
    Moreover, since DataTable’s .Dispose() method explicitely does nothing (Finalization is suppressed) your first example should actually work despite returning a disposed object.

    See Should I Dispose() DataSet and DataTable?

    So in your particular example the practical differences are probably zero.

    It is still generally good practise to wrap IDisposables with Using statement wherever practical. (if an object has to be constructed inside a function and then accessed outside that function this is not possible.)

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

Sidebar

Related Questions

I want to understand better the difference between using 'new' to allocate memory for
Yes, I do understand the difference between them. What I want to know is:
I want to understand the difference between a branch, a fork and a clone
I'm novice at C++, and I want to understand the difference between the examples
I want to understand what's the difference between those functions, and which of them
I want to understand the basic differences clearly between JavaScript object and JSON string.
I want to understand the difference between composition and aggregation relationship. Does anyone know
I'm working with ASP.NET AJAX and want to understand the difference between these two
I want to understand the external linkage and internal linkage and their difference. I
I can't understand the difference between count and sum I'm trying to get the

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.