ADO.NET使用using关闭数据库连接

   using (SqlConnection conn = new SqlConnection(source))
            {
                // open the connoction
                conn.Open();
                // Do something usefu△
                // Close it myself
                conn.Close();
            }

  因为using子句将确保在任何情况下都执行关闭操作。但是,应确保像这样的任何资源尽可能早地释放。因为在块的其余部分可能有更多的代码,而没有必要锁定资源。

posted on 2017-07-26 15:26  dotNet修行之路  阅读(2287)  评论(2编辑  收藏  举报