Close SqlConnection in the Finally when using “Using”

前端 未结 6 612
独厮守ぢ
独厮守ぢ 2020-12-21 00:06

I want to close the SqlConnection in the Finally since the using not really close it and the connection pool gets full. but I don\'t realize what\'s the right way to fo that

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-21 00:50

    using (var conn = new SqlConnection(_dbconnstr)) 
    {
        //code
    }
    

    is expaded to:

    SqlConnection conn = new SqlConnection(_dbconnstr);
    try
    {
        //code
    }
    finally
    {
        conn.Dispose();
    }
    

    So you should handle errors but you can forget about closing connection.

提交回复
热议问题