帮忙看看我的函数有没有合理关闭数据库连接

bary 2009-06-15 04:54:18
        
public static ArrayList getReferee1()
{
MySqlConnection myconn = new MySqlConnection(strcon);
myconn.Open();
MySqlCommand cmd = new MySqlCommand("select email from referee1", myconn);
MySqlDataReader reader = cmd.ExecuteReader();
ArrayList al = new ArrayList();
try
{
if (reader.HasRows)
{
while (reader.Read())
{
al.Add(reader["email"].ToString());
}
}
return al;
}
catch (Exception)
{
return null;
}
finally
{
myconn.Close();
}
}
...全文
74 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
龙宜坡 2009-06-15
  • 打赏
  • 举报
回复
finally
{
if(myconn != null)//请加这个判断
{
myconn.Close();
}
}

ygp285369675 2009-06-15
  • 打赏
  • 举报
回复

我建议在创建对象时,用using(实例化对象){},他可以自动释放资源,不用手动关闭,能优化程序
代码如下:
public static ArrayList getReferee1()
{
MySqlConnection myconn = new MySqlConnection(strcon);
myconn.Open();
MySqlCommand cmd = new MySqlCommand("select email from referee1", myconn);
MySqlDataReader reader = cmd.ExecuteReader();
ArrayList al = new ArrayList();
try
{
if (reader.HasRows)
{
while (reader.Read())
{
al.Add(reader["email"].ToString());
}
}
return al;
}
catch (Exception)
{
return null;
}
finally
{
myconn.Close();
}
}


public static ArrayList getReferee1()
{
using(MySqlConnection myconn = new MySqlConnection(strcon))
{
myconn.Open();
MySqlCommand cmd = new MySqlCommand("select email from referee1", myconn);
MySqlDataReader reader = cmd.ExecuteReader();
ArrayList al = new ArrayList();
try
{
if (reader.HasRows)
{
while (reader.Read())
{
al.Add(reader["email"].ToString());
}
}
return al;
}
catch (Exception)
{
return null;
}
finally
{
myconn.Close();
}
}
}

Profiteerchen 2009-06-15
  • 打赏
  • 举报
回复
reader没有关闭吧?
whh356 2009-06-15
  • 打赏
  • 举报
回复
推荐个好的学习网站
http://www.it63.net一起去学习学习
whh356 2009-06-15
  • 打赏
  • 举报
回复
推荐个好的学习网站
http://www.it63.net
一起去学习学习
昕颖 2009-06-15
  • 打赏
  • 举报
回复
一楼说的挺正确的。。。不防去试试看。。
ICanUseThisID 2009-06-15
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 bary 的回复:]
dispose和close用哪个好,还是都要用?
[/Quote]
一般来说都一样的,调用一个就可以了
xu_hfut 2009-06-15
  • 打赏
  • 举报
回复
MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

只要关闭reader 就可以自动关闭连接。
bary 2009-06-15
  • 打赏
  • 举报
回复
dispose和close用哪个好,还是都要用?
yangjiang113 2009-06-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ICanUseThisID 的回复:]
myconn.Open();
MySqlDataReader reader = cmd.ExecuteReader();

这两句也可能产生异常,所以最好把他们try进去,而且reader最后也要关闭
[/Quote]
我也这么认为
loveczz 2009-06-15
  • 打赏
  • 举报
回复
最后都会执行finally里的语句 所以是关闭数据库了
ICanUseThisID 2009-06-15
  • 打赏
  • 举报
回复
myconn.Open();
MySqlDataReader reader = cmd.ExecuteReader();

这两句也可能产生异常,所以最好把他们try进去,而且reader最后也要关闭
yczf1836 2009-06-15
  • 打赏
  • 举报
回复
当然可以了,链接都断了。
HDNGO 2009-06-15
  • 打赏
  • 举报
回复
wang_j_p 2009-06-15
  • 打赏
  • 举报
回复
public static ArrayList getReferee1()
{
MySqlConnection myconn = new MySqlConnection(strcon);
myconn.Open();
MySqlCommand cmd = new MySqlCommand("select email from referee1", myconn);
MySqlDataReader reader = cmd.ExecuteReader();
ArrayList al = new ArrayList();
try
{
if (reader.HasRows)
{
while (reader.Read())
{
al.Add(reader["email"].ToString());
}
}

return al;
}
catch (Exception)
{
return null;
}
finally
{
// add
reader.Close();
myconn.Close();
}
}
surlew 2009-06-15
  • 打赏
  • 举报
回复
可以
crazyleo814 2009-06-15
  • 打赏
  • 举报
回复
没有。。。
从里到外释放关闭

finally
{
reader.Close();
cmd.Dispose();
myconn.Dispose();
myconn.Close();
}

110,534

社区成员

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

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

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