MySqlHelp.cs 8.68 KB
using Microsoft.EntityFrameworkCore;
using System;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace WebRepository
{
    public class MySqlHelp
    {
        private readonly string _cnString;

        public MySqlHelp(string cnString)
        {
            _cnString = cnString;
        }

        /// <summary>
        /// 数据操作
        /// </summary>
        public int DataOperator(string Cmstring)
        {
            int Line = 0;

            MySqlCommand sqlCm = null;
            MySqlConnection sqlCn = null;

            try
            {
                using (sqlCn = new MySqlConnection(_cnString))
                {
                    sqlCn.Open();
                    sqlCm = new MySqlCommand(Cmstring, sqlCn);
                    Line = sqlCm.ExecuteNonQuery();
                    sqlCn.Close();
                }
            }
            catch (Exception ex)
            {
                Line = -1;
                throw new Exception(ex.Message.Replace(Environment.NewLine, ""));

            }
            finally
            {
                sqlCn.Dispose();
                if (sqlCm != null)
                    sqlCm.Dispose();
            }
            return Line;
        }

        /// <summary>
        /// 查询信息
        /// </summary>
        public DataSet SelectGet(string CmString)
        {
            DataSet ds = new System.Data.DataSet();

            MySqlConnection sqlCn = null;
            MySqlDataAdapter sqlDa = null;
            try
            {
                using (sqlCn = new MySqlConnection(_cnString))
                {
                    sqlCn.Open();
                    sqlDa = new MySqlDataAdapter(CmString, sqlCn);
                    sqlDa.Fill(ds);
                    sqlCn.Close();
                }
            }
            catch (Exception ex)
            {
                ds = null;
                throw new Exception(ex.Message.Replace(Environment.NewLine, ""));
                //App.ExFile.MessageLog(App.ExFile.Path_Emessage, "", ex.ToString());
            }
            finally
            {
                sqlCn.Dispose();
                if (sqlDa != null)
                    sqlDa.Dispose();
            }

            return ds;
        }

        /// <summary>
        /// 执行存储过程,返回影响的行数 
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <returns></returns>
        public int RunProcedure(string storedProcName, IDataParameter[] parameters)
        {
            int result = 0;

            using (MySqlConnection connection = new MySqlConnection(_cnString))
            {
                connection.Open();
                MySqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
                int rowsAffected = command.ExecuteNonQuery();
                result = (int)command.Parameters["ReturnValue"].Value;
                connection.Close();
            }

            return result;
        }

        public int ProcOutput(SqlCommand cmd, string procName, SqlParameter[] paras)
        {
            int result = 0;
            cmd.CommandType = CommandType.StoredProcedure;//设置此类型是存储过程类型
            cmd.CommandText = procName;//设置执行的存储过程//添加para参数
            if (paras != null)
            {
                foreach (SqlParameter parameter in paras)
                    cmd.Parameters.Add(parameter);
            }
            cmd.Parameters.Add(new SqlParameter("@ReturnValue",
             SqlDbType.Int,
            (int)ParameterDirection.Output));
            int n = cmd.ExecuteNonQuery();//执行语句并返回受影响的行数
            result = (int)cmd.Parameters["ReturnValue"].Value;

            return result;
        }

        public int ProcOutput(MySqlCommand cmd, string procName, MySqlParameter[] paras)
        {
            int result = 0;
            cmd.CommandType = CommandType.StoredProcedure;//设置此类型是存储过程类型
            cmd.CommandText = procName;//设置执行的存储过程//添加para参数
            if (paras != null)
            {
                foreach (MySqlParameter parameter in paras)
                    cmd.Parameters.Add(parameter);
            }
            cmd.Parameters.Add(new MySqlParameter("ReturnValue",
                MySqlDbType.Int32, 4, ParameterDirection.ReturnValue,
                false, 0, 0, string.Empty, DataRowVersion.Default, null));
            int n = cmd.ExecuteNonQuery();//执行语句并返回受影响的行数
            result = (int)cmd.Parameters["ReturnValue"].Value;

            return result;
        }

        public int ProcOutput(string procName, SqlParameter[] paras)
        {
            int result = 0;

            MySqlConnection conn = new MySqlConnection(_cnString);//创建数据库连接对象
            conn.Open();//打开数据库连接

            //SqlCommand cmd = conn.CreateCommand();//创建并执行与此连接关联的T-Sql语句命令的对象,可以不用这段用下面两段
            MySqlCommand cmd = new MySqlCommand
            {
                Connection = conn
            };

            cmd.CommandType = CommandType.StoredProcedure;//设置此类型是存储过程类型
            cmd.CommandText = procName;//设置执行的存储过程//添加para参数
            if (paras != null)
            {
                foreach (SqlParameter parameter in paras)
                    cmd.Parameters.Add(parameter);
            }
            cmd.Parameters.Add(new SqlParameter("@ReturnValue",
             SqlDbType.Int,
            (int)ParameterDirection.Output));
            int n = cmd.ExecuteNonQuery();//执行语句并返回受影响的行数
            result = (int)cmd.Parameters["ReturnValue"].Value;
            conn.Close();//关闭连接

            return result;
        }

        private MySqlCommand BuildIntCommand(MySqlConnection connection, string storedProcName, IDataParameter[] parameters)
        {
            MySqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
            command.Parameters.Add(new MySqlParameter("ReturnValue",
                MySqlDbType.Int32, 4, ParameterDirection.ReturnValue,
                false, 0, 0, string.Empty, DataRowVersion.Default, null));
            return command;
        }

        private MySqlCommand BuildQueryCommand(MySqlConnection connection, string storedProcName, IDataParameter[] parameters)
        {
            MySqlCommand command = new MySqlCommand(storedProcName, connection)
            {
                CommandType = CommandType.StoredProcedure
            };
            foreach (MySqlParameter parameter in parameters)
            {
                if (parameter != null)
                {
                    // 检查未分配值的输出参数,将其分配以DBNull.Value.
                    if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
                        (parameter.Value == null))
                    {
                        parameter.Value = DBNull.Value;
                    }
                    command.Parameters.Add(parameter);
                }
            }
            return command;
        }

        /// <summary>
        /// 调用存储过程Output参数方法
        /// </summary>
        /// <param name="procName">执行的存储过程语句</param>
        /// <param name="paras">数据库的参数</param>
        /// <param name="OutName">定义Output参数</param>
        /// <returns>返回值</returns>
        public object ProcOutput(string procName, SqlParameter[] paras, string OutName)
        {
            object o = null;

            MySqlConnection conn = new MySqlConnection(_cnString);//创建数据库连接对象
            conn.Open();//打开数据库连接

            //SqlCommand cmd = conn.CreateCommand();//创建并执行与此连接关联的T-Sql语句命令的对象,可以不用这段用下面两段
            MySqlCommand cmd = new MySqlCommand
            {
                Connection = conn,

                CommandType = CommandType.StoredProcedure,//设置此类型是存储过程类型
                CommandText = procName//设置执行的存储过程//添加para参数
            };
            if (paras != null)
            {
                foreach (SqlParameter parameter in paras)
                    cmd.Parameters.Add(parameter);
            }
            int n = cmd.ExecuteNonQuery();//执行语句并返回受影响的行数
            o = cmd.Parameters[OutName].Value;//获取@CardID的值
            conn.Close();//关闭连接

            return o;
        }
    }
}