using System;
using System.Data;
using System.Data.OleDb;
class OleDbWrapper : IDisposable {
///
OleDbConnection _connection;
///
OleDbTransaction _transaction;
bool _disposed;
///
/// コンストラクタ
///
/// 接続文字列
public OleDbWrapper(string connectString) {
_connection = new OleDbConnection(connectString);
}
///
/// リソースを解放します。
///
///
private void Dispose(bool disposing) {
if (_disposed == false) {
if (disposing) {
//
}
_disposed = true;
}
if (_transaction != null) {
_transaction.Dispose();
_transaction = null;
}
if (_connection != null) {
this.Close();
_connection.Dispose();
_connection = null;
}
}
///
/// リソースを解放します。
///
public void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
}
///
/// トランザクションを開始します。
///
public OleDbTransaction BeginTransaction() {
try {
_transaction = _connection.BeginTransaction();
} catch (Exception) {
throw;
}
return _transaction;
}
///
/// データベースへの接続を閉じます。
///
public void Close() {
try {
if (_connection != null &&
_connection.State == ConnectionState.Open) {
_connection.Close();
}
} catch (Exception) {
throw;
}
}
///
/// トランザクション処理をコミットします。
///
public void Commit() {
try {
if (_transaction != null) {
_transaction.Commit();
}
} catch (Exception) {
throw;
}
}
///
/// 接続に対して SQL ステートメントを実行し、影響を受けた行数を返します。
///
///
///
public int ExecNonQuery(OleDbCommand command) {
int ret = 0;
try {
command.Connection = _connection;
if (_transaction != null) {
command.Transaction = _transaction;
}
ret = command.ExecuteNonQuery();
} catch (Exception) {
throw;
}
return ret;
}
///
/// クエリを実行し、そのクエリが返す結果セットの最初の行にある最初の列を返します。
///
///
///
public Object ExecScaler(OleDbCommand command) {
Object ret = null;
command.Connection = _connection;
try {
if (_transaction != null) {
command.Transaction = _transaction;
}
ret = command.ExecuteScalar();
} catch (Exception) {
throw;
}
return ret;
}
///
/// データテーブル取得処理
///
///
///
///
/// パラメータに渡されたコマンドを実行し、データテーブルを取得します。
///
public DataTable GetDataTable(OleDbCommand command) {
DataTable ret = null;
try {
command.Connection = _connection;
if (_transaction != null) {
command.Transaction = _transaction;
}
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
ret = new DataTable();
adapter.Fill(ret);
} catch (Exception) {
ret = null;
throw;
}
return ret;
}
///
/// データベース接続を開きます。
///
public void Open() {
try {
_connection.Open();
} catch (Exception) {
throw;
}
}
///
/// トランザクション処理をロールバックします。
///
public void Rollback() {
try {
if (_transaction != null) {
_transaction.Rollback();
}
} catch (Exception) {
throw;
}
}
///
/// データテーブル更新処理
///
/// 更新対象となるデータテーブル
///
///
public int Update(DataTable source, OleDbCommand command) {
int ret = 0;
try {
command.Connection = _connection;
if (_transaction != null) {
command.Transaction = _transaction;
}
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
ret = adapter.Update(source);
} catch (Exception) {
throw;
}
return ret;
}
}
0 件のコメント:
コメントを投稿