当前位置:网站首页>沉淀下来的数据库操作类-C#版(SQL Server)

沉淀下来的数据库操作类-C#版(SQL Server)

2022-07-06 09:33:00 心蓝168

#region 命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
#endregion

namespace XL.DB.DbOperaClass
{
    #region SQL语句结构
    /// <summary>
    /// Sql语句结构
    /// </summary>
    public class SqlStruct
    {
        #region 变量
        /// <summary>
        /// sql语句
        /// </summary>
        public string sql = string.Empty;          //Sql语句
        /// <summary>
        /// sql语句参数列表(可选)
        /// </summary>
        public object[] para = null;               //参数数据
        #endregion
    }
    #endregion

    #region 数据参数结构
    /// <summary>
    /// 数据参数结构
    /// </summary>
    [Serializable]
    public class DbParameterStruct
    {
        #region 变量
        /// <summary>
        /// 参数名称
        /// </summary>
        public string ParaName = string.Empty;     //参数名称
        /// <summary>
        /// 参数值
        /// </summary>
        public object ParaValue = null;            //参数值
        #endregion
    }
    #endregion

    /// <summary>
    /// 数据库操作类
    /// </summary>
    public class DbConnClass
    {
        #region 变量
        private string connectionString = string.Empty;
        #endregion

        #region [属性]数据库连接串
        /// <summary>
        /// 数据库连接串
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return this.connectionString;
            }
            set
            {
                this.connectionString = value;
            }
        }
        #endregion

        #region [方法]打开数据库连接
        /// <summary>
        /// 打开数据库连接
        /// </summary>
        public virtual object OpenDbConnection()
        {
            return null;
        }
        #endregion

        #region [方法]关闭数据库连接
        /// <summary>
        /// 关闭数据库连接
        /// </summary>
        public virtual void CloseDbConnection(object dbConnectObj)
        {

        }
        #endregion

        #region [方法]执行只返回影响行数的Sql语句
        /// <summary>
        /// 执行只返回影响行数的Sql语句
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>影响行数</returns>
        public virtual int ExecuteNonQuery(string sql)
        {
            return -1;
        }
        #endregion

        #region [方法]执行只返回影响行数的Sql语句<参数形式>
        /// <summary>
        /// 执行只返回影响行数的Sql语句
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraArray">参数数组</param>
        /// <returns>影响行数</returns>
        public virtual int ExecuteNonQuery(string sql, object[] paraArray)
        {
            return -1;
        }
        #endregion

        #region [方法]返回与Sql语句相对应的数据表
        /// <summary>
        /// 返回与Sql语句相对应的数据表
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>结果数据表</returns>
        public virtual DataTable ReturnDataTable(string sql)
        {
            return null;
        }
        #endregion

        #region [方法]返回与Sql语句相对应的数据表<参数形式>
        /// <summary>
        /// 返回与Sql语句相对应的数据表
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraArray">参数数组</param>
        /// <returns>结果数据表</returns>
        public virtual DataTable ReturnDataTable(string sql, object[] paraArray)
        {
            return null;
        }
        #endregion

        #region [方法]返回包含单个数据表的数据集
        /// <summary>
        /// 返回包含单个数据表的数据集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>结果数据集</returns>
        public virtual DataSet ReturnDataSet(string sql)
        {
            return null;
        }
        #endregion

        #region [方法]返回包含单个数据表的数据集<参数形式>
        /// <summary>
        /// 返回包含单个数据表的数据集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraArray">参数数组</param>
        /// <returns>结果数据集</returns>
        public virtual DataSet ReturnDataSet(string sql, object[] paraArray)
        {
            return null;
        }
        #endregion

        #region [方法]返回与Sql语句相应的数据表的第一行第一列的值
        /// <summary>
        /// 返回与Sql语句相应的数据表的第一行第一列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>结果数据表第一行第一列的值</returns>
        public virtual string ReturnZRowZColContent(string sql)
        {
            return null;
        }
        #endregion

        #region [方法]返回与Sql语句相应的数据表的第一行第一列的值<参数形式>
        /// <summary>
        /// 返回与Sql语句相应的数据表的第一行第一列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraArray">参数数组</param>
        /// <returns>结果数据表第一行第一列的值</returns>
        public virtual string ReturnZRowZColContent(string sql, object[] paraArray)
        {
            return null;
        }
        #endregion

        #region [方法]返回与Sql语句相应的数据表的第一行第一列的值(Int)
        /// <summary>
        /// 返回与Sql语句相应的数据表的第一行第一列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>结果数据表第一行第一列的值</returns>
        public virtual int ReturnZRowZColContentInt(string sql)
        {
            return -1;
        }
        #endregion

        #region [方法]返回与Sql语句相应的数据表的第一行第一列的值(Int)<参数形式>
        /// <summary>
        /// 返回与Sql语句相应的数据表的第一行第一列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraArray">参数数组</param>
        /// <returns>结果数据表第一行第一列的值</returns>
        public virtual int ReturnZRowZColContentInt(string sql, object[] paraArray)
        {
            return -1;
        }
        #endregion

        #region [方法]批量更新数据库数据表
        /// <summary>
        /// 批量更新数据库数据表
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="TableName">需更新的数据表</param>
        public virtual void UpdateTable(string sql, System.Data.DataTable TableName)
        {

        }
        #endregion

        #region [方法]批量更新数据库数据表<参数形式>
        /// <summary>
        /// 批量更新数据库数据表
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraArray">参数数组</param>
        /// <param name="TableName">需更新的数据表</param>
        public virtual void UpdateTable(string sql, object[] paraArray, System.Data.DataTable TableName)
        {

        }
        #endregion

        #region [方法]命令方式批量更新数据库数据库
        /// <summary>
        /// 命令方式批量更新数据库数据库
        /// </summary>
        /// <param name="insertCommand">InsertCommand命令</param>
        /// <param name="updateCommand">UpdateCommand命令</param>
        /// <param name="deleteCommand">DeleteCommand命令</param>
        /// <param name="TableName">需更新的数据表</param>
        /// <returns>是否执行成功 True:成功 False:失败</returns>
        public virtual bool UpdateTableWithCommand(object insertCommand, object updateCommand, object deleteCommand, DataTable TableName)
        {
            return true;
        }
        #endregion

        #region [方法]执行存储过程<参数形式>
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="StoredProcedureName">存储过程名称</param>
        /// <param name="paraArray">参数数组</param>
        public virtual void ExecuteProcedure(string StoredProcedureName, object[] paraArray)
        {

        }
        #endregion

        #region [方法]执行存储过程<返回DataSet><参数形式>
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="StoredProcedureName">存储过程名称</param>
        /// <param name="paraArray">参数数组</param>
        /// <returns>结果数据集</returns>
        public virtual DataSet ExecuteProcedureDs(string StoredProcedureName, object[] paraArray)
        {
            return null;
        }
        #endregion

        #region [方法]执行一批只返回影响行数的SQL语句<事务>
        /// <summary>
        /// 执行一批只返回影响行数的SQL语句(事务)
        /// </summary>
        /// <param name="sqlList">Sql语句列表</param>
        public virtual void ExecuteSqlWidthTransaction(SqlStruct[] sqlList)
        {

        }

        /// <summary>
        /// 执行一批只返回影响行数的SQL语句(事务)
        /// </summary>
        /// <param name="sqlList">Sql语句列表</param>
        public virtual void ExecuteSqlWidthTransaction(List<SqlStruct> sqlList)
        { 
        
        }
        #endregion

        public virtual void ExecuteSqlList(List<SqlStruct> sqlList)
        { 
        
        }
    }
}
#region 命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
#endregion

namespace XL.DB.DbOperaClass
{
    /// <summary>
    /// SQL数据库操作类
    /// </summary>
    public class SqlDbConnClass : DbConnClass
    {
        #region [方法]构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connectionString">数据库连接串 例:Server=数据库IP;uid=用户名;pwd=密码;DataBase=数据库</param>
        public SqlDbConnClass(string connectionString)
        {
            this.ConnectionString = connectionString;
        }
        #endregion

        #region [方法]打开数据库连接
        /// <summary>
        /// 打开数据库连接
        /// </summary>
        public override object OpenDbConnection()
        {
            SqlConnection sqlConnection = null;
            try
            {
                sqlConnection = new SqlConnection(this.ConnectionString);
                sqlConnection.Open();
            }
            catch (Exception xe)
            {
                sqlConnection = null;
                throw new Exception(xe.Message);
            }
            return sqlConnection;
        }
        #endregion

        #region [方法]关闭数据库连接
        /// <summary>
        /// 关闭数据库连接
        /// </summary>
        public override void CloseDbConnection(object dbConnectObj)
        {
            try
            {
                SqlConnection dbConn = (SqlConnection)dbConnectObj;
                if (dbConn != null)
                {
                    dbConn.Close();
                }
            }
            catch
            {
                //Error
            }
        }
        #endregion

        #region [方法]执行只返回影响行数的Sql语句
        /// <summary>
        /// 执行只返回影响行数的Sql语句
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>影响行数</returns>
        public override int ExecuteNonQuery(string sql)
        {
            int RowsNum = 0;
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
                RowsNum = sqlCommand.ExecuteNonQuery();
                sqlCommand.Dispose();
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
            return RowsNum;
        }
        #endregion

        #region [方法]执行只返回影响行数的Sql语句<参数形式>
        /// <summary>
        /// 执行只返回影响行数的Sql语句
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraList">参数数组</param>
        /// <returns>影响行数</returns>
        public int ExecuteNonQuery(string sql, SqlParameter[] paraList)
        {
            int RowsNum = 0;
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
                foreach (SqlParameter para in paraList)
                {
                    sqlCommand.Parameters.Add(para);
                }
                RowsNum = sqlCommand.ExecuteNonQuery();
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
            return RowsNum;
        }

        /// <summary>
        /// 执行只返回影响行数的Sql语句
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraArray">参数数组</param>
        /// <returns>影响行数</returns>
        public override int ExecuteNonQuery(string sql, object[] paraArray)
        {
            SqlParameter[] paraList = null;
            if (paraArray is DbParameterStruct[])
            {
                paraList = new SqlParameter[paraArray.Length];
                for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
                {
                    DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
                    paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
                }
            }
            else
            {
                paraList = (SqlParameter[])paraArray;
            }
            return this.ExecuteNonQuery(sql, paraList);
        }
        #endregion

        #region [方法]返回与Sql语句相对应的数据表
        /// <summary>
        /// 返回与Sql语句相对应的数据表
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>结果数据表</returns>
        public override DataTable ReturnDataTable(string sql)
        {
            DataTable dataTable = new DataTable();
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConnection);
                sqlDataAdapter.Fill(dataTable);
                sqlDataAdapter.Dispose();
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
            return dataTable;
        }
        #endregion

        #region [方法]返回与Sql语句相对应的数据表<参数形式>
        /// <summary>
        /// 返回与Sql语句相对应的数据表
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraList">参数数组</param>
        /// <returns>结果数据表</returns>
        public DataTable ReturnDataTable(string sql, SqlParameter[] paraList)
        {
            DataTable dataTable = new DataTable();
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConnection);
                foreach (SqlParameter para in paraList)
                {
                    sqlDataAdapter.SelectCommand.Parameters.Add(para);
                }
                sqlDataAdapter.Fill(dataTable);
                sqlDataAdapter.Dispose();
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
            return dataTable;
        }

        /// <summary>
        /// 返回与Sql语句相对应的数据表
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraArray">参数数组</param>
        /// <returns>结果数据表</returns>
        public override DataTable ReturnDataTable(string sql, object[] paraArray)
        {
            SqlParameter[] paraList = null;
            if (paraArray is DbParameterStruct[])
            {
                paraList = new SqlParameter[paraArray.Length];
                for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
                {
                    DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
                    paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
                }
            }
            else
            {
                paraList = (SqlParameter[])paraArray;
            }
            return this.ReturnDataTable(sql, paraList);
        }
        #endregion

        #region [方法]返回包含单个数据表的数据集
        /// <summary>
        /// 返回包含单个数据表的数据集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>结果数据集</returns>
        public override DataSet ReturnDataSet(string sql)
        {
            DataSet dataSet = new DataSet();
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConnection);
                sqlDataAdapter.Fill(dataSet);
                sqlDataAdapter.Dispose();
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
            return dataSet;
        }
        #endregion

        #region [方法]返回包含单个数据表的数据集<参数形式>
        /// <summary>
        /// 返回包含单个数据表的数据集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraList">参数数组</param>
        /// <returns>结果数据集</returns>
        public DataSet ReturnDataSet(string sql, SqlParameter[] paraList)
        {
            DataSet dataSet = new DataSet();
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConnection);
                foreach (SqlParameter para in paraList)
                {
                    sqlDataAdapter.SelectCommand.Parameters.Add(para);
                }
                sqlDataAdapter.Fill(dataSet);
                sqlDataAdapter.Dispose();
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
            return dataSet;
        }

        /// <summary>
        /// 返回包含单个数据表的数据集
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraArray">参数数组</param>
        /// <returns>结果数据集</returns>
        public override DataSet ReturnDataSet(string sql, object[] paraArray)
        {
            SqlParameter[] paraList = null;
            if (paraArray is DbParameterStruct[])
            {
                paraList = new SqlParameter[paraArray.Length];
                for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
                {
                    DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
                    paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
                }
            }
            else
            {
                paraList = (SqlParameter[])paraArray;
            }
            return this.ReturnDataSet(sql, paraList);
        }
        #endregion

        #region [方法]返回与Sql语句相应的数据表的第一行第一列的值
        /// <summary>
        /// 返回与Sql语句相应的数据表的第一行第一列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>结果数据表第一行第一列的值</returns>
        public override string ReturnZRowZColContent(string sql)
        {
            string ContentStr = null;
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
                ContentStr = sqlCommand.ExecuteScalar().ToString();
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
            return ContentStr;
        }
        #endregion

        #region [方法]返回与Sql语句相应的数据表的第一行第一列的值<参数形式>
        /// <summary>
        /// 返回与Sql语句相应的数据表的第一行第一列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraList">参数数组</param>
        /// <returns>结果数据表第一行第一列的值</returns>
        public string ReturnZRowZColContent(string sql, SqlParameter[] paraList)
        {
            string ContentStr = null;
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
                foreach (SqlParameter para in paraList)
                {
                    sqlCommand.Parameters.Add(para);
                }
                ContentStr = sqlCommand.ExecuteScalar().ToString();
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
            return ContentStr;
        }

        /// <summary>
        /// 返回与Sql语句相应的数据表的第一行第一列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraArray">参数数组</param>
        /// <returns>结果数据表第一行第一列的值</returns>
        public override string ReturnZRowZColContent(string sql, object[] paraArray)
        {
            SqlParameter[] paraList = null;
            if (paraArray is DbParameterStruct[])
            {
                paraList = new SqlParameter[paraArray.Length];
                for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
                {
                    DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
                    paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
                }
            }
            else
            {
                paraList = (SqlParameter[])paraArray;
            }
            return this.ReturnZRowZColContent(sql, paraList);
        }
        #endregion

        #region [方法]返回与Sql语句相应的数据表的第一行第一列的值(Int)
        /// <summary>
        /// 返回与Sql语句相应的数据表的第一行第一列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>结果数据表第一行第一列的值</returns>
        public override int ReturnZRowZColContentInt(string sql)
        {
            int ContentInt = 0;
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
                ContentInt = int.Parse(sqlCommand.ExecuteScalar().ToString());
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
            return ContentInt;
        }
        #endregion

        #region [方法]返回与Sql语句相应的数据表的第一行第一列的值(Int)<参数形式>
        /// <summary>
        /// 返回与Sql语句相应的数据表的第一行第一列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraList">参数数组</param>
        /// <returns>结果数据表第一行第一列的值</returns>
        public int ReturnZRowZColContentInt(string sql, SqlParameter[] paraList)
        {
            int ContentInt = 0;
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
                foreach (SqlParameter para in paraList)
                {
                    sqlCommand.Parameters.Add(para);
                }
                ContentInt = int.Parse(sqlCommand.ExecuteScalar().ToString());
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
            return ContentInt;
        }

        /// <summary>
        /// 返回与Sql语句相应的数据表的第一行第一列的值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraArray">参数数组</param>
        /// <returns>结果数据表第一行第一列的值</returns>
        public override int ReturnZRowZColContentInt(string sql, object[] paraArray)
        {
            SqlParameter[] paraList = null;
            if (paraArray is DbParameterStruct[])
            {
                paraList = new SqlParameter[paraArray.Length];
                for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
                {
                    DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
                    paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
                }
            }
            else
            {
                paraList = (SqlParameter[])paraArray;
            }
            return this.ReturnZRowZColContentInt(sql, paraList);
        }
        #endregion

        #region [方法]批量更新数据库数据表
        /// <summary>
        /// 批量更新数据库数据表
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="TableName">需更新的数据表</param>
        public override void UpdateTable(string sql, System.Data.DataTable TableName)
        {
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
                SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
                sqlDataAdapter.SelectCommand = new SqlCommand(sql, sqlConnection);
                sqlDataAdapter.UpdateCommand = sqlCommandBuilder.GetUpdateCommand();
                sqlDataAdapter.Update(TableName);
                sqlDataAdapter.Dispose();
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
        }
        #endregion

        #region [方法]批量更新数据库数据表<参数形式>
        /// <summary>
        /// 批量更新数据库数据表
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraList">参数数组</param>
        /// <param name="TableName">需更新的数据表</param>
        public void UpdateTable(string sql, SqlParameter[] paraList, System.Data.DataTable TableName)
        {
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
                SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
                sqlDataAdapter.SelectCommand = new SqlCommand(sql, sqlConnection);
                foreach (SqlParameter para in paraList)
                {
                    sqlDataAdapter.SelectCommand.Parameters.Add(para);
                }
                sqlDataAdapter.UpdateCommand = sqlCommandBuilder.GetUpdateCommand();
                sqlDataAdapter.Update(TableName);
                sqlDataAdapter.Dispose();
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
        }

        /// <summary>
        /// 批量更新数据库数据表
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paraArray">参数数组</param>
        /// <param name="TableName">需更新的数据表</param>
        public override void UpdateTable(string sql, object[] paraArray, DataTable TableName)
        {
            SqlParameter[] paraList = null;
            if (paraArray is DbParameterStruct[])
            {
                paraList = new SqlParameter[paraArray.Length];
                for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
                {
                    DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
                    paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
                }
            }
            else
            {
                paraList = (SqlParameter[])paraArray;
            }
            this.UpdateTable(sql, paraList, TableName);
        }
        #endregion

        #region [方法]命令方式批量更新数据库数据库
        /// <summary>
        /// 命令方式批量更新数据库数据库
        /// </summary>
        /// <param name="insertCommand">InsertCommand命令</param>
        /// <param name="updateCommand">UpdateCommand命令</param>
        /// <param name="deleteCommand">DeleteCommand命令</param>
        /// <param name="TableName">需更新的数据表</param>
        /// <returns>是否执行成功 True:成功 False:失败</returns>
        public override bool UpdateTableWithCommand(object insertCommand, object updateCommand, object deleteCommand, DataTable TableName)
        {
            bool IsOk = false;
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlCommand InsertCommand = null;
                SqlCommand UpdateCommand = null;
                SqlCommand DeleteCommand = null;
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
                //检查Command命令
                if (insertCommand != null)
                {
                    InsertCommand = (SqlCommand)insertCommand;
                    InsertCommand.Connection = sqlConnection;
                    sqlDataAdapter.InsertCommand = InsertCommand;
                }
                if (updateCommand != null)
                {
                    UpdateCommand = (SqlCommand)updateCommand;
                    UpdateCommand.Connection = sqlConnection;
                    sqlDataAdapter.UpdateCommand = UpdateCommand;
                }
                if (deleteCommand != null)
                {
                    DeleteCommand = (SqlCommand)deleteCommand;
                    DeleteCommand.Connection = sqlConnection;
                    sqlDataAdapter.DeleteCommand = DeleteCommand;
                }
                //执行操作命令
                sqlDataAdapter.Update(TableName);
                sqlDataAdapter.Dispose();

                IsOk = true;
            }
            catch (Exception xe)
            {
                IsOk = false;
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
            return IsOk;
        }
        #endregion

        #region [方法]执行存储过程<参数形式>
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="StoredProcedureName">存储过程名称</param>
        /// <param name="paraList">参数数组</param>
        public void ExecuteProcedure(string StoredProcedureName, SqlParameter[] paraList)
        {
            bool IsHavePara = true;
            if (paraList == null || paraList.Length == 0) IsHavePara = false;
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandType = CommandType.StoredProcedure; //存储过程类型
                sqlCommand.CommandText = StoredProcedureName;
                if (IsHavePara)
                {
                    if (paraList == null) return;
                    for (int iLoop = 0; iLoop < paraList.Length; iLoop++)
                    {
                        sqlCommand.Parameters.Add(paraList[iLoop]);
                    }
                }
                sqlCommand.ExecuteNonQuery();
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
        }

        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="StoredProcedureName">存储过程名称</param>
        /// <param name="paraArray">参数数组</param>
        public override void ExecuteProcedure(string StoredProcedureName, object[] paraArray)
        {
            SqlParameter[] paraList = null;
            if (paraArray is DbParameterStruct[])
            {
                paraList = new SqlParameter[paraArray.Length];
                for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
                {
                    DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
                    paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
                }
            }
            else
            {
                paraList = (SqlParameter[])paraArray;
            }
            this.ExecuteProcedure(StoredProcedureName, paraList);
        }
        #endregion

        #region [方法]执行存储过程<返回DataSet><参数形式>
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="StoredProcedureName">存储过程名称</param>
        /// <param name="paraList">参数数组</param>
        /// <returns>结果数据集</returns>
        public DataSet ExecuteProcedureDs(string StoredProcedureName, SqlParameter[] paraList)
        {
            bool IsHavePara = true;
            if (paraList == null || paraList.Length == 0) IsHavePara = false;
            SqlConnection sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandType = CommandType.StoredProcedure; //存储过程类型
                sqlCommand.CommandText = StoredProcedureName;
                if (IsHavePara)
                {
                    if (paraList == null) return null;
                    for (int iLoop = 0; iLoop < paraList.Length; iLoop++)
                    {
                        sqlCommand.Parameters.Add(paraList[iLoop]);
                    }
                }
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                DataSet ds = new DataSet();
                sqlDataAdapter.Fill(ds);
                return ds;
            }
            catch (Exception xe)
            {
                throw new Exception(xe.Message);
            }
            finally
            {
                this.CloseDbConnection(sqlConnection);
            }
        }

        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="StoredProcedureName">存储过程名称</param>
        /// <param name="paraArray">参数数组</param>
        /// <returns>结果数据集</returns>
        public override DataSet ExecuteProcedureDs(string StoredProcedureName, object[] paraArray)
        {
            SqlParameter[] paraList = null;
            if (paraArray is DbParameterStruct[])
            {
                paraList = new SqlParameter[paraArray.Length];
                for (int iLoop = 0; iLoop < paraArray.Length; iLoop++)
                {
                    DbParameterStruct para = (DbParameterStruct)paraArray[iLoop];
                    paraList[iLoop] = new SqlParameter("@" + para.ParaName, para.ParaValue);
                }
            }
            else
            {
                paraList = (SqlParameter[])paraArray;
            }
            return this.ExecuteProcedureDs(StoredProcedureName, paraList);
        }
        #endregion

        #region [方法]执行一批只返回影响行数的SQL语句<事务>
        /// <summary>
        /// 执行一批只返回影响行数的SQL语句(事务)
        /// </summary>
        /// <param name="sqlList">Sql语句列表</param>
        public override void ExecuteSqlWidthTransaction(SqlStruct[] sqlList)
        {
            SqlConnection sqlConnection = null;
            SqlTransaction sqlTransaction = null;
            SqlCommand sqlCommand = null;
            sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                sqlTransaction = sqlConnection.BeginTransaction();
                sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.Transaction = sqlTransaction;
                for (int iLoop = 0; iLoop < sqlList.Length; iLoop++)
                {
                    sqlCommand.CommandText = sqlList[iLoop].sql;
                    if (sqlList[iLoop].para != null)
                    {
                        if (sqlList[iLoop].para is DbParameterStruct[])
                        {
                            DbParameterStruct[] paraArray = (DbParameterStruct[])sqlList[iLoop].para;
                            foreach (DbParameterStruct para in paraArray)
                            {
                                sqlCommand.Parameters.Add(new SqlParameter("@" + para.ParaName, para.ParaValue));
                            }
                        }
                        else
                        {
                            foreach (SqlParameter para in sqlList[iLoop].para)
                            {
                                sqlCommand.Parameters.Add(para);
                            }
                        }
                    }
                    sqlCommand.ExecuteNonQuery();
                    sqlCommand.Parameters.Clear();
                }
                sqlTransaction.Commit();
                sqlTransaction.Dispose();
            }
            catch (Exception xe)
            {
                if (sqlTransaction != null)
                {
                    sqlTransaction.Rollback();
                    sqlTransaction.Dispose();
                }
                throw new Exception(xe.Message);
            }
            finally
            {
                if (sqlConnection != null) this.CloseDbConnection(sqlConnection);
            }
        }

        /// <summary>
        /// 执行一批只返回影响行数的SQL语句(事务)
        /// </summary>
        /// <param name="sqlList">Sql语句列表</param>
        public override void ExecuteSqlWidthTransaction(List<SqlStruct> sqlList)
        {
            SqlConnection sqlConnection = null;
            SqlTransaction sqlTransaction = null;
            SqlCommand sqlCommand = null;
            sqlConnection = (SqlConnection)this.OpenDbConnection();
            try
            {
                sqlTransaction = sqlConnection.BeginTransaction();
                sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.Transaction = sqlTransaction;
                for (int iLoop = 0; iLoop < sqlList.Count; iLoop++)
                {
                    sqlCommand.CommandText = sqlList[iLoop].sql;
                    if (sqlList[iLoop].para != null)
                    {
                        if (sqlList[iLoop].para is DbParameterStruct[])
                        {
                            DbParameterStruct[] paraArray = (DbParameterStruct[])sqlList[iLoop].para;
                            foreach (DbParameterStruct para in paraArray)
                            {
                                sqlCommand.Parameters.Add(new SqlParameter("@" + para.ParaName, para.ParaValue));
                            }
                        }
                        else
                        {
                            foreach (SqlParameter para in sqlList[iLoop].para)
                            {
                                sqlCommand.Parameters.Add(para);
                            }
                        }
                    }
                    sqlCommand.ExecuteNonQuery();
                    sqlCommand.Parameters.Clear();
                }
                sqlTransaction.Commit();
                sqlTransaction.Dispose();
            }
            catch (Exception xe)
            {
                if (sqlTransaction != null)
                {
                    sqlTransaction.Rollback();
                    sqlTransaction.Dispose();
                }
                throw new Exception(xe.Message);
            }
            finally
            {
                if (sqlConnection != null) this.CloseDbConnection(sqlConnection);
            }
        }
        #endregion
    }
}

 

原网站

版权声明
本文为[心蓝168]所创,转载请带上原文链接,感谢
https://blog.csdn.net/hyjchina/article/details/114852803