48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
|
|
namespace RB_MES_API.Context
|
|
{
|
|
public class ConnectionString
|
|
{
|
|
private static readonly object objLock = new object();
|
|
private static ConnectionString instance = null;
|
|
IConfigurationRoot config = default;
|
|
|
|
public ConnectionString()
|
|
{
|
|
ConfigurationBuilder configuration = new ConfigurationBuilder();
|
|
//读取配置文件
|
|
config = configuration.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(file =>
|
|
{
|
|
file.Path = "/Config/appsettings.json";
|
|
file.Optional = false;
|
|
file.ReloadOnChange = true;
|
|
|
|
}).Build();
|
|
}
|
|
|
|
public static ConnectionString GetInstance()
|
|
{
|
|
if (instance == null)
|
|
{
|
|
lock (objLock)
|
|
{
|
|
if (instance == null)
|
|
{
|
|
instance = new ConnectionString();
|
|
}
|
|
}
|
|
}
|
|
|
|
return instance;
|
|
}
|
|
|
|
public static string GetConfig(string section, string key)
|
|
{
|
|
//实例化KDContet
|
|
|
|
//供ADO直接访问数据库的连接字符串
|
|
return (string)GetInstance().config.GetSection(section).GetValue(typeof(string), key);
|
|
}
|
|
}
|
|
}
|