バックグラウンド:
私は再利用のために ac# 4.0 クラスを作成しているので、プログラムのほとんどのメソッドに 1 つのパラメーターを送信し、次のようなものを保持できる「接続」オブジェクトを渡すことができます。
- 接続文字列
- DbConnection (つまり、OleDbConnection または SQLConnection)
- DataReader (つまり、OleDbDataReader または SQLDataReader)
その理由は、最終的に SQL Server にアップグレードする予定であり、メソッドのパラメーターやコードをあまり変更したくないからです。メソッドは、多くの場合、同じデータベースからあちこちでクイック クエリを呼び出さなければなりません。
質問:
以下の行のメソッドで、DbType の DRMDbConnection 'set' パラメータ (現在は整数 1 としてハードコードされています) の右側を適切にセットアップ (および呼び出し) するにはどうすればよいですか。
set { drmDbConnection = SetDRMDbConnection(1); }
サンプルコード:
public class Connections
{
public string DRMDbConnectionString { get; set; }
private object drmDbConnection;
public object DRMDbConnection
{
get { return drmDbConnection; }
set { drmDbConnection = SetDRMDbConnection(1); }
}
private object SetDRMDbConnection(int DbType)
{
if (DbType == 1)
return new System.Data.OleDb.OleDbConnection();
else
return new SqlConnection();
}
}
10月31日編集
推奨される IDbConnection、IDbCommand、および IDataReader を使用するようにクラス コードを更新しました。注: SQL Server 接続文字列を変更しないと、次のエラーが表示されます。したがって、これを接続文字列に追加します: "MultipleActiveResultSets=True;"
更新されたクラス コード
public class Connections
// reusuable object to hold all database Connections and a convenient DataReader and Command object.
// Its purpose is to also be able to handle your choice of database connection, i.e. OleDb or SQL Server.
{
public string DRMConnectionString { get; set; }
public IDbConnection DRMConnection;
public IDbCommand DRMCommand
{
get { return DRMConnection.CreateCommand(); }
set { }
}
public int DRMConnectionType
// must be called to setup database connection and command. The connectionstring must be previously set.
{
set
{
if (value == 1)
DRMConnection = new System.Data.OleDb.OleDbConnection(DRMConnectionString);
else
DRMConnection = new SqlConnection(DRMConnectionString);
}
}
public void CloseConnections()
{
if (DRMCommand != null)
DRMCommand.Dispose();
if ((DRMConnection != null) && (DRMConnection.State != ConnectionState.Closed))
DRMConnection.Close();
}
}
コーリングコード
var conn = new Connections();
conn.DRMConnectionString = "my connection string";
conn.DRMConnectionType = 2;
conn.DRMConnection.Open();
try
{
using (var cmd = conn.DRMCommand)
{
cmd.CommandText = "SELECT * FROM MyTable";
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
CallSubMethodThatAlsoOpensDataReader(conn);
}
}
}
}
finally
{
conn.CloseConnections();
}
CallSubMethodThatAlsoOpensDataReader(Connections Conn)
{
using (var cmd = Conn.DRMCommand)
{
cmd.CommandText = "SELECT * FROM MyOtherTable";
using (var rdr = cmd.ExecuteReader()) // Error: There is already an open DataReader associated with this Command which must be closed first.
{
; // do stuff
}
}
}