CommandBehavior.CloseConnection有何作用
其用在ExecuteReader(c)中,返回对象前不能关闭数据库连接,须用CommandBehavior.CloseConnection;
这是一个关于实际知识点的问题,面试官考查的是应聘者数据库访问的编程经验。本节将针对这个问题展开具体的分析。对于此类关于具体知识点的问题,读者在平时应该注意积累,这样在面试中才能从容应答。
所涉及的知识点
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有何作用的更多相关文章
- C#使用SqlDataReader读取数据库数据时CommandBehavior.CloseConnection参数的作用
主要用在ExecuteReader(c)中,如果想要返回对象前不关闭数据库连接,须要用CommandBehavior.CloseConnection: CloseConnection解决了流读取数据模 ...
- CommandBehavior.CloseConnection
cmd.commandTimeout设置为了1秒,sql执行了很长时间还没有超时, cmd.ExecuteReader(CommandBehavior.CloseConnection)这样就会立马重现 ...
- cmd.ExecuteReader(CommandBehavior.CloseConnection)
有些开发人员坚持认为,如果您设置 CommandBehavior.CloseConnection 选项,则 DataReader 及其相关联的连接会在 DataReader 完成数据读取时自动关闭.这 ...
- CommandBehavior.CloseConnection的使用
CommandBehavior.CloseConnection的使用 分析问题 由于流模式读取数据库的特点,在具体应用时很难确定数据库连接何时才能被关闭,因为读取的动作是连续进行的,下面是一个常见的数 ...
- CommandBehavior.CloseConnection使用
其用在ExecuteReader(c)中,返回对象前不能关闭数据库连接,须用CommandBehavior.CloseConnection: 这是一个关于实际知识点的问题,面试官考查的是应聘者数据库访 ...
- C# / MSSQL / WinForm / ASP.NET - SQLHelper中返回SqlDataReader数据
/// <summary> /// Execute a SqlCommand that returns a resultset against the database specified ...
- .NET基础 (20).NET中的数据库开发
ADO NET和数据库程序基础1 什么是关系型数据库2 如何通过SQL语句来实现行列转换3 ADO NET支持哪几种数据源 ADO NET和数据库的连接1 请简要叙述数据库连接池的机制2 如何提高连接 ...
- 浅谈CommandBehavior枚举的独特之处
提供对查询结果和查询对数据库的影响 此枚举有一个 FlagsAttribute 属性,通过该属性可使其成员值按位组合. 命名空间: System.Data程序集: System.Data(在 Sy ...
- 数据库助手类 DBHelper
using System; using System.Collections.Generic; using System.Text; using System.Configuration; using ...
随机推荐
- SpringMVC源码解读 - RequestMapping注解实现解读 - RequestMappingInfo
使用@RequestMapping注解时,配置的信息最后都设置到了RequestMappingInfo中. RequestMappingInfo封装了PatternsRequestCondition, ...
- 说一下自己对于 Linux 哲学的理解
查阅了一些资料,官方的哲学思想貌似是: 一切皆文件 由众多单一目的的小程序,一个程序只实现一个功能,多个程序组合完成复杂任务 文本文件保存配置信息 尽量避免与用户交互 什么,你问我的理解?哲学思想?E ...
- zookeeper学习及安装
HBase提示已创建表,但是list查询时,却显示表不存在. https://blog.csdn.net/liu16659/article/details/80216085 http://archiv ...
- mysql复制表以及复制数据库
(一)将旧表复制到新表 1.CREATE TABLE新表 SELECT* FROM旧表; 该语句只是复制表结构以及数据,它不会复制与表关联的其他数据库对象,如索引,主键约束,外键约束,触发器等. CR ...
- linux安装memcache及memcache扩展
一.安装libevent# wget http://www.monkey.org/~provos/libevent-2.0.12-stable.tar.gz# tar zxf libevent-2.0 ...
- IIS应用程序池自动回收作业
vb: appPoolName = WScript.Arguments() Set oWebAdmin = GetObject("winmgmts:root\WebAdministratio ...
- Gamma Correction of OIIO
\apiitem{"oiio:ColorSpace" : string} The name of the color space of the color channels. ...
- OI知识点|NOIP考点|省选考点|教程与学习笔记合集
点亮技能树行动-- 本篇blog按照分类将网上写的OI知识点归纳了一下,然后会附上蒟蒻我的学习笔记或者是我认为写的不错的专题博客qwqwqwq(好吧,其实已经咕咕咕了...) 基础算法 贪心 枚举 分 ...
- WebStrom创建自定义后缀名的文件
如下图所示,我在下面的项目里面创建了.ejs文件,但是正常的情况下我们WebStrom是无法创建.ejs文件的,那么我们怎么才能让我们的WebStrom拥有创建该后缀名的文件的能力呢? 不BB,直接看 ...
- [BZOJ5212][ZJOI2018]历史
传送门(洛谷) 人生第一道九条可怜……神仙操作…… 看着题解理解了一个早上才勉强看懂怎么回事…… 简化一下题目就是:已知每一个点access的总次数,求一个顺序使虚实边转化的次数最多 考虑一下,对于x ...