|
|
|
|
using SqlSugar;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
|
|
|
|
|
namespace ZJ_BYD.Untils
|
|
|
|
|
{
|
|
|
|
|
public partial class DBHelper
|
|
|
|
|
{
|
|
|
|
|
public static SqlSugarScope sqlSugarDb = null;
|
|
|
|
|
public static string defaultConnStr = "";
|
|
|
|
|
|
|
|
|
|
public DBHelper()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void GetConnStr()
|
|
|
|
|
{
|
|
|
|
|
var connectionStr = "pgsqlconnectionstring";
|
|
|
|
|
if (Program.CurrentDbType == DbType.MySql)
|
|
|
|
|
{
|
|
|
|
|
connectionStr = "connectionstring";
|
|
|
|
|
}
|
|
|
|
|
defaultConnStr = ConfigurationManager.ConnectionStrings[connectionStr].ConnectionString;
|
|
|
|
|
sqlSugarDb = GetDb();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static SqlSugarScope GetConnStrByIp(string ip)
|
|
|
|
|
{
|
|
|
|
|
var connStr = $"PORT=5432;DATABASE=sc_byd;HOST={ip};PASSWORD=123456;USER ID=postgres;Pooling=True;Search Path=public";
|
|
|
|
|
if (Program.CurrentDbType == DbType.MySql)
|
|
|
|
|
{
|
|
|
|
|
connStr = $"server={ip};port=3306;user id=root;password=123456;database=sc_byd;Convert Zero Datetime=True;Allow Zero Datetime=True;Connection Timeout=10";
|
|
|
|
|
}
|
|
|
|
|
return GetDb(connStr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static SqlSugarScope GetDb(string connstr = "")
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//用单例模式
|
|
|
|
|
var sugarDb = new SqlSugarScope(new ConnectionConfig()
|
|
|
|
|
{
|
|
|
|
|
ConnectionString = string.IsNullOrWhiteSpace(connstr) ? defaultConnStr : connstr,//连接符字串
|
|
|
|
|
DbType = Program.CurrentDbType, //数据库类型
|
|
|
|
|
IsAutoCloseConnection = true, //不设成true要手动close
|
|
|
|
|
},
|
|
|
|
|
db =>
|
|
|
|
|
{
|
|
|
|
|
//(A)全局生效配置点
|
|
|
|
|
//调试SQL事件,可以删掉
|
|
|
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
|
|
{
|
|
|
|
|
//LogHelper.WriteLog(sql);//输出sql,查看执行sql
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
if (Program.CurrentDbType == DbType.PostgreSQL)
|
|
|
|
|
{
|
|
|
|
|
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
|
|
|
|
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
|
|
|
|
|
}
|
|
|
|
|
return sugarDb;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var msg = ex == null ? "连接数据库异常" : ex.Message;
|
|
|
|
|
LogHelper.WriteLog(msg);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|