using System;
using System.Data;
using MySql.Data.MySqlClient;
namespace MySqlTest
{
<summary>
</summary>
class MySqlAccess : IDisposable
{
#region 変数・プロパティ
<summary>
</summary>
private MySqlConnection connection = null;
<summary>
</summary>
private MySqlTransaction transaction = null;
<summary>
</summary>
private bool disposedFlag = false;
#endregion
#region コンストラクタ
<summary>
</summary>
<param name="connectionString"></param>
public MySqlAccess(string connectionString)
{
this.connection = new MySqlConnection(connectionString);
}
#endregion
#region リソースを開放
<summary>
</summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
<summary>
</summary>
public void Dispose(bool disposing)
{
if (this.disposedFlag == false)
{
if (disposing)
{
if (this.transaction != null)
{
this.transaction.Dispose();
}
if (this.connection != null)
{
this.Close();
this.connection.Dispose();
}
}
this.disposedFlag = true;
}
}
#endregion
#region データベース接続を開く
<summary>
</summary>
public void Open()
{
try
{
this.connection.Open();
}
catch (Exception)
{
throw;
}
}
#endregion
#region データベースへの接続を閉じる
<summary>
</summary>
public void Close()
{
try
{
if (this.connection != null &&
this.connection.State == ConnectionState.Open)
{
this.connection.Close();
}
}
catch (Exception)
{
throw;
}
}
#endregion
#region トランザクションを開始
<summary>
</summary>
public MySqlTransaction BeginTransaction()
{
try
{
this.transaction =
this.connection.BeginTransaction(IsolationLevel.RepeatableRead);
}
catch (Exception)
{
throw;
}
return this.transaction;
}
<summary>
</summary>
public MySqlTransaction BeginTransaction(IsolationLevel isolationLevel)
{
try
{
this.transaction = this.connection.BeginTransaction(isolationLevel);
}
catch (Exception)
{
throw;
}
return this.transaction;
}
#endregion
#region トランザクション処理をコミット
<summary>
</summary>
public void Commit()
{
try
{
if (this.transaction != null)
{
this.transaction.Commit();
}
}
catch (Exception)
{
throw;
}
}
#endregion
#region トランザクション処理をロールバック
<summary>
</summary>
public void Rollback()
{
try
{
if (this.transaction != null)
{
this.transaction.Rollback();
}
}
catch (Exception)
{
throw;
}
}
#endregion
#region 参照系SQLを実行
<summary>
</summary>
<param name="cmd"></param>
<returns></returns>
public DataTable ExecuteQuery(MySqlCommand cmd)
{
DataTable retDt = null;
try
{
cmd.Connection = this.connection;
if (this.transaction != null)
{
cmd.Transaction = this.transaction;
}
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
MySqlCommandBuilder builder = new MySqlCommandBuilder(adapter);
retDt = new DataTable();
adapter.Fill(retDt);
}
catch (Exception)
{
retDt = null;
throw;
}
return retDt;
}
#endregion
#region 更新系SQLを実行
<summary>
</summary>
<param name="command"></param>
<returns></returns>
public int ExecNonQuery(MySqlCommand cmd)
{
int retCount = 0;
try
{
cmd.Connection = this.connection;
if (this.transaction != null)
{
cmd.Transaction = this.transaction;
}
retCount = cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
return retCount;
}
#endregion
#region 単一の値を取得するを実行
<summary>
</summary>
<param name="cmd"></param>
<returns></returns>
public T ExecuteScalar<T>(MySqlCommand cmd)
{
cmd.Connection = this.connection;
try
{
if (this.transaction != null)
{
cmd.Transaction = this.transaction;
}
return (T)(cmd.ExecuteScalar());
}
catch (Exception)
{
throw;
}
}
#endregion
}
}