浅谈CommandBehavior枚举的独特之处

时间:2023-03-09 15:53:40
浅谈CommandBehavior枚举的独特之处

提供对查询结果和查询对数据库的影响

此枚举有一个 FlagsAttribute 属性,通过该属性可使其成员值按位组合。

命名空间:  System.Data
程序集:
 System.Data(在 System.Data.dll 中)

语法:

[FlagsAttribute]
public enum CommandBehavior成员名称及说明:

Default:此查询可能返回多个结果集。执行查询可能会影响数据库状态。Default 不设置 CommandBehavior 标志,因此调用 ExecuteReader(CommandBehavior.Default) 在功能上等效于调用 ExecuteReader()。

SingleResult:查询返回一个结果集。

SchemaOnly:查询仅返回列信息。当使用 SchemaOnly 时,用于 SQL Server 的 .NET Framework 数据提供程序将在要执行的语句前加上 SET FMTONLY ON。

KeyInfo:此查询返回列和主键信息。

性能小常识:SingleRow:查询应返回一行。执行查询可能会影响数据库的状态。一些 .NET Framework 数据提供程序可能(但不要求)使用此信息来优化命令的性能。用 OleDbCommand 对象的 ExecuteReader 方法指定 SingleRow 时,用于 OLE DB 的 .NET Framework 数据提供程序使用 OLE DB IRow 接口(如果可用)执行绑定。否则,它使用 IRowset 接口。如果您的 SQL 语句应该只返回一行,则指定 SingleRow 还可以提高应用程序性能。在执行返回多个结果集的查询时,可以指定 SingleRow。在这种情况下,仍返回多个结果集,但每个结果集只有一行。

SequentialAccess:提供一种方法,以便 DataReader 处理包含带有大二进制值的列的行。SequentialAccess 不是加载整行,而是使 DataReader 将数据作为流来加载。然后可以使用 GetBytes 或 GetChars 方法来指定开始读取操作的字节位置以及正在返回的数据的有限的缓冲区大小。

CloseConnection:在执行该命令时,如果关闭关联的 DataReader 对象,则关联的 Connection 对象也将关闭。

备注:

其用在ExecuteReader(c)中,返回对象前不能关闭数据库连接,须用CommandBehavior.CloseConnection;

这是一个关于实际知识点的问题,面试官考查的是应聘者数据库访问的编程经验。本节将针对这个问题展开具体的分析。对于此类关于具体知识点的问题,读者在平时应该注意积累,这样在面试中才能从容应答。

所涉及的知识点

代码如下:

public enum CommandBehavior
{
      // Fields
      CloseConnection = 0x20,
      Default = ,
      KeyInfo = ,
      SchemaOnly = ,
      SequentialAccess = 0x10,
      SingleResult = ,
      SingleRow = 

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对象关闭时自动关闭。