SqlConnection 在 Dispose 之前 要不要先 Close 呢?
我个人的习惯是会先叫 Close 再 叫 Dispose
但 Dispose 真的不会先帮我 Close 吗?
今天索性来反组译一下~
.methodfamilyhidebysigvirtualinstancevoid Dispose(bool disposing) cil managed
{
.maxstack 2
.localsinit ([0] System.Data.ConnectionState state1)
L_0000: ldarg.1
L_0001: brfalse.s L_0027
L_0003: ldarg.0
L_0004: ldfld System.Data.ConnectionStateSystem.Data.SqlClient.SqlConnection::_objectState
L_0009: stloc.0
L_000a: ldloc.0
L_000b: switch (L_0020, L_001a)
L_0018: br.s L_0020
L_001a: ldarg.0
L_001b: call instancevoidSystem.Data.SqlClient.SqlConnection::Close()
L_0020: ldarg.0
L_0021: ldnull
L_0022: stfld System.Data.SqlClient.SqlConnectionStringSystem.Data.SqlClient.SqlConnection::_constr
L_0027: ldarg.0
L_0028: ldarg.1
L_0029: call instancevoid [System]System.ComponentModel.Component::Dispose(bool)
L_002e: ret
}
所以答案是 Dispose 就好 ~