Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this code which I took from MSDN website, i have a doubt. Please help.

C#
private static void CreateCommand(string queryString,
string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
           connectionString))
    {
        try
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            command.Connection.Open();
            command.ExecuteNonQuery();
        }
        catch (InvalidOperationException)
        {
            //log and/or rethrow or ignore
        }
        catch (SqlException)
        {
            //log and/or rethrow or ignore
        }
        catch (ArgumentException)
        {
            //log and/or rethrow or ignore
        }
    }
}


In this snippet, does it mean that all unmanaged resources inside the using block will get disposed when we exit the block ?

Thanks in advance.

What I have tried:

Did a lot reading on the web on dispose, GC and also posted questions to clarify my doubts here in code project. Im still in dilemma on several aspects of memory management.
Posted
Updated 2-Aug-16 1:04am
Comments
[no name] 2-Aug-16 8:05am    
Nice and simple eaxample/Explanation: http://www.dotnetperls.com/sqlconnection[^]
Priya-Kiko 2-Aug-16 8:15am    
Thank you so much. But then, even for the data tables used to load data into I think it has to be enclosed inside using() then what about their scope ?

1 solution

No. Just the SqlConnection connection will get disposed.
Given the SqlConnection is correct, any unmanaged resources it occupies will be available after the closing bracket. But anything outside the SqlConnection type is up to you to take care of.
 
Share this answer
 
Comments
Priya-Kiko 2-Aug-16 7:07am    
Thank you for the reply. That means 'command' has to be command.dispose() later on ?
Michael_Davies 2-Aug-16 7:11am    
You can have more than one variable in the using, adding command to the using will dispose it for you.
using (SqlConnection connection = new SqlConnection(
connectionString),
SqlCommand command = new SqlCommand(queryString, connection))
Priya-Kiko 2-Aug-16 7:32am    
Thank you for your reply.

In a situation like this where there are multiple statements next to next should i use it like this and what about the scope of the datatables. please help clarify. Thanks.

{
mconn.Open();
using (SqlCommand msqlcmd = new SqlCommand("select * from bmast", mconn),
SqlDataReader msqldat = msqlcmd.ExecuteReader(),
DataTable dtbmast = new DataTable(),
dtbmast.Load(msqldat),
msqldat.Close())

using (SqlCommand msqlcmd = new SqlCommand("select * from kmast", mconn),
SqlDataReader msqldat = msqlcmd.ExecuteReader(),
DataTable dtkmast = new DataTable(),
dtkmast.Load(msqldat),
msqldat.Close())
}


Michael_Davies 2-Aug-16 8:28am    
Not quite sure what you mean, however why dispose an object when you can re-use it and why are you putting execution statements in the using statement, very difficult to read easy to make mistakes, if it works.

What is the point of loading a table which is then disposed of almost immediately as the Using terminates.

If msqldat and msqlcmd were in one Using statement you can re-use them saving the time taken to dispose then initialise another instance of the same. For the reader, in their examples MS simply close a reader and do not dispose it.

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.close(v=vs.110).aspx

{
mconn.Open();
using (SqlCommand msqlcmd = new SqlCommand("select * from bmast", mconn),
DataTable dtbmast = new DataTable(),
DataTable dtkmast = new DataTable())
{
SqlDataReader msqldat = msqlcmd.executereader();
dtbmast.Load(msqldat);
msqldat.Close();

msqlcmd.commandtext = "select * from kmast";
msqldat = msqlcmd.ExecuteReader();
dtkmast.Load(msqldat);
msqldat.Close();
}
// Now the objects are disposed along with the data in the datatables...
mconn.close();
}
lukeer 9-Aug-16 2:56am    
I didn't know the comma syntax worked and never tried.
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900