私はまだC#に比較的慣れておらず、「IDisposables」にさらされたのは過去数日だけです。usingメソッドを呼び出すことを手動で覚える必要なく、破棄する必要があるオブジェクトを処理するためのブロックの概念を把握できます.Dispose()- 便利です!
ただし、ステートメントSqlConnection内で処理する new から始めるとしましょう。usingそのコード ブロック内で、追加の IDisposables をいくつか作成しますSqlDataAdapter。usingそのアダプターには独自のステートメントが必要ですか?
たとえば、コードがある場合...
using (SqlConnection myConnection = new SqlConnection())
{
SqlCommand myCommand = new SqlCommand();
SqlDataAdapter myAdapter = new SqlDataAdapter();
// Do things
}
... が破棄myCommandさmyAdapterれるときにmyConnection破棄されますか (それらはそのコード ブロックの範囲内にあるため)? usingまたは、次のような複数のステートメントが必要ですか。
using (SqlConnection myConnection = new SqlConnection())
{
using (SqlCommand myCommand = new SqlCommand())
{
using (SqlDataAdapter myAdapter = new SqlDataAdapter())
{
// Do things
}
}
}