sql 超时间问题

wasion 2014-09-17 09:15:22
我使用以下sqlhelper代码:
public SqlConnection GetCon()
{

Str_Connectionstring = "";
conn = new SqlConnection(Str_Connectionstring);
conn.Open();
return conn;

}
public bool GetExecute(string cmdtext)
{

S_Com = new SqlCommand(cmdtext, GetCon());
try
{

S_Com.ExecuteNonQuery();
this.conn.Close();
this.conn.Dispose();
return true;
}
catch (Exception ex)
{
MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
return false;

}
finally
{

if (GetCon().State == ConnectionState.Open)
{

GetCon().Close();
GetCon().Dispose();

}
}
}


当我执行该语句的语句的时候,400-500条的时候就显示超时间。
sql sp_who2 链接数130
该如何解决呢?
...全文
213 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
代码似乎也有很大的问题,特别是那个finally会没有必要的打开三次连接,然后关闭, 试一下下面的。 try { using (SqlConnection conn = new SqlConnection(...)) { SqlCommand S_Com = new SqlCommand(cmdtext,conn ); S_Com.ExecuteNonQuery(); return true; } } catch (Exception ex) { MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); return false; }
xiaoxiangqing 2014-09-18
  • 打赏
  • 举报
回复
要看下是不是有死锁发生
---涛声依旧--- 2014-09-18
  • 打赏
  • 举报
回复
跟连接数没多大关系,楼主看看有无死锁情况 或者代码里将TimeOut的值设置为0(无限制)
  • 打赏
  • 举报
回复
在看了一下,你这段代码会造成连接泄漏。 if (GetCon().State == ConnectionState.Open) { GetCon().Close(); GetCon().Dispose(); } GetCon().State ,GetCon().Close(), GetCon().Dispose()会创建3个连接,但是只关闭了2次。

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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