MySqlAccessクラス

using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace MySqlTest
{
        /// <summary>
        /// MySQL処理クラス
        /// </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>
                /// 参照系SQLを実行
                /// </summary>
                /// <param name="cmd">コマンド</param>
                /// <returns>データを取得した場合は参照結果データテーブル。取得できない場合はNULL。</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>
                /// 更新系SQLを実行
                /// </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> 結果セットの最初の行の最初の列。結果セットが空の場合はnull。</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
        }
}