"Using"ステートメントを同じコードブロックで異なる方法で適用することによって大きな違いがあるのは間違いです。私のため sameple 1つのコードブロック"using"ステートメント内でsqlオブジェクトを開始し、ステートメントを使用して装飾することの相違点
using (SqlConnection SqlConnection = new SqlConnection(dbConnectionString))
{
SqlConnection.Open();
using (var command = new SqlCommand(store_procName, SqlConnection))
{
command.Parameters.Add(Constants.PARAM_Value, SqlDbType.VarChar).Value = Id;
command.CommandType = CommandType.StoredProcedure;
using (var adp = new SqlDataAdapter(command))
{
adp.Fill(dtValid);
}
}
}
return dtValid;
サンプルコードブロック2
using (SqlConnection SqlConnection = new SqlConnection(dbConnectionString))
{
SqlConnection.Open();
SqlCommand command = new SqlCommand(store_procName, SqlConnection);
command.Parameters.Add(Constants.PARAM_Value, SqlDbType.VarChar).Value = Id;
command.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(command);
adp.Fill(dtValid);
}
return dtValid;
これは 'asp.net-mv'c固有のものではなく、' sql-serve'rの特定の質問でもありません。ちょうどC#。さらに、読みやすくするためにコードを書式設定するために投稿する前に余分な10秒を費やしてください。 :) – Shyju
実際に私はMVCフレームワークで、そのようなシナリオに遭遇するかもしれないと思っています。 –
見てくださいhttp://stackoverflow.com/questions/3715126/what-is-meant-by-connection-dispose-in-c –