【发布时间】:2016-02-18 14:01:33
【问题描述】:
当您手动管理数据库连接时,您总是打开和关闭它。有时您需要在执行某些操作之前检查连接是否具有某种状态。 一个经典的情况是在关闭连接之前检查未关闭状态。 类似的东西
if (connection.State != ConnectionState.Closed)
connnection.Close();
正如MSDN 所说,ConnectionState 是带标志的枚举。这意味着连接状态可以同时具有不同的状态。可能是 Broken+Closed 或其他...
如果你反编译 System.Data.ConnectionState 枚举,你会看到
[Flags]
public enum ConnectionState
{
Closed = 0,
Open = 1,
Connecting = 2,
Executing = 4,
Fetching = 8,
Broken = 16,
}
已关闭项目的值为零。这意味着以下总是正确的:
connection.State.HasFlag(ConnectionState.Closed)
所以。有什么建议为什么这个枚举有 Flags 属性?或者(如果这个枚举必须是 Flags)为什么 Closed 项的值为 0?
【问题讨论】:
标签: c# .net ado.net database-connection