138 lines
6.2 KiB
C#
138 lines
6.2 KiB
C#
using NetCore.Common;
|
||
using NetCore.Model.enums;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
|
||
namespace NetCore.BLL
|
||
{
|
||
public class Connection
|
||
{
|
||
public DatabaseType databaseType { get; set; }
|
||
public string connectionString { get; set; }
|
||
}
|
||
public class ConnectionFactory
|
||
{
|
||
/// <summary>
|
||
/// 转换数据库类型
|
||
/// </summary>
|
||
/// <param name="databaseType">数据库类型</param>
|
||
/// <returns></returns>
|
||
private static DatabaseType GetDataBaseType(string databaseType)
|
||
{
|
||
DatabaseType returnValue = DatabaseType.SqlServer;
|
||
foreach (DatabaseType dbType in Enum.GetValues(typeof(DatabaseType)))
|
||
{
|
||
if (dbType.ToString().Equals(databaseType, StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
returnValue = dbType;
|
||
break;
|
||
}
|
||
}
|
||
return returnValue;
|
||
}
|
||
//private static string _type { get; set; }
|
||
//private static string _connstr { get; set; }
|
||
protected static Dictionary<ContextType, Connection> _mydic { get; set; }
|
||
|
||
protected static Connection qwConnect = null;//数据源
|
||
protected static Connection crmConnect = null;//数据源
|
||
protected static Connection wxConnect = null;//数据源
|
||
protected static Connection qwConnect_targt = null;//目标数据库
|
||
protected static Connection crmConnect_targt = null;//目标数据库
|
||
protected static Connection wxConnect_targt = null;//目标数据库
|
||
private static Connection GetConnStr(ContextType contextType, string comcode, bool istargt)
|
||
{
|
||
switch (contextType)
|
||
{
|
||
case ContextType.crmContext:
|
||
if (crmConnect == null || string.IsNullOrEmpty(crmConnect.connectionString))
|
||
{
|
||
crmConnect = GetRealtimeStr(contextType, comcode, false);
|
||
}
|
||
if (crmConnect_targt == null || string.IsNullOrEmpty(crmConnect_targt.connectionString))
|
||
{
|
||
crmConnect_targt = GetRealtimeStr(contextType, comcode, true);
|
||
}
|
||
return istargt ? crmConnect_targt : crmConnect;
|
||
|
||
case ContextType.qwContext:
|
||
if (qwConnect == null || string.IsNullOrEmpty(qwConnect.connectionString))
|
||
{
|
||
qwConnect = GetRealtimeStr(contextType, comcode, false);
|
||
}
|
||
if (qwConnect_targt == null || string.IsNullOrEmpty(qwConnect_targt.connectionString))
|
||
{
|
||
qwConnect_targt = GetRealtimeStr(contextType, comcode, true);
|
||
}
|
||
return istargt ? qwConnect_targt : qwConnect;
|
||
case ContextType.wxContext:
|
||
if (wxConnect == null || string.IsNullOrEmpty(wxConnect.connectionString))
|
||
{
|
||
wxConnect = GetRealtimeStr(contextType, comcode, false);
|
||
}
|
||
if (wxConnect_targt == null || string.IsNullOrEmpty(wxConnect_targt.connectionString))
|
||
{
|
||
wxConnect_targt = GetRealtimeStr(contextType, comcode, true);
|
||
}
|
||
return istargt ? wxConnect_targt : wxConnect;
|
||
default:
|
||
return qwConnect;
|
||
}
|
||
}
|
||
private static Connection GetRealtimeStr(ContextType contextType, string comcode, bool istargt)
|
||
{
|
||
return new Connection()
|
||
{
|
||
connectionString = ConfigHelper.GetSectionValue($"{comcode}_Config:{contextType.ToString()}:{(istargt ? "targtConectStr" : "connectionString")}"),//从配置中获取配置
|
||
databaseType = GetDataBaseType(ConfigHelper.GetSectionValue($"{comcode}_Config:{contextType.ToString()}:databaseType")),//从配置中获取配置
|
||
};
|
||
}
|
||
/// <summary>
|
||
/// 获取数据库连接
|
||
/// </summary>
|
||
/// <param name="datatype">SqlServer,MySql,Oracle 如果不传递指定数据库,则按照配置的来</param>
|
||
/// <param name="comcode">事业部的编码</param>
|
||
/// <param name="istargt">true:目标库 flase:数据源库</param>
|
||
/// <returns></returns>
|
||
public static IDbConnection CreateConnection(ContextType contextType, string comcode, bool istargt = true)
|
||
{
|
||
IDbConnection connection = null;
|
||
Connection database = GetConnStr(contextType, comcode, istargt);
|
||
switch (database.databaseType)
|
||
{
|
||
case DatabaseType.SqlServer:
|
||
connection = new System.Data.SqlClient.SqlConnection(database.connectionString);
|
||
break;
|
||
case DatabaseType.MySql:
|
||
connection = new MySql.Data.MySqlClient.MySqlConnection(database.connectionString);
|
||
break;
|
||
case DatabaseType.Oracle:
|
||
connection = new Oracle.ManagedDataAccess.Client.OracleConnection(database.connectionString);
|
||
break;
|
||
}
|
||
return connection;
|
||
}
|
||
|
||
public static IDbConnection CreatePushConnection()
|
||
{
|
||
IDbConnection connection = null;
|
||
string connectionstr = ConfigHelper.GetSectionValue($"PushConfig:connectionString");
|
||
string contype = ConfigHelper.GetSectionValue($"PushConfig:databaseType");
|
||
DatabaseType databaseType = GetDataBaseType(contype);
|
||
switch (databaseType)
|
||
{
|
||
case DatabaseType.SqlServer:
|
||
connection = new System.Data.SqlClient.SqlConnection(connectionstr);
|
||
break;
|
||
case DatabaseType.MySql:
|
||
connection = new MySql.Data.MySqlClient.MySqlConnection(connectionstr);
|
||
break;
|
||
case DatabaseType.Oracle:
|
||
connection = new Oracle.ManagedDataAccess.Client.OracleConnection(connectionstr);
|
||
break;
|
||
}
|
||
return connection;
|
||
}
|
||
}
|
||
} |