CommandBehavior.CloseConnection的使用
分析问题
由于流模式读取数据库的特点,在具体应用时很难确定数据库连接何时才能被关闭,因为读取的动作是连续进行的,下面是一个常见的数据访问层的静态方法:
/// <summary>
/// 常见的获取SqlDataReader方法
/// 通常的数据访问层都会提供这个方法
/// </summary>
static SqlDataReader GetReader()
{
//通过连接字符串获取连接
SqlConnection con = new SqlConnection(conn_String);
try
{
//打开连接,执行查询
//并且返回SqlDataReader
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = Sql;
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}
finally
{
//这里的代码处于两难的境地
//如果这里执行关闭:con.Close();那返回的
SqlDataReader将毫无用处,因为其
//依赖的连接已经关闭
//如果这里不执行con.Close();那返回后该连接
将永远无法关闭,因为调用方无法
//得到连接对象
}
}
正如代码注释里描述的那样,这样的方法既不能关闭连接,也不能保持连接打开状态。很多系统为了解决这样两难的境地,只能放弃使用Reader模式的数据源,或者把连接对象交给方法调用者,以便进行关闭。
而CommandBehavior.CloseConnection的功能恰好就是为了避免类似的尴尬境地,它能够保证当
SqlDataReader对象被关闭时,其依赖的连接也会被自动关闭。代码9-2展示了使用
CommandBehavior.CloseConnection和不使用CommandBehavior.CloseConnection的区别。
这里以SqlDataReader为例进行说明,对于其他命名空间下的XXXDataReader对象,其功能是类似的。
首先为了展示功能,代码9-2包含了两个静态的返回SqlDataReader的方法,其中一个在执行ExecuteReader方法时传入了CommandBehavior.CloseConnection方法。
代码9-2 使用CommandBehavior.CloseConnection:UseCommandBehavior.cs
partial class UseCommandBehavior
{
//数据库看连接字符串
const String conn_String =
"Server=localhost;Integrated Security=true;database=NetTest";
const String Sql = "select * from dbo.DepartCost";
/// <summary>
/// 使用CommandBehavior.CloseConnection
/// </summary>
/// <param name="con">为了测试需要,传入连接对象</param>
static SqlDataReader GetReader_CloseConnection(SqlConnection con)
{
try
{
//打开连接,执行查询
//并且返回SqlDataReader
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = Sql;
SqlDataReader dr = cmd.ExecuteReader
(CommandBehavior.CloseConnection);
return dr;
}
finally
{
//因为使用了CommandBehavior.CloseConnection,
//这里不需要关闭连接
//con.Close();
}
}
/// <summary>
/// 不使用CommandBehavior.CloseConnection
/// </summary>
/// <param name="con">为了测试需要,传入连接对象</param>
static SqlDataReader GetReader_NoCloseConnection(SqlConnection con)
{
try
{
//打开连接,执行查询
//并且返回SqlDataReader
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = Sql;
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}
finally
{
//为了使返回的SqlDataReader可用,这里不能关闭连接
//con.Close();
}
}
}
可以看到,无论是否使用CommandBehavior.CloseConnection,两个方法都没有在最终关闭连接,但是它们不关闭
连接的原因并不相同。准备好了两个方法之后,就从主方法中分别调用这两个方法来进行测试,以查看从使用了
CommandBehavior.CloseConnection的方法中返回的SqlDataReader对象是否在关闭的同时自动关闭连接,如代码
9-3所示。
代码9-3 使用CommandBehavior.CloseConnection:UseCommandBehavior.cs
partial class UseCommandBehavior
{
/// <summary>
/// 测试方法
/// </summary>
static void Main(string[] args)
{
//建立连接
SqlConnection con = new SqlConnection(conn_String);
try
{
//测试使用了CommandBehavior.CloseConnection的方法
Console.WriteLine("测试使用了CommandBehavior.
CloseConnection的方法:");
SqlDataReader sdr = GetReader_CloseConnection(con);
while (sdr.Read()) { }
sdr.Close();
Console.WriteLine("读取完毕后的连接状态:" + con.State.ToString());
//测试没有使用CommandBehavior.CloseConnection的方法
Console.WriteLine("测试没有使用CommandBehavior.
CloseConnection的方法:");
SqlDataReader sdr1 = GetReader_NoCloseConnection(con);
while (sdr1.Read()) { }
sdr1.Close();
Console.WriteLine("读取完毕后的连接状态:" +
con.State.ToString());
Console.Read();
}
finally
{
//确保连接被关闭
if (con.State != ConnectionState.Closed)
con.Close();
}
}
}
下面是代码的执行结果:
测试使用了CommandBehavior.CloseConnection的方法:
读取完毕后的连接状态:Closed
测试没有使用CommandBehavior.CloseConnection的方法:
读取完毕后的连接状态:Open
正如读者所看到的,使用了CommandBehavior.CloseConnection得到的SqlDataReader对象,在关闭的同时会自动地关闭其依赖的数据库连接对象,这个特性解决了数据访问层编写中的困境。
答案
CommandBehavior.CloseConnection解决了流读取数据模式下,数据库连接不能有效关闭的情况。当某个
XXXDataReader对象在生成时使用了CommandBehavior.CloseConnection,那数据库连接将在
XXXDataReader对象关闭时自动关闭。
CommandBehavior.CloseConnection的使用的更多相关文章
- CommandBehavior.CloseConnection
cmd.commandTimeout设置为了1秒,sql执行了很长时间还没有超时, cmd.ExecuteReader(CommandBehavior.CloseConnection)这样就会立马重现 ...
- cmd.ExecuteReader(CommandBehavior.CloseConnection)
有些开发人员坚持认为,如果您设置 CommandBehavior.CloseConnection 选项,则 DataReader 及其相关联的连接会在 DataReader 完成数据读取时自动关闭.这 ...
- C#使用SqlDataReader读取数据库数据时CommandBehavior.CloseConnection参数的作用
主要用在ExecuteReader(c)中,如果想要返回对象前不关闭数据库连接,须要用CommandBehavior.CloseConnection: CloseConnection解决了流读取数据模 ...
- CommandBehavior.CloseConnection有何作用
其用在ExecuteReader(c)中,返回对象前不能关闭数据库连接,须用CommandBehavior.CloseConnection: 这是一个关于实际知识点的问题,面试官考查的是应聘者数据库访 ...
- CommandBehavior.CloseConnection使用
其用在ExecuteReader(c)中,返回对象前不能关闭数据库连接,须用CommandBehavior.CloseConnection: 这是一个关于实际知识点的问题,面试官考查的是应聘者数据库访 ...
- 浅谈CommandBehavior枚举的独特之处
提供对查询结果和查询对数据库的影响 此枚举有一个 FlagsAttribute 属性,通过该属性可使其成员值按位组合. 命名空间: System.Data程序集: System.Data(在 Sy ...
- Microsoft Visual Studio 2017 for Mac Preview 下载+安装+案例Demo
目录: 0. 前言 1. 在线安装器 2. 安装VS 3. HelloWorld 4. ASP.NET MVC 5. 软件下载 6. 结尾 0. 前言: 工作原因,上下班背着我的雷神,一个月瘦了10斤 ...
- 完美解决CodeSmith无法获取MySQL表及列Description说明注释的方案
问题描述: CodeSmith是现在比较实用的代码生成器,但是我们发现一个问题: 使用CodeSmith编写MySQL模板的时候,会发现一个问题:MySQL数据表中的列说明获取不到,也就是column ...
- .NET基础拾遗(6)ADO.NET与数据库开发基础
Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开发基 ...
随机推荐
- 关于tcc、tlink的编译链接机制的研究
1.学习过程 在c:\下建立文件夹c,并将编译器tcc.exe.连接器tlink.exe.相关文件c0s.obj.cs.lib.emu.lib.maths.lib放入文件夹中. 要搭建一个简单的C语言 ...
- Delphi 自带的 Base64 编解码函数
今天帮别人解决一个关于 Base64 编解码的问题,竟然发现 Delphi 自带了 Base64 编解码的单元,叫 EncdDecd,这名字很拗口而且不直观,估计这是一直很少人关注和知道的原因. 这个 ...
- Printing Architecture
Printing Architecture http://www.codeproject.com/Articles/8916/Printing-Architecture This articl ...
- SignalR 的跨域支持
How to establish a cross-domain connection Typically if the browser loads a page from http://contoso ...
- SmartBusinessDevFramework架构设计-1:结构简介
SmartBusinessDevFramework 简介 基于.net 4.0 开发的企业级系统框架 功能 1 自定义ORM.如果客官喜欢NHibernate EntityFramework ,并对其 ...
- buffer busy wait
什么是buffer busy wait? A session that reads or modifies a buffer in the SGA must first acquire the cac ...
- 获取SQL段的执行时间
对SQL进行优化 经常会需要知道这条SQL语句执行的时间,这里介绍我的一种常用做法 DECLARE @d DATETIME SET @d=GETDATE() --do something --for ...
- HDU-1176(基础方程DP)
Problem Description 都 说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉, 就掉落在他 ...
- zookeeper[2] zookeeper原理(转)
转自:http://cailin.iteye.com/blog/2014486 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现 ...
- Struts+Spring搭建
前言 本文以Tomcat为j2ee容器,数据库为Sqlserver2005进行说明.Struts版本为2.3.15.3,Spring版本为3.2.5 Spring简介 Spring也是appache下 ...