zoukankan      html  css  js  c++  java
  • ConnectionState详解

    ConnectionState有六个属性值
    ConnectionState.Broken;与数据源连接断开。只有在连接打开后才有可能发生这种情况。可以关闭处于这种状态下的连接,然后重新打开。
    ConnectionState.Closed;连接处于关闭状态。
    ConnectionState.Connecting;连接对象正在与数据源连接。
    ConnectionState.Executing;连接对象正在执行命令。
    ConnectionState.Fetching;连接对象正在检索数据。
    ConnectionState.Open;连接处于打开状态
    通常我们会常用到Open,Broken和Close去进行状态的判断。
    例如:
    一般建立连接的方式如下:

    private static SqlConnection connection;
            public static SqlConnection Connection
            {
                get
                {                 
                    if (connection == null)
                    {
                        string connectionString = GetconnStr();
                        connection = new SqlConnection(connectionString);
                        connection.Open();
                    }
                    else if (connection.State == System.Data.ConnectionState.Closed)
                    {
                        connection.Open();
                    }
                    else if (connection.State == System.Data.ConnectionState.Broken)
                    {
                        connection.Close();
                        connection.Open();
                    }
                    return connection;
                }
            }


    关闭连接:
    public void Dispose()
    {
              if (connection.State == ConnectionState.Open || connection.State == ConnectionState.Broken)
              connection.Close();
    }
    ---------------------
    作者:风林山火
    来源:CSDN
    原文:https://blog.csdn.net/zhaoleiwang/article/details/9851041
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    memcache 应用场景
    如何写接口文档(登录)
    PHP常见错误级别及错误码
    ex33 while 循环
    ex32 循环和列表
    ex31--作出决定
    ex29-30 if,elif and else.
    ex28 布尔表达式练习
    ex25 更多更多的实践
    ex21 函数可以返回某些东西
  • 原文地址:https://www.cnblogs.com/asdyzh/p/9983901.html
Copyright © 2011-2022 走看看