ado.net Closing Connection when using “using” statement

后端 未结 7 720
青春惊慌失措
青春惊慌失措 2020-11-27 07:46

I am doing my database access methods to SQL Server like this

  using (SqlConnection con = new SqlConnection(//connection string)
  {
    using (SqlCommand c         


        
7条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 08:17

    The using will take care of it for you. Under the hood, SqlConnection.Dispose() calls the SqlConnection.Close() method, and SqlCommand.Dispose() calls SqlCommand.Close().

    As additional background, a using statement is syntactic sugar for a try ... finally that disposes the IDisposable object in the finally.

提交回复
热议问题