关于Command.ExecuteReader(CommandBehavior.CloseConnection)的问题
这个参数是关闭Connection连接嘛?
但是我测试过Connection通过这语句后还是打开的.
这个参数到底是用来关闭什么的?我测过不是关闭Connection对象的
问题点数:10、回复次数:2Top
1 楼net_lover(【孟子E章】)回复于 2006-05-03 20:21:32 得分 0
如果关闭关联的 DataReader 对象,则关联的 Connection 对象也将关闭。
你应该关闭datareader后测试Top
2 楼namhyuk(namhyuk)回复于 2006-05-04 13:55:27 得分 0
CloseConnection: When the command is done executing, both the DataReader and the connection are closed.
不知道楼主是怎么测试的。
举个例子。
System.Collections.ArrayList al = new System.Collections.ArrayList();
using (OracleConnection conn = new OracleConnection())
{
conn.ConnectionString = "Data Source=**;User ID=**;Password=**";
OracleCommand cmd = new OracleCommand();
cmd.CommandText = "select * from TABLE where ***";
cmd.Connection = conn;
conn.Open();
OracleDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.HasRows)
{
foreach (System.Data.Common.DbDataRecord ddr in dr)
{
al.Add(ddr);
}
}
MessageBox.Show(conn.State.ToString()); //这里输出Closed,说明连接已经关闭
} // 自动调用conn.Dispose()。
dataGridView1.DataSource = al;
楼主能不能把你的测试代码贴出来?Top




