public class DbController
{
#region Member
private DataTable _table = null;
private int _errorNo = 0;
private string _oleDbCommandText = "";
private OleDbConnection _oleDbConnection;
private OleDbCommand _oleDbCommand;
private OleDbTransaction _oleDbTransaction;
private string _oleDbConnString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};";
#endregion
#region Property
public DataTable Table
{
get { return this._table; }
set { this._table = value; }
}
public OleDbCommand OleDbCommand
{
get { return this._oleDbCommand; }
set { this._oleDbCommand = value; }
}
public string OleDbCommandText
{
get { return this._oleDbCommandText; }
set { this._oleDbCommandText = value; }
}
#endregion
#region Method
public DbController(string dbPath)
{
_oleDbConnString = String.Format(_oleDbConnString, dbPath);
_oleDbConnection = new OleDbConnection(_oleDbConnString);
_oleDbCommand = new OleDbCommand();
_oleDbCommand.Connection = _oleDbConnection;
_oleDbCommand.CommandType = CommandType.Text;
OpenDbConnection();
}
/// <summary>
/// DBコネクションをオープンする。
/// </summary>
public void OpenDbConnection()
{
try
{
//指定したプロパティ設定を使用して、データベース接続を開く
if (_oleDbConnection.State != ConnectionState.Open)
_oleDbConnection.Open();
}
catch (Exception ex)
{
LogUtil.AddLogMessage("Error: DB connected fail.");
throw new Exception("DB connected fail.", ex);
}
}
/// <summary>
/// DBコネクションをクローズする。
/// </summary>
public void CloseDbConnection()
{
//指定したプロパティ設定を使用して、データベース接続を閉める
if (_oleDbConnection.State != ConnectionState.Closed)
_oleDbConnection.Close();
}
/// <summary>
/// トランザクションを開始する。
/// </summary>
public void BeginTransaction()
{
try
{
if (_oleDbConnection.State != ConnectionState.Open)
{
_oleDbConnection.Open();
}
_oleDbTransaction = _oleDbConnection.BeginTransaction();
}
catch (Exception ex)
{
LogUtil.AddLogMessage("Error: Start transaction failed.");
throw new Exception("Start transaction failed.", ex);
}
}
/// <summary>
/// トランザクションをコミットする。
/// </summary>
public void CommitTransaction()
{
try
{
_oleDbTransaction.Commit();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// トランザクションをロールバックする。
/// </summary>
public void RollbackTransaction()
{
try
{
if (0 == _errorNo)
{
_oleDbTransaction.Rollback();
}
}
catch (Exception ex)
{
LogUtil.AddLogMessage("Error: Transaction rollback faild.");
throw new Exception("Transaction rollback faild.", ex);
}
}
public DataTable Select()
{
DataTable dt = new DataTable();
try
{
_oleDbCommand.CommandText = _oleDbCommandText;
OleDbDataAdapter oleDbAdapter = new OleDbDataAdapter(_oleDbCommand);
oleDbAdapter.Fill(dt);
}
catch (Exception ex)
{
throw ex;
}
return dt;
}
public DataTable Select(string tableName)
{
DataTable dt = new DataTable(tableName);
try
{
_oleDbCommand.CommandText = _oleDbCommandText;
OleDbDataAdapter oleDbAdapter = new OleDbDataAdapter(_oleDbCommand);
oleDbAdapter.Fill(dt);
oleDbAdapter.Dispose();
oleDbAdapter = null;
}
catch (Exception ex)
{
throw ex;
}
finally
{
GC.Collect();
}
return dt;
}
public void Insert()
{
Execute();
}
public void Delete()
{
Execute();
}
public void Update()
{
Execute();
}
private void Execute()
{
try
{
_oleDbCommand = new OleDbCommand(_oleDbCommandText, _oleDbConnection);
_oleDbCommand.Connection = _oleDbConnection;
_oleDbCommand.Transaction = _oleDbTransaction;
_oleDbCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
GC.Collect();
}
}
public DataTable GetColumnsInfo(string tableName)
{
return _oleDbConnection.GetOleDbSchemaTable(
OleDbSchemaGuid.Columns,
new object[] { null, null, tableName, null });
}
public DataTable GetTablesInfo()
{
return _oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null });
}
public void CreateTable()
{
Execute();
}
public void DeleteTable()
{
Execute();
}
public void AddColumn()
{
Execute();
}
#endregion
}