DbHelperSQL.cs 4.24 KB
using System;
using System.Collections.Generic;
using System.Data;
//using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace XingYe_ACS.Common
{
    public class DbHelperSQL
    {
        /// <summary>
        /// 执行SQL语句,返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString)
        {
            SQLString = SQLString.Replace("'null'", "null");
            int Line = 0;
            MySqlCommand mySqlCm = null;
            MySqlConnection mySqlCn = null;
            try
            {
                using (mySqlCn = new MySqlConnection(App.ConSql))
                {
                    mySqlCn.Open();
                    mySqlCm = new MySqlCommand(SQLString, mySqlCn);
                    Line = mySqlCm.ExecuteNonQuery();
                    mySqlCn.Close();
                }
            }
#pragma warning disable CS0168 // 声明了变量“Ex”,但从未使用过
            catch (Exception Ex)
#pragma warning restore CS0168 // 声明了变量“Ex”,但从未使用过
            {
                //if (!Ex.Message.Contains("事务在触发器中结束。批处理已中止"))
                //    App.ExFile.MessageError("ExecuteSql", SQLString + ";\n" + Ex.Message.Replace(Environment.NewLine, ""));
            }
            finally
            {
                mySqlCn.Dispose();
                mySqlCm.Dispose();
            }
            return Line;
        }

        /// <summary>
        /// 事务批量操作数据库
        /// </summary>
        /// <param name="SQLString">需要执行的SQL语句集合</param>
        /// <returns>是否成功</returns>
        public static bool SqlTransOperator(string MySQLString)
        {
            MySQLString = MySQLString.Replace("'null'", "null");
            MySqlConnection mySqlCn = new MySqlConnection(App.ConSql);
            mySqlCn.Open();
            MySqlTransaction mySqlTran = mySqlCn.BeginTransaction();
            string[] sqlList = MySQLString.Split(';');
            List<MySqlCommand> mySqlCmdList = new List<MySqlCommand>();
            foreach (string strCm in sqlList)
            {
                if (strCm == "") continue;
                MySqlCommand mySqlcm = new MySqlCommand(strCm, mySqlCn);
                mySqlcm.Transaction = mySqlTran;
                mySqlCmdList.Add(mySqlcm);
            }
            try
            {

                foreach (MySqlCommand mySqlComm in mySqlCmdList)
                {
                    mySqlComm.ExecuteNonQuery();
                }
                mySqlTran.Commit();
                mySqlCn.Close();
            }
            catch (Exception Ex)
            {
                mySqlTran.Rollback();
                App.ExFile.MessageError("SqlTransOperator", MySQLString + ";\n" + Ex.Message.Replace(Environment.NewLine, ""));
                return false;
            }
            finally
            {
                foreach (MySqlCommand cm in mySqlCmdList)
                {
                    cm.Dispose();
                }
                mySqlTran.Dispose();
                mySqlCn.Dispose();
            }
            return true;
        }

        /// <summary>
        /// 执行查询语句,返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public static DataSet Query(string SQLString)
        {
            DataSet ds = new DataSet();
            MySqlConnection mySqlCn = null;
            MySqlDataAdapter mySqlDa = null;
            try
            {
                using (mySqlCn = new MySqlConnection(App.ConSql))
                {
                    mySqlCn.Open();
                    mySqlDa = new MySqlDataAdapter(SQLString, mySqlCn);
                    mySqlDa.Fill(ds);
                    mySqlCn.Close();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                mySqlCn.Dispose();
                mySqlDa.Dispose();
            }
            return ds;
        }
    }
}