いいえ、 を処分してSqlCommand
も接続には影響しません。より良いアプローチはSqlConnection
、 using ブロックにもラップすることです。
using (SqlConnection conn = new SqlConnection(connstring))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(cmdstring, conn))
{
cmd.ExecuteNonQuery();
}
}
それ以外の場合、それを使用していたコマンドが破棄されたという事実によって、接続は変更されません (おそらくそれがあなたの望みですか?)。ただし、接続も破棄する必要があり、コマンドよりも破棄する方が重要であることに注意してください。
編集:
私はこれをテストしました:
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
using (SqlCommand cmd = new SqlCommand("select field from table where fieldid = 1", conn))
{
Console.WriteLine(cmd.ExecuteScalar().ToString());
}
using (SqlCommand cmd = new SqlCommand("select field from table where fieldid = 2", conn))
{
Console.WriteLine(cmd.ExecuteScalar().ToString());
}
conn.Dispose();
最初のコマンドは、using ブロックが終了したときに破棄されました。接続はまだ開いていて、2 番目のコマンドに適していました。
したがって、コマンドを破棄しても、それが使用していた接続は確実に破棄されません。