DATAREADER没有关闭的问题

fskjb01 2008-12-02 02:44:02
请各位帮我看看为什么说链接没有关闭呢,以确定是//以下有问题和//以上有问题之间的代码有问题,就是不知道该如何解决啊。。


//监控数据库
private void tmrDB_Tick(object sender, EventArgs e)
{
ShowFriendList(); // 刷新好友列表
int MessageTypeId = 1; // 消息类型
int MessageState = 1; // 消息状态
string sql = string.Format("select top 1 Id,FromUserId,MessageTypeId,MessageState from Messages where MessageState=0 and ToUserId={0}", UserHelper.loginId);
try
{
DBhelper.connection.Open();
DBhelper.datareader = DBhelper.command(sql).ExecuteReader();
if (DBhelper.datareader.Read())
{
MessageTypeId = (int)DBhelper.datareader["MessageTypeId"];
MessageState = (int)DBhelper.datareader["MessageState"];
fromUserId = (int)DBhelper.datareader["FromUserId"];
messageId = (int)DBhelper.datareader["Id"];
friendFaceId = GetfriendFaceId(fromUserId);

}

DBhelper.datareader.Close();
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
finally {
DBhelper.connection.Close();
}

// 如果是添加好友消息让小喇叭闪烁
if (MessageTypeId == 2)
{
SoundPlayer player = new SoundPlayer("system.wav");
player.Play();
tmrAddFriend.Start();
}
// 如果是聊天消息让好友头像闪烁
else if (MessageTypeId == 1)
{
//以下有问题
sql = string.Format("select FaceId from users where Id={0}", fromUserId);
try
{
DBhelper.connection.Open();
friendFaceId = (int)DBhelper.command(sql).ExecuteScalar();
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
finally
{
DBhelper.connection.Close();
}
//以上有问题

// 如果发消息的人没有在列表中就添加到陌生人列表中
if (!isContainInList(fromUserId))
{
AddToStranger(fromUserId);
}
SoundPlayer player = new SoundPlayer("msg.wav");
player.Play();
tmrface.Start();
}
}


...全文
106 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
fskjb01 2008-12-02
  • 打赏
  • 举报
回复
晕,问题解决啦,重新生产一下解决方案就搞定了,怎么会事呢???
qinhl99 2008-12-02
  • 打赏
  • 举报
回复
首先DBhelper中的connection作为一个静态全局变量非常的不好,应该作为每一个方法的局部变量!
你这样写,会经常遇到意想不到的问题!
Zhanlixin 2008-12-02
  • 打赏
  • 举报
回复
重新实例化new DBhelper.connection一次
fskjb01 2008-12-02
  • 打赏
  • 举报
回复
我以锁定有问题的代码了,但问题是connectin已经关闭了啊!!
nbgcqlk 2008-12-02
  • 打赏
  • 举报
回复
要DBhelper.datareader.dispose();
HDNGO 2008-12-02
  • 打赏
  • 举报
回复

SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
一游 2008-12-02
  • 打赏
  • 举报
回复
那你一步一步调度,看哪里在打开后没有关闭就可以了

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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