日期:2014-05-17  浏览次数:20470 次

数据库关闭另一种方法
原来一直用.close(),今天发新一个新的方法就是using

在尝试的过程中发现一个疑惑
就是查寻数据的时候例子这样写的
C# code

            DataTable dt = new DataTable();
            cmd = new SqlCommand(sql, Getconn());
            using (str = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {

                dt.Load(str);
            }
           
            return dt;



我自己疑惑using里面的内容,试着改写了一下

C# code

            DataTable dt = new DataTable();

            using (cmd = new SqlCommand(sql, Getconn()))
           {
                str = cmd.ExecuteReader();
                dt.Load(str);
            }
           return dt;



测试的结果是一样都能实现查询的功能,哪位给我讲讲上面那样写的作用是什么,而我下面这样写数据库究竟关闭了没

------解决方案--------------------
using的作用是释放非托管资源