关于VS2005中DataAdapter的Fill方法的连接是否会自动关闭的测试
更多文章参见:
http://hi.csdn.net/xjzdr/profile
最初我也相信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