geboshi_V1/LeatherProject/LeatherApp/Utils/DBUtils.cs
2024-03-07 14:03:22 +08:00

60 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LeatherApp.Utils
{
public class DBUtils
{
private static SqlSugarClient db;
//public static dbBackup()
//{
// //mysqldump -uroot-p123456 mydb > /data/mysqlDump/mydb.sql
// //mysqldump - uroot - p123456 mydb > / data / mysqlDump / mydb.sql
//}
public static SqlSugarClient getErpDBCon(SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer)
{
if (string.IsNullOrEmpty(Config.ErpDBConStr))
return null;
if (db != null) return db;
db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = Config.ErpDBConStr,
DbType = dbType,
IsAutoCloseConnection = true
},
db =>
{
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响
//获取原生SQL推荐 5.1.4.63 性能OK
//UtilMethods.GetNativeSql(sql,pars)
//获取无参数化SQL 对性能有影响特别大的SQL参数多的调试使用
//UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)
};
});
return db;
}
public static DataTable execSql(string sql, List<SugarParameter> parameters, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer)
{
//查询表的所有
var mydb = getErpDBCon(dbType);
if(mydb == null) return null;
if (!mydb.Ado.IsValidConnection())
mydb.Ado.Open();
return mydb.Ado.GetDataTable(sql, parameters);
}
}
}