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的使用

时间: 2024-07-30 10:06:34

CommandBehavior.CloseConnection的使用的相关文章

CommandBehavior.CloseConnection有何作用

[原文地址:http://www.cnblogs.com/agian/articles/1648835.html] 其用在ExecuteReader(c)中,返回对象前不能关闭数据库连接,须用CommandBehavior.CloseConnection: 这是一个关于实际知识点的问题,面试官考查的是应聘者数据库访问的编程经验.本节将针对这个问题展开具体的分析.对于此类关于具体知识点的问题,读者在平时应该注意积累,这样在面试中才能从容应答. 所涉及的知识点 CommandBehavior.Clo

C#使用SqlDataReader读取数据库数据时CommandBehavior.CloseConnection参数的作用

主要用在ExecuteReader(c)中,如果想要返回对象前不关闭数据库连接,须要用CommandBehavior.CloseConnection: CloseConnection解决了流读取数据模式下,数据库连接不能有效关闭的情况.当某个XXXDataReader对象在生成时使用了CommandBehavior.CloseConnection,那数据库连接将在XXXDataReader对象关闭时自动关闭. 由于流模式读取数据库的特点,在具体应用时很难确定数据库连接何时才能被关闭,因为读取的动

CommandBehavior.CloseConnection使用

其用在ExecuteReader(c)中,返回对象前不能关闭数据库连接,须用CommandBehavior.CloseConnection: 这是一个关于实际知识点的问题,面试官考查的是应聘者数据库访问的编程经验.本节将针对这个问题展开具体的分析.对于此类关于具体知识点的问题,读者在平时应该注意积累,这样在面试中才能从容应答. 所涉及的知识点 CommandBehavior.CloseConnection的使用 分析问题 由于流模式读取数据库的特点,在具体应用时很难确定数据库连接何时才能被关闭,

记分员

1.计划   计划时间 实际完成时间 估计任务需要时间 7天   需求分析 10min   生成设计文档 30min   设计复审 1h   代码规范 20min   具体设计 3h   具体编码 5day   代码复审 1h   测试 1h   测试报告 30min   计算工作量 30min   事后总结并提出过程改进计划     2. 开发   需求分析      作为一名现场记分员,我希望详细记录比赛现场比分增长情况,以便观众及运动员.教练员及时掌握比赛状况.    生成设计文档    

【2016-11-2】【坚持学习】【Day17】【微软 推出的SQLHelper】

从网络上找到 微软原版本的SQLHelper,很多行代码.认真看了,学习了. 代码: 1 using System; 2 using System.Data; 3 using System.Xml; 4 using System.Data.SqlClient; 5 using System.Collections; 6 7 namespace Helper 8 { 9 /// <summary> 10 /// The SqlHelper class is intended to encapsu

【SQLServer】DBHelper即C#数据库底层封装

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.Data.SqlClient; using System.Data; namespace MYHelper { //不加public,只有dal私有引用: //封装执行sql server增删改查辅助类,提供相应执行sql语句和存储过程的方法 /

巩固ADO.NET

回想一年半前学数据库的时候是找的培训视频看的,大概10天视频(有一半多时间关于数据库操作的winform)的内容看完后数据库这块就没有了,还以为自己学的挺好,现在想想真是傻的可爱.虽然以ADO.NET为基础的EF挺好用的,但抽时间好好巩固下基础我觉得很重要.不废话了,ADO.NET提供对SQL.Oracle和MySql等数据库的访问操作,它有2个重要的组成部分:.NET数据提供程序和数据集DataSet. 1.什么是ADO.NET .NET数据提供程序就是一个类集,本质的功能就是用于连接数据库.

Access中的SELECT @@IDENTITY

在Access数据库中存在select @@identity吗?答案是肯定的.但是Access一次只能执行一条SQL,多条SQL需要多次执行,这是限制.在SQL Server中,可以一次执行多条SQL语句.Access使用的是Jet-SQL,SQL Server使用的是T-SQL,两者用法上相差很大. 但是Access中可以连续执行N条语句,象下面这样:   cmd.CommandText = "INSERT INTO MyTable (N1,N2) VALUES (22,11)"; 

最近几天的搜集

//获取或设置一个值,该值指示参数是只可输入.只可输出.双向还是存储过程返回值参数.默认为input//parameter.Value == null---其实实现的是,C#中的Null和数据库中的DBNull相对应 if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null