当前位置: 代码迷 >> Sql Server >> sql 超时间有关问题
  详细解决方案

sql 超时间有关问题

热度:12   发布时间:2016-04-24 09:39:39.0
sql 超时间问题
我使用以下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  
该如何解决呢?
------解决思路----------------------
要看下是不是有死锁发生
------解决思路----------------------
代码似乎也有很大的问题,特别是那个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;
}
------解决思路----------------------
在看了一下,你这段代码会造成连接泄漏。
if (GetCon().State == ConnectionState.Open)
                {
                    GetCon().Close();
                    GetCon().Dispose();
                }
GetCon().State ,GetCon().Close(), GetCon().Dispose()会创建3个连接,但是只关闭了2次。
  相关解决方案