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

  • 相关阅读:
    Oracle RAC移动本地数据文件到ASM中
    日期模拟器(改变check即可 手切日期)
    vscode不显示横滚动条处理
    Linux的介绍与应用
    18.匿名内部类【20220629】
    go介绍32——字符串操作
    【软考】-- 操作系统(中)
    【Rust日报】2022-08-08 基于Rust能力的Linux runtime
    Spark SQL
    Docker基础知识总结
  • 原文地址:https://blog.csdn.net/moonshineidolon/article/details/126546298