一个Reader的类,在那里Conn.close()啊

No001 2010-03-17 04:26:22
public OleDbDataReader getDr(string StrText)
{
OleDbConnection Conn = getConn();
Conn.Open();
OleDbCommand Comm = new OleDbCommand(StrText, Conn);
OleDbDataReader Dr = Comm.ExecuteReader();
return Dr;
}

现在这样的话,经常会出现80004005 未指定的错误,测试发现是Conn没有关闭造成的。

可是不知道在哪里进行Conn.Close();

请大侠指教指教
...全文
148 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dobzhansky 2010-03-18
  • 打赏
  • 举报
回复
reader 她不是一个人
WoooXVi 2010-03-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sandy945 的回复:]
Comm.ExecuteReader();

=》

Comm.ExecuteReader(CommandBehavior.CloseConnection);
[/Quote]

使用这个的话,Reader关闭了Connection也随之关闭
暗淡亮点 2010-03-17
  • 打赏
  • 举报
回复
需要在return之前把它关闭。
Alden 2010-03-17
  • 打赏
  • 举报
回复
OleDbDataReader 是一个连接状态的对象,当你读完了数据后,就可以关闭了,你的方法需要修改.
wuyq11 2010-03-17
  • 打赏
  • 举报
回复
Comm.ExecuteReader(CommandBehavior.CloseConnection);
while (Dr.Read())
{}
Dr.Close();
porschev 2010-03-17
  • 打赏
  • 举报
回复
哪用完了在哪关啊。。。。。。。。。还是用using吧。。。。。比较保险。。。
le616 2010-03-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sandy945 的回复:]
Comm.ExecuteReader();

=》

Comm.ExecuteReader(CommandBehavior.CloseConnection);
[/Quote]
up
xupeihuagudulei 2010-03-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sandy945 的回复:]

Comm.ExecuteReader();

=》

Comm.ExecuteReader(CommandBehavior.CloseConnection);
[/Quote]顶非哥
煊烨 2010-03-17
  • 打赏
  • 举报
回复
reader取出先存到其他地方如list ,并马上关闭reader和connection
zhulong1111 2010-03-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zhaowanru19 的回复:]
返回值是reader的话很容易出问题。
吃过亏~
[/Quote]吃一亏,涨一智啊~~~~~~~~~!!!
zhaowanru19 2010-03-17
  • 打赏
  • 举报
回复
返回值是reader的话很容易出问题。
吃过亏~
polarissky 2010-03-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sandy945 的回复:]

Comm.ExecuteReader();

=》

Comm.ExecuteReader(CommandBehavior.CloseConnection);
[/Quote]
UP!OleDbCommand 执行完后,关闭数据库连接。
使用using(OleDbDataReader reader = getDr(string StrText))
{}
或者try catch finally 执行查询后关闭OleDbDataReader
yongfa365 2010-03-17
  • 打赏
  • 举报
回复
while(dr.Read())
{

}
dr.Close();
royrandy 2010-03-17
  • 打赏
  • 举报
回复
使用Reader时请使用Using()
阿非 2010-03-17
  • 打赏
  • 举报
回复
Comm.ExecuteReader();

=》

Comm.ExecuteReader(CommandBehavior.CloseConnection);

leonbingo 2010-03-17
  • 打赏
  • 举报
回复
最好是用DataAdapter,读取完了以后就关闭连接
DataReader是一条一条读取的,读取完后就可以关闭了。

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧