using (SqlCommand cmd = new SqlCommand(strSql, this.conn))
            {
                //给SqlCommand加事务
                if (inTransaction)
                    cmd.Transaction = trans;                //执行SQL语句
                try
                {
                    cmd.Connection.Open();
                    int rows = cmd.ExecuteNonQuery();
                    return rows;
                }
                catch (System.Data.SqlClient.SqlException e)
                {
                    cmd.Connection.Close();
                    throw e;
                }            }

解决方案 »

  1.   

    这样写为什么不去看下sqlhelper的呢
      

  2.   

    try没异常的话就不关,要想关闭的话using (SqlCommand cmd = new SqlCommand(strSql, this.conn)) 
                { 
                    //给SqlCommand加事务 
                    if (inTransaction) 
                        cmd.Transaction = trans;                 //执行SQL语句 
                    try 
                    { 
                        cmd.Connection.Open(); 
                        int rows = cmd.ExecuteNonQuery(); 
                        return rows; 
                    } 
                    catch (System.Data.SqlClient.SqlException e) 
                    { 
                        
                        throw e; 
                    }
                    finally
                    { 
                         cmd.Connection.Close(); 
                    }             }
      

  3.   

    使用 using程序自动释放资源,using本质就是 使用了 try  finally 
    所以没必要再自己写个 finally 实现回收!
      

  4.   

    4楼你所说的自动回收,你实验过吗?
    我实验过了,他就是不释SQL连接
      

  5.   

    使用 using程序自动释放资源,using本质就是 使用了 try  finally 
    所以没必要再自己写个 finally 实现回收!4楼说的没错  
      

  6.   

    using(sqlconnection conn = new sqlconnection)这样会关SQL连接,但是using(sqlcommand cmd = new sqlcommd) 会关SQL连接吗?
      

  7.   


    没看清楚代码吧?他using 里的是SqlCommand cmd = new SqlCommand(strSql, this.conn)能关conn吗?