• 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 Query() 
            {
                List Result = null;
                Result = db.Queryable().ToList();
                return Result;
            }

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


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

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


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

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

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

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

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

            ///


            /// 通过sqlcommand 获取sql语句
            ///

            ///
            ///
            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;  
                
            } 
        }

  • 相关阅读:
    使用Python语言制作贪吃蛇游戏,并制作成为exe可执行文件
    nodejs+vue高校校图书馆elementui
    【附源码】计算机毕业设计SSM石家庄铁道大学影视资料管理系统
    运营商大数据,三网融合大数据,联通大数据,移动大数据
    SpringBoot Web 分层解耦
    苍穹外卖(二)新增员工及项目细节
    【Linux】基础IO —— 缓冲区深度剖析
    控制文件全部损坏的解决方案
    2.ClickHouse系列之特点介绍
    聊聊FASTER和进程内混合缓存
  • 原文地址:https://blog.csdn.net/moonshineidolon/article/details/126546298