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. SpringMVC学习(二)

    Ajax 简介 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分 ...

  2. .Net Core 2.2 存取Cookie

    第一步(注释代码):注释Startup.cs中 ConfigureServices 函数中的  options.CheckConsentNeeded = context => true; 第二步 ...

  3. 033 01 Android 零基础入门 01 Java基础语法 03 Java运算符 13 运算符和表达式知识点总结

    033 01 Android 零基础入门 01 Java基础语法 03 Java运算符 13 运算符和表达式知识点总结 本文知识点:运算符和表达式知识点总结 前面学习的几篇文都是运算符和表达式相关的知 ...

  4. 01 AS 首次编译执行项目过程中遇到的几个常见问题

    问题01 as打开时出现The environment variable JAVA_HOME (with The value of C:\Java\jdk1.8.0_101\bin) does not ...

  5. C++ 中explicit的作用

    转载:https://www.cnblogs.com/diligenceday/p/5781408.html C++ 中explicit的作用   explicit作用: 在C++中,explicit ...

  6. Linux系统编程—有名管道

    ▋****1. 管道的概念 管道,又名「无名管理」,或「匿名管道」,管道是一种非常基本,也是使用非常频繁的IPC方式. 1.1 管道本质 管道的本质也是一种文件,不过是伪文件,实际上是一块内核缓冲区, ...

  7. 这次一定让你记住 TCP 三次握手、四手挥手!

    TCP协议全称为:Transmission Control Protocol,是一种面向链接.保证数据传输安全.可靠的数据传输协议.为了确保数据的可靠传输,不仅需要对发出的每个字节进行编号确认,还需要 ...

  8. Brew error: Could not symlink, path is not writable

    As explained here by Rick: Start with brew doctor which will show you errors with your brew setup. Y ...

  9. linux的bootmem内存管理

    内核刚开始启动的时候如果一步到位写一个很完善的内存管理系统是相当麻烦的.所以linux先建立了一个非常简单的临时内存管理系统bootmem,有了这个bootmem就可以做简单的内存分配/释放操作,在b ...

  10. 多测师讲解html _链接标签004_高级讲师肖sir

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>链 ...