zoukankan      html  css  js  c++  java
  • DataAdapter的Fill方法的连接是否会自动关闭的测试

    关于vs2005中dataadapter的fill方法的连接是否会自动关闭的测试

    此示例中所示的代码不显式打开和关闭 Connection。如果 Fill 方法发现连接尚未打开,它将隐式地打开 DataAdapter 正在使用的 Connection。如果 Fill 已打开连接,它还将在 Fill 完成时关闭连接。当处理单一操作(如 Fill 或 Update)时,这可以简化您的代码。但是,如果您在执行多项需要打开连接的操作,则可以通过以下方式提高应用程序的性能:显式调用 Connection 的 Open 方法,对数据源执行操作,然后调用 Connection 的 Close 方法。应尝试保持与数据源的连接打开的时间尽可能短,以便释放资源供其他客户端应用程序使用。

    最初我也相信dbdataadapter.fill 方法关联的connection对象是会自动打开和自动关闭的,可在一次检测后台进程时,
    发现我所有的连接都是显式关闭了(除了这个fill方法),但发现,oracle的进程并没有关闭。
    一开始以为是oracle的反应时间慢,可是我退出程序时,进程马上就没了,
    说明oracle关闭进程的速度是很快的,
    那就是fill方法的问题。

    好了,下面就开始讨论吧。

    在msdn中有一段话是关于dbdataadapter.fill 方法 (dataset)的说明:
    原文如下:

    fill 方法使用 select 语句从数据源中检索数据。
    与 select 命令关联的 idbconnection 对象必须有效,但不需要将其打开。
    如果调用 fill 之前 idbconnection 已关闭,

    则将其打开以检索数据,然后再将其关闭。
    如果调用 fill 之前连接已打开,它将保持打开状态。
    网址是:
    http://msdn2.microsoft.com/zh-cn/library/zxkb3c3d(vs.80).aspx
    这段话的意思很显然,
    1、在调用fill方法时,如果idbconnection 对象已经是打开的,那么,保持打开状态,
    在执行完fill方法后,再将其关闭。
    2、如果调用fill方法时,如果idbconnection 对象是关闭状态的,则将其打开以检索数据,然后再将其关闭。

    也就是说在调用fill方法时,是检索完数据后,就会自动关闭。

    下面测试说明并非如此,在执行检索数据后,并没有关闭。不知道是我的测试方法有问题,还是vs2005的bug。
    欢迎大家发言。
    一、下面的代码很简单,就是打开执行fill方法,然后看connection的状态:
    private void button2_click(object sender, eventargs e)
      {
          string cmdtext = "select * from bill_user";
    this.textbox1.clear();
          if (conn.state != connectionstate.open)
          {
        this.textbox1.appendtext("原连接是关闭的,现在打开连接成功");
        conn.open();
          }
          this.textbox1.appendtext("\n\r\n\r");
          system.data.oledb.oledbdataadapter adapter = new system.data.oledb.oledbdataadapter(cmdtext, this.conn);

          dataset ds = new dataset();
          adapter.fill(ds, "dt");
          if (this.conn.state != connectionstate.open)
          {
        this.textbox1.appendtext("执行完fill命令后的状态是关闭的");
          }
          else
          {
        this.textbox1.appendtext("执行完fill命令后的状态是打开的");
          }
          this.textbox1.appendtext("\n\r\n\r");
          this.timer1.start();
      }

    测试结果: connection对象并没有关闭。

    二、测试oracle多长时间会关闭fill方法打开的连接:

    using system;
    using system.collections.generic;
    using system.componentmodel;
    using system.data;
    using system.drawing;
    using system.text;
    using system.windows.forms;
    using system.data.oledb;

    namespace 测试进程
    {
        public partial class form1 : form
        {
      public form1()
      {
          initializecomponent();
      }
      oledbconnection conn = new oledbconnection(oracletask.orahelper.conn_string_non_dtc);
      /// <summary>
      /// 测试连接多长时间oracle会自动释放
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void button1_click(object sender, eventargs e)
      {
          conn.open();
          this.textbox1.clear();
          this.textbox1.appendtext("打开连接成功");
          this.timer1.start();
         // messagebox.show("");
         // conn.close();

      }

      private void timer1_tick(object sender, eventargs e)
      {
          this.textbox1.appendtext("\n\r\n\r");
          if (this.conn.state == connectionstate.open)
          {
        this.textbox1.appendtext("连接打开"+system.datetime.now.tolongtimestring());
          }
          else
          {
        this.textbox1.appendtext("连接关闭" + system.datetime.now.tolongtimestring());
        this.timer1.stop();
          }

      }

      private void button2_click(object sender, eventargs e)
      {
          string cmdtext = "select * from bill_user";
    this.textbox1.clear();
          if (conn.state != connectionstate.open)
          {
        this.textbox1.appendtext("原连接是关闭的,现在打开连接成功");
        conn.open();
          }
          this.textbox1.appendtext("\n\r\n\r");
          system.data.oledb.oledbdataadapter adapter = new system.data.oledb.oledbdataadapter(cmdtext, this.conn);

          dataset ds = new dataset();
          adapter.fill(ds, "dt");
          if (this.conn.state != connectionstate.open)
          {
        this.textbox1.appendtext("执行完fill命令后的状态是关闭的");
          }
          else
          {
       // this.conn.close();
        this.textbox1.appendtext("执行完fill命令后的状态是打开的");
          }
          this.textbox1.appendtext("\n\r\n\r");
          this.timer1.start();
      }

        }
    }
    在上面的基础上添加一个timer控件,看fill打开打开的连接多长时间能关闭。

    测试结果:

    10分钟后,连接仍是打开的。

    本次测试环境:oracle 817+windows xp

    http://msdn.microsoft.com/zh-cn/library/bh8kx08z(VS.80).aspx

    http://www.dnbcw.com/biancheng/c/CLXO22738.html

    http://hi.baidu.com/zhou_hs/blog/item/115e96ee83e303e7ce1b3e1b.html

  • 相关阅读:
    升级linux bash
    vim关键字自动补全
    linux bash shell之变量替换::=句法、=句法、:句法、句法、=?句法、?句法、:+句法、+句法
    使用Bash编写Linux Shell脚本7.复合命令
    使用Bash编写Linux Shell脚本5.变量
    使用Bash编写Linux Shell脚本8.调试和版本控制
    Perl之单引号\双引号的字符串直接量
    linux bash shell 中的单引号和双引号
    使用Bash编写Linux Shell脚本6.表达式
    Vim的行号、语法显示等设置,即.vimrc文件的配置
  • 原文地址:https://www.cnblogs.com/emanlee/p/1580175.html
Copyright © 2011-2022 走看看