Files
lserp_cs_5.0/插件库/CommonLib/utils/DBConfig.cs
T
2026-07-10 15:25:05 +08:00

393 lines
13 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 System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using Microsoft.Win32;
using System.IO;
using Lskj.PubUtils;
namespace CommonLib
{
/// <summary>
/// 数据库连接参数配置类
/// </summary>
public sealed class DBConfig
{
private static DBConfig _instance = null;
private DBConfig()
{
}
public static DBConfig Instance
{
get
{
if (_instance == null)
{
_instance = new DBConfig();
_instance.ReadConfig();
_instance.WriteConfig();
}
return _instance;
}
}
/// <summary>
/// 数据库名
/// </summary>
public string DataBook = "";
/// <summary>
/// 数据库名
/// </summary>
public string datastr = "";
/// <summary>
/// 服务器名
/// </summary>
public string ServerName = "";
/// <summary>
/// 登录名
/// </summary>
public string LoginName = "";
public string LocationServer = "";
// 是否为debug模式
public string Debug;
public string ProgramPath;
/// <summary>
/// 文件升级标志
/// </summary>
private string _UpdateSet = "0";
public bool UpdateSet
{
get { return _UpdateSet == "1"; }
set { _UpdateSet = value ? "1" : "0"; }
}
///<summary>
///智能客户端启动
///</summary>
private string _SmallClient = "0";
public bool SmallClient
{
get { return _SmallClient == "1"; }
set { _SmallClient = value ? "1" : "0"; }
}
/// <summary>
/// 内外外网标志
/// </summary>
private int _Internet = 0;
public bool Internet
{
get { return _Internet == 1; }
set { _Internet = value ? 1 : 0; }
}
/// <summary>
/// 内部系统/外部系统切换
/// </summary>
private string IsConsumer = "0";
//服务器模式连接字符串
public string Connection;
//服务器模式连接字符串(dephi
public string dephiConnection;
//程序连接字符串
public string ProgramConnectionSql = "Server={0};Database={1};Persist Security Info=True;User ID=lserpAdmin;Password=lserp110;MultipleActiveResultSets=true";
//delphi连接字符串
public string DelphiConnectionSql = "Provider=SQLOLEDB.1;Server={0};Database={1};Persist Security Info=True;User ID=lserpAdmin;Password=lserp110";
/// <summary>
/// 读配置
/// </summary>
public void ReadConfig(bool isRefConfigFlag = false)
{
RegistryKey regKey = Registry.CurrentUser;
try
{
RegistryKey erpKey = regKey.CreateSubKey("AA_LS_Erp V2.0");
erpKey.CreateSubKey("File");
}
catch (Exception)
{
}
RegistryKey subKey = regKey.OpenSubKey("AA_LS_Erp V2.0");
subKey = subKey.OpenSubKey("File");
datastr = subKey.GetValue("datastr", "").ToString();
ServerName = subKey.GetValue("ServerName", "").ToString();
LoginName = subKey.GetValue("LoginName", "").ToString();
LocationServer = subKey.GetValue("LocationServer", "").ToString();
_UpdateSet = subKey.GetValue("UpdateSet", "1").ToString();
IsConsumer = subKey.GetValue("IsConsumer", "1").ToString();
_SmallClient = subKey.GetValue("SmallClient", "0").ToString();
try
{
_Internet = (int)subKey.GetValue("Internet", 0);
}
catch (Exception)
{
}
try
{
Debug = subKey.GetValue("Debug", "0") + "";
}
catch (Exception)
{
}
try
{
ProgramPath = subKey.GetValue("ProgramPath", "").ToString();
}
catch (Exception)
{
}
try
{
DataBook = subKey.GetValue("DataBook", "") + "";
}
catch (Exception)
{
}
//subKey.Close();
string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemResources.ini");
if (File.Exists(file))
{
string lastIPPort = string.Format("{0}:{1}", INIHelper.Read("SystemResources.ini", "LocalLastIP"), INIHelper.Read("SystemResources.ini", "LocalLastPort"));
string downloadAESStrKey = string.Format("DownloadAESStr_{0}", lastIPPort);
string[] downloadAESStrValue = INIHelper.Read("SystemResources.ini", downloadAESStrKey).Split('^');
string downloadAESStr = downloadAESStrValue.Length == 2 ? downloadAESStrValue[1] : "";
string configFlag = INIHelper.Read("SystemResources.ini", "FrmConfigFlag");
if (configFlag.Equals("1") && !isRefConfigFlag)
{
//string args = AESUtil.Decrypt(File.ReadAllText(file));
if (!string.IsNullOrEmpty(downloadAESStr))
{
string args = AESUtil.Decrypt(downloadAESStr);
string[] controlAfgs = args.Split('^');
if (controlAfgs != null && controlAfgs.Length == 5)
{
ServerName = controlAfgs[0];
datastr = controlAfgs[1];
string str = "Server={0};Database={1};Persist Security Info=True;User ID=" + controlAfgs[2] + ";Password=" + controlAfgs[3] + ";Connection Timeout=5;MultipleActiveResultSets=true";
string dephistr = "Provider=SQLOLEDB.1;Server={0};Database={1};Persist Security Info=True;User ID=" + controlAfgs[2] + ";Password=" + controlAfgs[3] + ";Connection Timeout=5";
Connection = AESUtil.Encrypt(str);
dephiConnection = AESUtil.Encrypt(dephistr);
}
}
}
}
}
/// <summary>
/// 写配置
/// </summary>
public void WriteConfig()
{
RegistryKey regKey = Registry.CurrentUser;
RegistryKey subKey = regKey.OpenSubKey("AA_LS_Erp V2.0");
subKey = subKey.OpenSubKey("File", true);
subKey.SetValue("datastr", datastr);
subKey.SetValue("ServerName", ServerName);
subKey.SetValue("LoginName", LoginName);
subKey.SetValue("UpdateSet", _UpdateSet);
//subKey.SetValue("Internet", _Internet);
subKey.SetValue("IsConsumer", IsConsumer);
subKey.SetValue("LocationServer", LocationServer);
subKey.SetValue("SmallClient", _SmallClient);
subKey.Close();
}
/// <summary>
/// 写配置
/// </summary>
public void WriteConfig2()
{
RegistryKey regKey = Registry.CurrentUser;
RegistryKey subKey = regKey.OpenSubKey("AA_LS_Erp V2.0");
subKey = subKey.OpenSubKey("File", true);
//subKey.SetValue("datastr", datastr);
//subKey.SetValue("ServerName", ServerName);
subKey.SetValue("LoginName", LoginName);
subKey.SetValue("UpdateSet", _UpdateSet);
//subKey.SetValue("Internet", _Internet);
subKey.SetValue("IsConsumer", IsConsumer);
subKey.SetValue("LocationServer", LocationServer);
subKey.SetValue("SmallClient", _SmallClient);
subKey.Close();
}
public void WriteConfig(string Key, string Value)
{
RegistryKey regKey = Registry.CurrentUser;
RegistryKey subKey = regKey.OpenSubKey("AA_LS_Erp V2.0");
subKey = subKey.OpenSubKey("File", true);
subKey.SetValue(Key, Value);
subKey.Close();
}
/// <summary>
/// 获取升级标志(P_updateFileTab表有数据强制升级,否则看标志UpdateSet)
/// </summary>
/// <returns></returns>
public bool GetUpdateFlag()
{
//没有升级共享目录
if (ERPInfo.Instance.UpdateIP == string.Empty)
{
return false;
}
try
{
using (SqlDataReader dr = SqlHelper.ExecuteReader(CommandType.Text, "select top 1 1 from P_updateFileTab with(nolock)", null))
{
if (dr.Read())
{
return true;
}
}
}
catch (Exception ex)
{
PubUtil.WriteError("获取升级日志标记出错", ex.Message);
}
return UpdateSet;
}
/// <summary>
/// C#连接字符串
/// </summary>
/// <returns></returns>
public string GetConStr(string Connection = "")
{
string connectionStr = string.Format(ProgramConnectionSql, ServerName, datastr);
if (!string.IsNullOrWhiteSpace(Connection))
{
return string.Format(AESUtil.Decrypt(Connection), ServerName, datastr);
}
//if (IsConsumer == "0")
// connectionStr = string.Format("Server=114.116.145.208,14331;Database=lscrm;Persist Security Info=True;User ID=Lserp_crm;Password=lserp_ERP_@#$_123", ServerName, datastr);
//string strSql = string.Format("Provider=SQLOLEDB.1;Password=lserp110;Persist Security Info=True;User ID=lserpAdmin;Initial Catalog=lsErp;Data Source=JINJIN;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=JINJIN;Use Encryption for Data=False;Tag with column collation when possible=False");
return connectionStr;
}
/// <summary>
/// delphi连接字符串
/// </summary>
/// <returns></returns>
public string GetDelphiConStr(string Connection = "")
{
string connectionStr = string.Format(DelphiConnectionSql, ServerName, datastr);
if (IsConsumer == "0")
connectionStr = string.Format("Provider=SQLOLEDB.1;Server=114.116.145.208,14331;Database=lscrm;Persist Security Info=True;User ID=Lserp_crm;Password=lserp_ERP_@#$_123", ServerName, datastr);
if (!string.IsNullOrEmpty(Connection))
{
return string.Format(AESUtil.Decrypt(Connection), ServerName, datastr);
}
return connectionStr;
}
/// <summary>
/// 创建连接
/// </summary>
/// <returns></returns>
public bool CreateConnection(string Connection = "")
{
string connectionStr = GetConStr(Connection);
if (SqlHelper.conn != null)
{
try
{
SqlHelper.conn.Close();
SqlHelper.conn.Dispose();
}
catch (Exception ex)
{
}
SqlHelper.conn = null;
}
try
{
SqlHelper.conn = new System.Data.SqlClient.SqlConnection(connectionStr);
SqlHelper.conn.Open();
return true;
}
catch (Exception ex)
{
PubUtil.WriteLog(ex.Message);
}
return false;
}
/// <summary>
/// 创建新的连接
/// </summary>
/// <returns></returns>
public SqlConnection CreateNewConnection()
{
string connectionStr = GetConStr();
SqlConnection conn = new SqlConnection(connectionStr);
try
{
conn.Open();
return conn;
}
catch (Exception ex)
{
PubUtil.WriteLog(ex.Message);
}
return conn;
}
/// <summary>
/// 创建新的连接
/// </summary>
/// <returns></returns>
public bool CreateNewConnection(string connectionStr)
{
SqlConnection conn = new SqlConnection(connectionStr);
if (SqlHelper.conn != null)
{
try
{
SqlHelper.conn.Close();
SqlHelper.conn.Dispose();
}
catch (Exception ex)
{
}
SqlHelper.conn = null;
}
try
{
SqlHelper.conn = new System.Data.SqlClient.SqlConnection(connectionStr);
SqlHelper.conn.Open();
return true;
}
catch (Exception ex)
{
PubUtil.WriteLog(ex.Message);
}
return false;
}
}
}