C#三层架构通用数据库访问类SQLHerper总结

写入错误日志

写入错误日志

     /// 
    /// 写入错误日志
    /// 
    /// 
    private static void WriterLog(string log)
    {
        FileStream fs = new FileStream("sqlhelper.log", FileMode.Append);
        StreamWriter sw = new StreamWriter(fs);
        sw.WriteLine(System.DateTime.Now.ToString()+"  "+log);
        sw.Close();
        fs.Close();
    }

封装格式化SQL语句执行各种的方法

封装格式化SQL语句执行各种的方法

  #region 封装格式化SQL语句执行各种的方法
        /// 
        /// 格式化SQL语句增删改
        /// 
        /// sql语句
        /// 1:执行成功 ;-1执行失败
        public static int Update(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql,conn);
            try
            {
                conn.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //将错误信息写入到错误日志中
                WriterLog("在执行 public static int Update(string sql)时出现错误,错误信息:" + ex.Message.ToString());
                throw new Exception( ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }

        /// 
        /// 格式化SQL语句查找
        /// 
        /// sql语句
        /// 返回sqlDataReader对象
        public static SqlDataReader GetDataReader(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                WriterLog("在执行public static SqlDataReader GetDataReader(string sql)时出现错误,错误信息:" + ex.Message.ToString());
                throw new Exception( ex.Message);
            }
        }

        /// 
        /// 格式化SQL语句通过DataSet查询
        /// 
        /// 
        /// 
        public static DataSet GetDataSet(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql,conn);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet da = new DataSet();
            try
            {
                conn.Open();
                sda.Fill(da);
                return da;
            }
            catch (Exception ex)
            {
                WriterLog("在执行public static DataSet GetDataSet(string sql)时出现错误,错误信息:" + ex.Message.ToString());
                throw new Exception( ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }

        /// 
        /// 格式化SQL语句获取单行信息
        /// 
        /// 
        /// 返回单行信息,该信息为Object类型
        public static object GetSingleReslut(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                return cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                //将错误信息写入到错误日志中
                WriterLog("在执行 public static object ReaderSingRow(string sql)时出现错误,错误信息:" + ex.Message.ToString());
                throw new Exception(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion

封装带参SQL语句执行各种的方法

封装带参SQL语句执行各种的方法

   #region 封装带参SQL语句执行各种的方法
        /// 
        /// 封装带参SQL语句增删改
        /// 
        /// sql语句
        /// Sql参数数组
        /// 1:执行成功 ;-1执行失败
        public static int Update(string sql,SqlParameter[] param)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                cmd.Parameters.AddRange(param);
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //将错误信息写入到错误日志中
                WriterLog("在执行 public static int Update(string sql,SqlParameter[]param)时出现错误,错误信息:" + ex.Message.ToString());
                //throw new Exception(ex.Message);
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }

        /// 
        /// 封装带参SQL语句查找
        /// 
        /// sql语句
       /// Sql参数数组
        /// 返回sqlDataReader对象
        public static SqlDataReader GetDataReader(string sql,SqlParameter[] param)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                cmd.Parameters.AddRange(param);
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                WriterLog("在执行public static SqlDataReader GetDataReader(string sql,SqlParameter[]param)时出现错误,错误信息:" + ex.Message.ToString());
                throw new Exception(ex.Message);
            }
        }

        /// 
        /// 封装带参SQL语句通过DataSet查询
        /// 
        /// sql语句
        /// Sql参数数组
        /// 
        public static DataSet GetDataSet(string sql,SqlParameter[] param)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet da = new DataSet();
            try
            {
                conn.Open();
                cmd.Parameters.AddRange(param);
                sda.Fill(da);
                return da;
            }
            catch (Exception ex)
            {
                WriterLog("在执行public static DataSet GetDataSet(string sql,SqlParameter[]param)时出现错误,错误信息:" + ex.Message.ToString());
                throw new Exception(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }

        /// 
        /// 封装带参SQL语句获取单行信息
        /// 
        /// sql语句
        /// Sql参数数组
        /// 返回单行信息,该信息为Object类型
        public static object GetSingleReslut(string sql,SqlParameter[]param)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                cmd.Parameters.AddRange(param);
                return cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                //将错误信息写入到错误日志中
                WriterLog("在执行 public static object ReaderSingRow(string sql,SqlParameter[]param)时出现错误,错误信息:" + ex.Message.ToString());
                throw new Exception(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }


        /// 
        /// 启动事务提交多条带参数的SQL语句
        /// 
        /// 主表SQL语句
        /// 主表参数
        /// 明细表SQL语句
        /// 明细表SQL语句对应的参数数组集合
        /// true:执行成功
        public static bool UpdateByTran(string Minsql, SqlParameter[] Mainparam,string detailSql,List<SqlParameter[]>detailparam)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            try
            {
                conn.Open();
                //开启事务
                cmd.Transaction = conn.BeginTransaction();
                if(Minsql!=null&&Minsql.Length!=0)
                {
                    cmd.CommandText = Minsql;
                    cmd.Parameters.AddRange(Mainparam);
                    cmd.ExecuteNonQuery();
                }
                cmd.CommandText = detailSql;
                foreach (SqlParameter[] param in detailparam)
                {
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddRange(param);
                    cmd.ExecuteNonQuery();
                }
                //提交事务
                cmd.Transaction.Commit();
                return true;
            }
            catch (Exception ex)
            {
                if (cmd.Transaction != null)
                {
                    cmd.Transaction.Rollback();
                }
                //将错误信息写入到错误日志中
                WriterLog("在执行UpdateByTran(string Minsql, SqlParameter[] Mainparam,string detailSql,Listdetailparam)时出现错误,错误信息:" + ex.Message.ToString());
                throw new Exception(ex.Message);
            }
            finally
            {
                if(cmd.Transaction!=null)
                {
                    cmd.Transaction = null;
                }
                conn.Close();
            }
        }
        #endregion

封装存储过程SQL语句执行各种的方法

封装存储过程SQL语句执行各种的方法

 #region 封装存储过程SQL语句执行各种的方法
        /// 
        /// 封装存储过程SQL语句增删改
        /// 
        /// sql语句
        /// Sql参数数组
        /// 1:执行成功 ;-1执行失败
        public static int UpdateByProcedure(string spName, SqlParameter[] param)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(spName, conn);
            try
            {
                conn.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(param);
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //将错误信息写入到错误日志中
                WriterLog("在执行UpdateByProcedure(string spName, SqlParameter[] param)时出现错误,错误信息:" + ex.Message.ToString());
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }

        /// 
        /// 封装存储过程SQL语句查找
        /// 
        /// sql语句
        /// Sql参数数组
        /// 返回sqlDataReader对象
        public static SqlDataReader GetDataReaderByProcedure(string spName, SqlParameter[] param)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(spName, conn);
            try
            {
                conn.Open();
                cmd.Parameters.AddRange(param);
                cmd.CommandType = CommandType.StoredProcedure;
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                WriterLog("在执行public static SqlDataReader GetDataReaderByProcedure(string spName, SqlParameter[] param)时出现错误,错误信息:" + ex.Message.ToString());
                throw new Exception(ex.Message);
            }
        }

        /// 
        /// 封装存储过程SQL语句获取单行信息
        /// 
        /// sql语句
        /// Sql参数数组
        /// 返回单行信息,该信息为Object类型
        public static object GetSingleReslutByProcedure(string spName, SqlParameter[] param)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(spName, conn);
            try
            {
                conn.Open();
                cmd.Parameters.AddRange(param);
                cmd.CommandType = CommandType.StoredProcedure;
                return cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                //将错误信息写入到错误日志中
                WriterLog("在执行    public static object GetSingleReslutByProcedure(string spName, SqlParameter[] param)时出现错误,错误信息:" + ex.Message.ToString());
                throw new Exception(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }


        /// 
        /// 启用事务调用带参数的存储过程
        /// 
        /// 存储过程名称
        /// 存储过程参数数组集合
        /// 返回结果 True:执行成功
        public static bool UpdateByTran( string ProdetrueName, List<SqlParameter[]> paramArray)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            try
            {
                conn.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                //开启事务
                cmd.Transaction = conn.BeginTransaction();
                cmd.CommandText = ProdetrueName;
                foreach (SqlParameter[] param in paramArray)
                {
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddRange(param);
                    cmd.ExecuteNonQuery();
                }
                //提交事务
                cmd.Transaction.Commit();
                return true;
            }
            catch (Exception ex)
            {
                if (cmd.Transaction != null)
                {
                    cmd.Transaction.Rollback();
                }
                //将错误信息写入到错误日志中
                WriterLog("在执行 public static bool UpdateByTran( string ProdetrueName, List paramArray)时出现错误,错误信息:" + ex.Message.ToString());
                throw new Exception(ex.Message);
            }
            finally
            {
                if (cmd.Transaction != null)
                {
                    cmd.Transaction = null;
                }
                conn.Close();
            }
        }   
        #endregion

你可能感兴趣的:(c#基础)