使用using释放资源

理解using的作用

使用Connection,如何保证连接关闭

以前我们使用的是try…catch…final的方式,将数据库连接语句放在里面,在final语句块中写关闭语句。

使用using释放Connection对象

  • using的用法一:导入命名空间
  • 释放对象占用的资源,比如:Connection、DataReader、FileStream 等等
    写法:

using(类型 对象名 = new 类型(参数列表))

//…
//…

大括号是声明using的有效范围,括号内是对象的适用范围,一旦出了这个使用范围,程序会自动释放程序内部对象。

using(SqlConnection conn = new SqlConnection(connString))
{
conn.open();
//......
}

不需要显式调用Close()方法。
例子如下:

#region bage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
#endregion
namespace CH03
{
    class Program
    {
        static void Main(string[] args)
        {
            string sql = "select count(*) from dbo.Grade";
            string conStr = "server=.;database=MySchool;uid=sa;pwd=123456";
            int count = 0;
            SqlConnection con = null;

            using( con = new SqlConnection(conStr))
            {
               
                con.Open();
                SqlCommand cmd = new SqlCommand(sql,con);
                 count = Convert.ToInt32(cmd.ExecuteScalar());
                //con.Close();
            }
            Console.WriteLine(con.State);//观看执行状态是否关闭
            Console.WriteLine(count);
            Console.ReadLine();
        
        }
    }
}

使用using释放SqlDataReader

using(sqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
while(reader.Read())
{
//遍历查询结果
}
}

CommandBehavior.CloseConnection 是保证DataReader释放后自动释放Connection。
例子如下:

#region bage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
#endregion
namespace CH03
{
    class Program
    {
        static void Main(string[] args)
        {
            //string sql = "select count(*) from dbo.Grade";
            string sql = "select * from dbo.Grade";
            string conStr = "server=.;database=MySchool;uid=sa;pwd=123456";
           // int count = 0;
            SqlConnection con = new SqlConnection(conStr);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql,con);
            using(SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))//顺便关闭SqlConnection对象
            {
                while (reader.Read())
                {
                    Console.WriteLine(reader[0]+"   "+reader[1]);
                }
            }
            //using( con = new SqlConnection(conStr))
            //{
               
            //    con.Open();
            //    SqlCommand cmd = new SqlCommand(sql,con);
            //     count = Convert.ToInt32(cmd.ExecuteScalar());
            //    //con.Close();
            //}
            Console.WriteLine(con.State);//观看执行状态是否关闭
           // Console.WriteLine(count);
            Console.ReadLine();
        
        }
    }
}

using的本质

相当于在try-catch-finally的finally块中调用了对象名.Dispose()方法。

  • Dispose()是IDisable接口的方法
  • Dispose()专用于释放对象稀缺资源
  • object类实现了Dispose(),部分类实现了Close()
  • Close()封装了对Dispose()的调用

掌握使用using释放资源对象

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值