DBに接続してDataTableを取得するSQLクラスがあります。終了時にSqlConnectionを破棄する必要があることを認識しています。using
これはブロックを使用して実行できることは知っていますがDispose()
、このクラスのデストラクタ内に呼び出しを配置することもできますか?
Herreは私のコードです:
public class SQLEng
{
//Connection String Property
//Must be set to establish a connection to the database
public string ConnectionString{ get; set; }
SqlConnection _Conn;
//Overridden Constructor enforcing the Connection string to be set when created
public SQLEng(string connectionString)
{
ConnectionString = connectionString;
_Conn = new SqlConnection(connectionString);
}
//ensure the SqlConnection is disposed when destructing this object
public ~SQLEng()
{
_Conn.Dispose();
}
//various other methods to get datatables etc...
}
基本的に、DBにアクセスするすべてのメソッド内でSqlConnectionをインスタンス化するのではなく、クラス変数SqlConnectionが必要です。この音は練習ですか?