1. 程式人生 > >以ADO形式操作mysql資料庫

以ADO形式操作mysql資料庫

首先得需要一個連線mysql的helper類:

public class MySqlHelper
{

        #region [ Connection ]
        public static string connectionString = "Database=newspublish;Data Source=127.0.0.1;Port=3306;User Id=root;Password=123456;Charset=utf8;SslMode=none;";
        public static MySqlConnection GetConnection
        {
            get
            {
                return new MySqlConnection(connectionString);
            }
        }
        #endregion

        #region [ ExecuteNonQuery ]
        /// <summary>
        /// 普通SQL語句執行增刪改
        /// </summary>
        /// <param name="cmdText">SQL語句</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns>受影響行數</returns>
        public static int ExecuteNonQuery(string cmdText, params MySqlParameter[] commandParameters)
        {
            return ExecuteNonQuery(cmdText, CommandType.Text, commandParameters);
        }
        /// <summary>
        /// 儲存過程執行增刪改
        /// </summary>
        /// <param name="cmdText">儲存過程</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns>受影響行數</returns>
        public static int ExecuteNonQueryByProc(string cmdText, params MySqlParameter[] commandParameters)
        {
            return ExecuteNonQuery(cmdText, CommandType.StoredProcedure, commandParameters);
        }
        /// <summary>
        /// 執行增刪改
        /// </summary>
        /// <param name="cmdText">命令字串</param>
        /// <param name="cmdType">命令型別</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns>受影響行數</returns>
        public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params MySqlParameter[] commandParameters)
        {
            int result = 0;

            using (MySqlConnection conn = GetConnection)
            {
                try
                {
                    MySqlCommand command = new MySqlCommand();
                    PrepareCommand(command, conn, cmdType, cmdText, commandParameters);
                    result = command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (conn != null && conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }
            return result;
        }
        #endregion

        #region [ ExecuteReader ]
        /// <summary>
        /// SQL語句得到 MySqlDataReader 物件
        /// </summary>
        /// <param name="cmdText">命令字串</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns>MySqlDataReader 物件</returns>
        public static MySqlDataReader ExecuteReader(string cmdText, params MySqlParameter[] commandParameters)
        {
            return ExecuteReader(cmdText, CommandType.Text, commandParameters);
        }
        /// <summary>
        /// 儲存過程得到 MySqlDataReader 物件
        /// </summary>
        /// <param name="cmdText">命令字串</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns>MySqlDataReader 物件</returns>
        public static MySqlDataReader ExecuteReaderByProc(string cmdText, params MySqlParameter[] commandParameters)
        {
            return ExecuteReader(cmdText, CommandType.StoredProcedure, commandParameters);
        }
        /// <summary>
        /// 得到 MySqlDataReader 物件
        /// </summary>
        /// <param name="cmdText">命令字串</param>
        /// <param name="cmdType">命令型別</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns>MySqlDataReader 物件</returns>
        public static MySqlDataReader ExecuteReader(string cmdText, CommandType cmdType, params MySqlParameter[] commandParameters)
        {
            MySqlDataReader result = null;

            using (MySqlConnection conn = GetConnection)
            {
                try
                {
                    MySqlCommand command = new MySqlCommand();
                    PrepareCommand(command, conn, cmdType, cmdText, commandParameters);
                    result = command.ExecuteReader(CommandBehavior.CloseConnection);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (conn != null && conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }

            return result;
        }
        #endregion

        #region [ ExecuteDataSet ]
        /// <summary>
        /// 執行SQL語句, 返回DataSet
        /// </summary>
        /// <param name="cmdText">命令字串</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns> DataSet </returns>
        public static DataSet ExecuteDataSet(string cmdText, params MySqlParameter[] commandParameters)
        {
            return ExecuteDataSet(cmdText, CommandType.Text, commandParameters);
        }

        /// <summary>
        /// 執行儲存過程, 返回DataSet
        /// </summary>
        /// <param name="cmdText">命令字串</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns> DataSet </returns>
        public static DataSet ExecuteDataSetByProc(string cmdText, params MySqlParameter[] commandParameters)
        {
            return ExecuteDataSet(cmdText, CommandType.StoredProcedure, commandParameters);
        }

        /// <summary>
        /// 返回DataSet
        /// </summary>
        /// <param name="cmdText">命令字串</param>
        /// <param name="cmdType">命令型別</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns> DataSet </returns>
        public static DataSet ExecuteDataSet(string cmdText, CommandType cmdType, params MySqlParameter[] commandParameters)
        {
            DataSet result = null;

            using (MySqlConnection conn = GetConnection)
            {
                try
                {
                    MySqlCommand command = new MySqlCommand();
                    PrepareCommand(command, conn, cmdType, cmdText, commandParameters);
                    MySqlDataAdapter adapter = new MySqlDataAdapter();
                    adapter.SelectCommand = command;
                    result = new DataSet();
                    adapter.Fill(result);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (conn != null && conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }

            return result;
        }
        #endregion

        #region [ ExecuteDataTable ]
        /// <summary>
        /// 執行SQL語句, 返回DataTable
        /// </summary>
        /// <param name="cmdText">命令字串</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns> DataTable </returns>
        public static DataTable ExecuteDataTable(string cmdText, params MySqlParameter[] commandParameters)
        {
            return ExecuteDataTable(cmdText, CommandType.Text, commandParameters);
        }

        /// <summary>
        /// 執行儲存過程, 返回DataTable
        /// </summary>
        /// <param name="cmdText">命令字串</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns> DataTable </returns>
        public static DataTable ExecuteDataTableByProc(string cmdText, params MySqlParameter[] commandParameters)
        {
            return ExecuteDataTable(cmdText, CommandType.StoredProcedure, commandParameters);
        }

        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <param name="cmdText">命令字串</param>
        /// <param name="cmdType">命令型別</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns> DataTable </returns>
        public static DataTable ExecuteDataTable(string cmdText, CommandType cmdType, params MySqlParameter[] commandParameters)
        {
            DataTable dtResult = null;
            DataSet ds = ExecuteDataSet(cmdText, cmdType, commandParameters);

            if (ds != null && ds.Tables.Count > 0)
            {
                dtResult = ds.Tables[0];
            }
            return dtResult;
        }
        #endregion

        #region [ ExecuteScalar ]
        /// <summary>
        /// 普通SQL語句執行ExecuteScalar
        /// </summary>
        /// <param name="cmdText">SQL語句</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns>受影響行數</returns>
        public static object ExecuteScalar(string cmdText, params MySqlParameter[] commandParameters)
        {
            return ExecuteScalar(cmdText, CommandType.Text, commandParameters);
        }
        /// <summary>
        /// 儲存過程執行ExecuteScalar
        /// </summary>
        /// <param name="cmdText">儲存過程</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns>受影響行數</returns>
        public static object ExecuteScalarByProc(string cmdText, params MySqlParameter[] commandParameters)
        {
            return ExecuteScalar(cmdText, CommandType.StoredProcedure, commandParameters);
        }
        /// <summary>
        /// 執行ExecuteScalar
        /// </summary>
        /// <param name="cmdText">命令字串</param>
        /// <param name="cmdType">命令型別</param>
        /// <param name="commandParameters">可變引數</param>
        /// <returns>受影響行數</returns>
        public static object ExecuteScalar(string cmdText, CommandType cmdType, params MySqlParameter[] commandParameters)
        {
            object result = null;

            using (MySqlConnection conn = GetConnection)
            {
                try
                {
                    MySqlCommand command = new MySqlCommand();
                    PrepareCommand(command, conn, cmdType, cmdText, commandParameters);
                    result = command.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (conn != null && conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }
            return result;
        }
        #endregion

        #region [ PrepareCommand ]
        /// <summary>
        /// Command物件執行前預處理
        /// </summary>
        /// <param name="command"></param>
        /// <param name="connection"></param>
        /// <param name="trans"></param>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="commandParameters"></param>
        private static void PrepareCommand(MySqlCommand command, MySqlConnection connection, CommandType cmdType, string cmdText, MySqlParameter[] commandParameters)
        {
            try
            {
                if (connection.State != ConnectionState.Open) connection.Open();

                command.Connection = connection;
                command.CommandText = cmdText;
                command.CommandType = cmdType;
                //command.CommandTimeout = 3600;    //此處請自定義

                //if (trans != null) command.Transaction = trans;

                if (commandParameters != null)
                {
                    foreach (MySqlParameter parm in commandParameters)
                        command.Parameters.Add(parm);
                }
            }
            catch
            {

            }
        }
        #endregion

}

新建控制檯應用程式:

static void Main(string[] args)
{
            //string sql = "select title from news where id = 10";/*查詢*/
            //string result = (string)MySqlHelper.ExecuteScalar(sql);


            //string sql = "INSERT INTO news(title,content,typeid) values('111','222',6)";/*新增*/
            //string sql = "UPDATE news SET title = '666' WHERE id = 11";/*更新*/
            //string sql = "DELETE FROM news WHERE id = 11";/*刪除*/
            //int i = MySqlHelper.ExecuteNonQuery(sql);

            //Console.WriteLine(i);

            Console.Read();
}