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

  • 相关阅读:
    Postman下发流表至Opendaylight
    构建 JavaScript ChatGPT 插件
    常见IO模型(非常详细)
    【矩阵分析】线性空间、λ矩阵、内积空间、Hermite矩阵、矩阵分解、矩阵范数、矩阵函数
    Keil C51 - ERROR L107: ADDRESS SPACE OVERFLOW
    Google开源offload友好协议PSP,目前已正式部署到生产中
    高等数学(第七版)同济大学 习题4-4(后14题) 个人解答
    C++项目:【负载均衡式在线OJ】
    扩展windows 10 文件夹文件路径位数
    MCU软核 3. Xilinx Artix7上运行cortex-m3软核
  • 原文地址:https://blog.csdn.net/moonshineidolon/article/details/126546298