1、实例化DB

public static SqlSugarClient GetDB(string s)
{
var ssc = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = s, //必填
DbType = SqlSugar.DbType.SqlServer, //必填
IsAutoCloseConnection = true
});
ssc.SetExpMethods();
ssc.SetEvents();
return ssc;
}

2、自定义扩展方法

public static void SetExpMethods(this SqlSugarClient ssc)
{
var expMethods = new List<SqlFuncExternal>();
expMethods.Add(new SqlFuncExternal()
{
UniqueMethodName = "CastToFloat",
MethodValue = (expInfo, dbType, expContext) =>
{
if (dbType == DbType.SqlServer)
return string.Format("CAST({0} AS float)", expInfo.Args[0].MemberName);
else
throw new Exception("未实现");
}
});
   ssc.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
{
SqlFuncServices = expMethods //set ext method
};
}

3、AOP输出错误的sql

public static void SetEvents(this SqlSugarClient ssc)
{
ssc.Aop.OnError = error =>
{
Regex reg = new Regex(@"[^0-9]+");
var sql = error.Sql;
var pars = error.Parametres as SugarParameter[];
sql = pars.OrderByDescending(o => reg.Replace(o.ParameterName, "").ObjToInt()).ThenByDescending(o => o.ParameterName.Length).Aggregate(sql, (current, p) => current.Replace(p.ParameterName, (p.DbType == System.Data.DbType.Decimal ||
p.DbType == System.Data.DbType.Double ||
p.DbType == System.Data.DbType.Int16 ||
p.DbType == System.Data.DbType.Int32 ||
p.DbType == System.Data.DbType.Int64 ||
p.DbType == System.Data.DbType.Single ||
p.DbType == System.Data.DbType.VarNumeric ||
p.DbType == System.Data.DbType.UInt16 ||
p.DbType == System.Data.DbType.UInt32 ||
p.DbType == System.Data.DbType.UInt64
) ? (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("{0}", p.Value) : (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("'{0}'", p.Value))); }; }

4、输出sql

public static string ToSqlString<T>(this ISugarQueryable<T> ISugarQueryable)
{
var tosql = ISugarQueryable.Clone().ToSql();
Regex reg = new Regex(@"[^0-9]+");
var sql = tosql.Key;
var pars = tosql.Value;
if (pars == null)
{
return sql;
}
sql = pars.OrderByDescending(o => reg.Replace(o.ParameterName, "").ObjToInt()).ThenByDescending(o => o.ParameterName.Length).Aggregate(sql, (current, p) => current.Replace(p.ParameterName, (p.DbType == System.Data.DbType.Decimal ||
p.DbType == System.Data.DbType.Double ||
p.DbType == System.Data.DbType.Int16 ||
p.DbType == System.Data.DbType.Int32 ||
p.DbType == System.Data.DbType.Int64 ||
p.DbType == System.Data.DbType.Single ||
p.DbType == System.Data.DbType.VarNumeric ||
p.DbType == System.Data.DbType.UInt16 ||
p.DbType == System.Data.DbType.UInt32 ||
p.DbType == System.Data.DbType.UInt64
) ? (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("{0}", p.Value) : (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("'{0}'", p.Value)));
return sql;
} public static string ToSqlString<T>(this IUpdateable<T> IUpdateable) where T : class, new()
{
var tosql = IUpdateable.ToSql();
Regex reg = new Regex(@"[^0-9]+");
var sql = tosql.Key;
var pars = tosql.Value;
if (pars == null)
{
return sql;
}
sql = pars.OrderByDescending(o => reg.Replace(o.ParameterName, "").ObjToInt()).ThenByDescending(o => o.ParameterName.Length).Aggregate(sql, (current, p) => current.Replace(p.ParameterName, (p.DbType == System.Data.DbType.Decimal ||
p.DbType == System.Data.DbType.Double ||
p.DbType == System.Data.DbType.Int16 ||
p.DbType == System.Data.DbType.Int32 ||
p.DbType == System.Data.DbType.Int64 ||
p.DbType == System.Data.DbType.Single ||
p.DbType == System.Data.DbType.VarNumeric ||
p.DbType == System.Data.DbType.UInt16 ||
p.DbType == System.Data.DbType.UInt32 ||
p.DbType == System.Data.DbType.UInt64
) ? (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("{0}", p.Value) : (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("'{0}'", p.Value)));
return sql;
} public static string ToSqlString<T>(this IDeleteable<T> IDeleteable) where T : class, new()
{
var tosql = IDeleteable.ToSql();
Regex reg = new Regex(@"[^0-9]+");
var sql = tosql.Key;
var pars = tosql.Value;
if (pars == null)
{
return sql;
}
sql = pars.OrderByDescending(o => reg.Replace(o.ParameterName, "").ObjToInt()).ThenByDescending(o => o.ParameterName.Length).Aggregate(sql, (current, p) => current.Replace(p.ParameterName, (p.DbType == System.Data.DbType.Decimal ||
p.DbType == System.Data.DbType.Double ||
p.DbType == System.Data.DbType.Int16 ||
p.DbType == System.Data.DbType.Int32 ||
p.DbType == System.Data.DbType.Int64 ||
p.DbType == System.Data.DbType.Single ||
p.DbType == System.Data.DbType.VarNumeric ||
p.DbType == System.Data.DbType.UInt16 ||
p.DbType == System.Data.DbType.UInt32 ||
p.DbType == System.Data.DbType.UInt64
) ? (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("{0}", p.Value) : (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("'{0}'", p.Value)));
return sql;
} public static string ToSqlString<T>(this IInsertable<T> IInsertable) where T : class, new()
{
var tosql = IInsertable.ToSql();
Regex reg = new Regex(@"[^0-9]+");
var sql = tosql.Key;
var pars = tosql.Value;
if (pars == null)
{
return sql;
}
sql = pars.OrderByDescending(o => reg.Replace(o.ParameterName, "").ObjToInt()).ThenByDescending(o => o.ParameterName.Length).Aggregate(sql, (current, p) => current.Replace(p.ParameterName, (p.DbType == System.Data.DbType.Decimal ||
p.DbType == System.Data.DbType.Double ||
p.DbType == System.Data.DbType.Int16 ||
p.DbType == System.Data.DbType.Int32 ||
p.DbType == System.Data.DbType.Int64 ||
p.DbType == System.Data.DbType.Single ||
p.DbType == System.Data.DbType.VarNumeric ||
p.DbType == System.Data.DbType.UInt16 ||
p.DbType == System.Data.DbType.UInt32 ||
p.DbType == System.Data.DbType.UInt64
) ? (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("{0}", p.Value) : (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("'{0}'", p.Value)));
return sql;
}

5、Demo地址:https://gitee.com/xuanyun2018/sqlsugardemo.git

sqlsugar入门(1)-初识sugar正确打开sugar的方式的更多相关文章

  1. 跨平台C++:(前言)正确打开C++的方式

    接触C++已经十五年了...但是对于C++而言,我至今是个门外汉,不是谦虚,而是确实不得其门而入. 历程是这样的—— 大学考研要考C++,就自学了.研没考上,C++算是学了,准确的说是C++的语法,以 ...

  2. iOS开发小技巧--相机相册的正确打开方式

    iOS相机相册的正确打开方式- UIImagePickerController 通过指定sourceType来实现打开相册还是相机 UIImagePickerControllerSourceTypeP ...

  3. Xcode 的正确打开方式——Debugging(转载)

    Xcode 的正确打开方式——Debugging   程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不可避免地需要使用 Xcode.这篇博客就主要介绍了 Xcode 中几种能 ...

  4. C#语法——泛型的多种应用 C#语法——await与async的正确打开方式 C#线程安全使用(五) C#语法——元组类型 好好耕耘 redis和memcached的区别

    C#语法——泛型的多种应用   本篇文章主要介绍泛型的应用. 泛型是.NET Framework 2.0 版类库就已经提供的语法,主要用于提高代码的可重用性.类型安全性和效率. 泛型的定义 下面定义了 ...

  5. InnoDB缓冲池预加载在MySQL 5.7中的正确打开方式

    InnoDB缓冲池预加载在MySQL 5.7中的正确打开方式 https://mp.weixin.qq.com/s/HGa_90XvC22anabiBF8AbQ 在这篇文章里,我将讨论在MySQL 5 ...

  6. Console控制台的正确打开方式

    Console控制台的正确打开方式 console对象提供了访问浏览器调试模式的信息到控制台 -- Console对象 |-- assert() 如果第一个参数断言为false,则在控制台输出错误信息 ...

  7. 任务队列和异步接口的正确打开方式(.NET Core版本)

    任务队列和异步接口的正确打开方式 什么是异步接口? Asynchronous Operations Certain types of operations might require processi ...

  8. mysql core文件的正确打开姿势

         最近两天自己负责的一个实例频繁出现crash的情况,分析了日志,大致明白了crash的原因,但是没有定位到具体的SQL,也没有找到很好的规避的办法,因此想在mysql出现crash的时候自动 ...

  9. 【Android开发笔记】返回上层Activity的正确打开方式

    技术支持 http://stackoverflow.com/questions/12276027/how-can-i-return-to-a-parent-activity-correctly 首先, ...

随机推荐

  1. day61:Linux:权限管理&rpm软件包管理&yum工具

    目录 1.权限管理 2.rpm软件包管理 3.yum工具(联网) 权限管理 1.什么是权限? 权限主要用来约束用户能对系统所做的操作 2.为什么要使用权限? 因为系统中不可能只存在一个root用户,一 ...

  2. html 背景花瓣特效--1

    html背景樱花可以用js添加,将<script>标签复制到<body>标签下就可以,javascript脚本点击 <!DOCTYPE html> <html ...

  3. 解决Use 'LimitInternalRecursion' to increase the limit if necessary的问题 CodeIgniter .htaccess

    配置.htaccess如下: RewriteEngine on RewriteBase / RewriteCond $1 !^(index\.php|images|robots\.txt|css|js ...

  4. 091 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 02 static关键字 01 static关键字(上)

    091 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 ...

  5. 【随笔】菜刀(代码执行)函数和命令执行函数详解及Getshell方法

    代码执行函数 VS 命令执行函数 一直想整理这两块的内容,但是一直没时间弄,直到前两天碰上一个写入了菜刀马但是死活连不上菜刀的站,顿时不知道怎么继续了,所以就趁这个机会整理了一下代码执行函数怎么get ...

  6. P4107 [HEOI2015]兔子与樱花 贪心

    题目描述 传送门 分析 一道贪心题 首先我们可以证明最优的贡献一定是从下依次合并到上的 不会出现一个节点不能合并到父亲节点,却能合并到父亲节点的祖先节点的情况 我们设当前的节点为 \(u\),\(u\ ...

  7. regsvr32 bypass windows defender 新思路

    原文链接:blog 在对regsvr32的用法进行了解之后,对于Casey Smith的远程js脚本执行命令的思路很感兴趣. 命令语法如下: regsvr32 /s /n /u /i:http://1 ...

  8. 【开源】Springboot API 一键生成器

    Springboot API 一键生成器 写这个项目,最大的想法就是:不做CRUD 程序猿 Springboot 在我们平时开发项目当中,是如此的常用.然而,比如平时我们写的一些: XX 管理系统 X ...

  9. Dubbo部分知识点总结

    Dubbo部分 Dubbo工作原理 dubbo工作原理第一层:service层,接口层,给服务提供者和消费者来实现的第二层:config层,配置层,主要是对dubbo进行各种配置的第三层:proxy层 ...

  10. Python的逻辑控制true/false和循环

    逻辑判断 简单的几个尝试,看下和java的一点不同之处 1 > 2 # False 1 < 2 <3 # True 42 != '42' # True 'Name' == 'name ...