ado.net Closing Connection when using “using” statement

后端 未结 7 684
青春惊慌失措
青春惊慌失措 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 07:52

    Yes your code will close the connection, however that typcally means release back to the connection pool to be truely closed later.

    If you execute this snippet of code, and then do an sp_who and observe that your connection is still there, that would be why.

    If you absolutely need the connection truely closed (an edge case to be sure) then use the ClearAllPools static method of ths SqlConnection

提交回复
热议问题