听说用使用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()方法
那么这两个之间的性能效率有什么相差,
谢谢
但是我没有使用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()方法
那么这两个之间的性能效率有什么相差,
谢谢
{
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
}
你写的代码冗余地多做了(没有结果的)工作。不过,我倒是觉得比较性能是非常次要的。你可以自己写个速度测试程序,然后说出这种速度差别可以换算为你的收入的真实的减少。否则,就算速度慢了,又怎样呢?还有更大的事情要优先考虑。
{
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
}
在using作用域结束时会自动调用Dispose方法你可以看一下msdn
像SqlConnection 这些类都是实现了System.IDisposable接口的
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
写那么多代码干嘛?
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand(SQLString, connection);
int rows = cmd.ExecuteNonQuery();
return rows;
}[/code]
我认为是没性能差异的,using只是帮你加了Dispose而已,没差别
但如果你想知道非常精确的答案(哪怕性能相差那么一丁点也算),那你就自己做个测试页测吧
以前我问过【减少不必要的“using 命名空间”语句能提高多少性能】到现在还不大清楚