C# - closing Sql objects best practice

前端 未结 8 988
陌清茗
陌清茗 2020-12-31 11:29

If you have a C# function with Sqlaccess, is it mandatory to close all objects/handles, or is everything cleaned up automatically once you exit the function

For exam

相关标签:
8条回答
  • 2020-12-31 11:58

    You should close the SqlConnection object as soon as you're done with it. If you don't then the connection will remain open, and will not be available to handle other requests.

    The using statement is useful for this. It will call Dispose() on the object for you:

    using (SqlConnection cn = new SqlConnection(connectionString))
    {   
        SqlCommand cm = new SqlCommand(commandString, cn)
        cn.Open();
        cm.ExecuteNonQuery();       
    }
    
    0 讨论(0)
  • 2020-12-31 11:59

    You don't need to have a separate using statement for the SqlDataReader (as well as one using statement for the connection) unless you plan do perform other operations with the connection after the SqlDataReader has fully read through the row set.

    If you are just opening a connection, reading some data using the reader, and then closing the connection, then one using statement for the entire block of code (surrounding the connection) will suffice as the garbage collector will clean up all resources tied to the connection that is disposed by the first using statement.

    Anyway, here's a good article that describes it all...

    0 讨论(0)
  • 2020-12-31 11:59

    All three classes have a Dispose() method. Mandatory is too strong, but definitely highly recommended you use the using keyword so Dispose() is automatically called. Failing to do so makes your program run "heavy", using more system resources than necessary. And outright failure when you don't use the "new" keyword enough to trigger the garbage collector.

    0 讨论(0)
  • 2020-12-31 12:02

    Any class handling SQL stuff like Connections should implement the IDisposable interface as stated by Microsoft .NET coding guidelines.

    Thus, you should probably close and dispose your connection in your Dispose method.

    0 讨论(0)
  • 2020-12-31 12:04

    Calling Close on the SQL connection won't actually close it, but will return it to a connection pool to be reused, improving performance.

    Additionally it is generally poor practice to not explicitly dispose of unmanaged resources when you are finished with them (asap).

    0 讨论(0)
  • 2020-12-31 12:06

    Explicit disposing in the finally statement is another approach, although the using statement is a much better solution. It produces a bit more code, but demonstrates the goal...

    SqlConnection conn = null;
    try
    {
        //create connection
    
        SqlCommand cmd = null;
        try
        {
            //create command
    
            SqlDataReader reader = null;
            try 
            {
                //create reader
            }
            finally
            {
                reader.Dispose();
            }
        }
        finally
        {
            cmd.Dispose();
        }
    }
    finally 
    {
        conn.Dispose();
    }
    
    0 讨论(0)
提交回复
热议问题