听说用使用using语句块可以自动打开和释放连接
但是我没有使用using语句块的习惯,
所以这里问一下性能问题
如下两段代码,首先不管代码有没有错,大家只要理解我的意思就行
1:       using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand(SQLString, connection))
            {
                try
                {
                    connection.Open();
                    int rows = cmd.ExecuteNonQuery();
                    return rows;
                }
                catch (System.Data.SqlClient.SqlException E)
                {
                    connection.Close();
                    throw new Exception(E.Message);
                }
            }
        }2:        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(SQLString, connection);
        try
        {
            connection.Open();
            int rows = cmd.ExecuteNonQuery();
            return rows;
        }
        catch (System.Data.SqlClient.SqlException E)
        {
            connection.Close();
            throw new Exception(E.Message);
        }
        finally
        {
            cmd.Dispose();
            connection.Dispose();
        }
如上的2是我自己的编程习惯,听说using语句块可以自动打开和释放连接
但我的2也是在程序的最后也会执行Dispose()方法
那么这两个之间的性能效率有什么相差,
谢谢

解决方案 »

  1.   

    应该没有性能问题,using语句块只是声明一个范围,这个范围内open有效。
      

  2.   

    .net本身实际上并没有using{},它只是c#和vb.net的编译器支持罢了。代码:static void Main()
    {
        using (SqlConnection conn = new SqlConnection("adfa"))
        {
            int i = 0;
        }
    }
    被编译为.method private hidebysig static void Main() cil managed
    {
        .entrypoint
        .maxstack 2
        .locals init (
            [0] class [System.Data]System.Data.SqlClient.SqlConnection connection)
        L_0000: ldstr "adfa"
        L_0005: newobj instance void [System.Data]System.Data.SqlClient.SqlConnection::.ctor(string)
        L_000a: stloc.0 
        L_000b: leave.s L_0017
        L_000d: ldloc.0 
        L_000e: brfalse.s L_0016
        L_0010: ldloc.0 
        L_0011: callvirt instance void [mscorlib]System.IDisposable::Dispose()
        L_0016: endfinally 
        L_0017: ret 
        .try L_000b to L_000d finally handler L_000d to L_0017
    }
    而代码:static void Main()
    {
        using (SqlConnection conn = new SqlConnection("adfa"))
        {
            try
            {
                int i = 0;
            }
            catch (System.Data.SqlClient.SqlException E)
            {
                conn.Close();
                throw new Exception(E.Message);
            }
            finally
            {
                conn.Dispose();
            }
        }
    }被编译为.method private hidebysig static void Main() cil managed
    {
        .entrypoint
        .maxstack 2
        .locals init (
            [0] class [System.Data]System.Data.SqlClient.SqlConnection connection,
            [1] class [System.Data]System.Data.SqlClient.SqlException exception)
        L_0000: ldstr "adfa"
        L_0005: newobj instance void [System.Data]System.Data.SqlClient.SqlConnection::.ctor(string)
        L_000a: stloc.0 
        L_000b: leave.s L_0014
        L_000d: ldloc.0 
        L_000e: callvirt instance void [System]System.ComponentModel.Component::Dispose()
        L_0013: endfinally 
        L_0014: leave.s L_0020
        L_0016: ldloc.0 
        L_0017: brfalse.s L_001f
        L_0019: ldloc.0 
        L_001a: callvirt instance void [mscorlib]System.IDisposable::Dispose()
        L_001f: endfinally 
        L_0020: ret 
        .try L_000b to L_000d finally handler L_000d to L_0014
        .try L_000b to L_0016 finally handler L_0016 to L_0020
    }
    你写的代码冗余地多做了(没有结果的)工作。不过,我倒是觉得比较性能是非常次要的。你可以自己写个速度测试程序,然后说出这种速度差别可以换算为你的收入的真实的减少。否则,就算速度慢了,又怎样呢?还有更大的事情要优先考虑。
      

  3.   

    sorry,第二个程序应该是:static void Main()
    {
        SqlConnection conn = new SqlConnection("adfa");
        try
        {
            int i = 0;
        }
        catch (System.Data.SqlClient.SqlException E)
        {
            conn.Close();
            throw new Exception(E.Message);
        }
        finally
        {
            conn.Dispose();
        }
    }
    }编译结果:.method private hidebysig static void Main() cil managed
    {
        .entrypoint
        .maxstack 2
        .locals init (
            [0] class [System.Data]System.Data.SqlClient.SqlConnection connection,
            [1] class [System.Data]System.Data.SqlClient.SqlException exception)
        L_0000: ldstr "adfa"
        L_0005: newobj instance void [System.Data]System.Data.SqlClient.SqlConnection::.ctor(string)
        L_000a: stloc.0 
        L_000b: leave.s L_0014
        L_000d: ldloc.0 
        L_000e: callvirt instance void [System]System.ComponentModel.Component::Dispose()
        L_0013: endfinally 
        L_0014: ret 
        .try L_000b to L_000d finally handler L_000d to L_0014
    }
      

  4.   

    只要你的类实现了IDisposable接口并实现接口中的Dispose方法
    在using作用域结束时会自动调用Dispose方法你可以看一下msdn
    像SqlConnection 这些类都是实现了System.IDisposable接口的
      

  5.   

    我们可以看到,就算你写出严禁精炼的流程,其编译结果最多跟using{}得到的编译结果完全一样,而不会更好、更精炼了。何况,最主要地不在乎效率,而在于你手写那么多代码时难免产生遗漏。
      

  6.   

    对了,你的代码写的冗余了(把握在对比“性能”时都绕进去了)。真正开发时只要写: 
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                int rows = cmd.ExecuteNonQuery();
                return rows;
            }
    写那么多代码干嘛?
      

  7.   

    code=C#]
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand(SQLString, connection);
                int rows = cmd.ExecuteNonQuery();
                return rows;
            }[/code]
      

  8.   

    呵呵,我也喜欢问这种问题,看似不大但是就想知道
    我认为是没性能差异的,using只是帮你加了Dispose而已,没差别
    但如果你想知道非常精确的答案(哪怕性能相差那么一丁点也算),那你就自己做个测试页测吧
    以前我问过【减少不必要的“using 命名空间”语句能提高多少性能】到现在还不大清楚