DataBaseHelper sqlsugar

    using System.Collections.Generic;
    using System;
    using System.Diagnostics;
    using SqlSugar;
    using System.Linq.Expressions;
    using InterfaceSimulator.Utility;

    public sealed class DataBaseHelper
    {

        public  SqlSugarClient db; 
        public DataBaseHelper()
        {

            db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlConnString"].ConnectionString,
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true

            });

            db.Aop.OnLogExecuted = (sql, pra) =>
                {                    
                    Console.WriteLine($"sql语句:{sql}");

                    if (pra.Length > 0)
                    {
                        string sqlcmd = this.GetExecMsSqlCommandText(sql, pra);
                     
                    }
                    else
                    {
                        string sqlcmd = this.GetExecMsSqlCommandText(sql);
                    }
                };
            }
         
        
        public List<T> Query<T>() 
        {
            List<T> Result = null;
            Result = db.Queryable<T>().ToList();
            return Result;
        }

        public List<T> Query<T>(Expression<Func<T, bool>> whereExp)
        {
            List<T> Result = null;
            Result = db.Queryable<T>().Where(whereExp).ToList();
            return Result;
        }


        public List<T> Query<T>(Expression<Func<T, bool>> whereExp,PageModel pageModel, Expression<Func<T,object>> orderByExp=null,OrderByType orderByType= OrderByType.Asc)
        {
            List<T> Result = null; 

            Result = db.Queryable<T>().OrderBy(orderByExp,orderByType).Where(whereExp).ToPageList(pageModel.PageIndex,pageModel.PageSize);      return Result;
        }


        public   int Add<T>(T info) where T : class, new()
        {
            int result = -1; 
            result = db.Insertable(info).ExecuteCommand();
            return result;
        }

        public   int Delete<T>(dynamic id) where T : class, new()
        {
            int result = -1;
            result = db.Deleteable<T>().In(id).ExecuteCommand();
            return result;
        }

        public int Delete<T>(Expression<Func<T, bool>> whereExp) where T : class, new()
        {
            int result = -1;
            result = db.Deleteable<T>().Where( whereExp ).ExecuteCommand();
            return result;
        }

        public   int Update<T>(T info) where T : class, new()
        {
            int result = -1;

            result = db.Updateable<T>(info).ExecuteCommand();
            return result;
        }
 

        /// <summary>
        /// 通过sqlcommand 获取sql语句
        /// </summary>
        /// <param name="sqlCommand"></param>
        /// <returns></returns>
        private string GetExecMsSqlCommandText(string sql, SugarParameter[] param = null)
        {

            string sqlCmd = sql; 

            if (param != null)
            {
                foreach (var p in param)
                {
                    string name = p.ParameterName;
                    object value = p.Value;

                    string repValue = "Null";
                    if (p.Value == DBNull.Value)
                    { }
                    else
                    {
                        if ((p.DbType == System.Data.DbType.Int32) || (p.DbType == System.Data.DbType.Int64))
                        {
                            repValue = value.ToString();
                        }
                        else if (p.DbType == System.Data.DbType.String)
                        {
                            repValue = string.Format("'{0}'", value.ToString());
                        }
                        else if (p.DbType == System.Data.DbType.DateTime)
                        {
                            repValue = string.Format("'{0}'", ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"));
                        }
                    }


                    sqlCmd = sqlCmd.Replace(name, repValue);
                }
            }

            Logger.Debug("exec sql:" + sql); 
            return sqlCmd;  
            
        } 
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值