/****************************** * 说明:数据库连接参数配置类 * 创建人:龚宇超 * 创建日期:2017-07-24 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Win32; using System.Data.SqlClient; using System.Data; using System.Net; using System.Net.Sockets; using System.Threading; using System.Diagnostics; namespace Lskj.Core { /// /// 数据库连接参数配置类 /// public sealed class DBConfig { private static string _regKey = "AA_LS_Erp V2.0"; private static DBConfig _instance = null; /// /// DBConfig静态单例对象 /// /// The instance. public static DBConfig Instance { get { if (_instance == null) { _instance = new DBConfig(); _instance.ReadConfig(); _instance.WriteConfig(); } return _instance; } } /// /// 数据库名 /// public string DataBase; /// /// 帐套 /// public string DataBook; /// /// 服务器名 /// public string ServerName; /// /// 登录名 /// public string LoginName; /// /// 文件升级标志 /// public bool UpdateSet; /// ///智能客户端启动 /// public bool SmallClient; /// /// 内外外网标志 /// public bool Internet; /// /// 说明:读取注册表配置 /// 创建人:龚宇超 /// 创建日期:2017-08-07 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// public void ReadConfig() { RegistryKey regKey = Registry.CurrentUser; RegistryKey subKey = regKey.OpenSubKey(_regKey); subKey = subKey.OpenSubKey("File"); DataBase = subKey.GetValue("datastr", "") + ""; ServerName = subKey.GetValue("ServerName", "") + ""; LoginName = subKey.GetValue("LoginName", "") + ""; UpdateSet = subKey.GetValue("UpdateSet", "0") + "" == "1" || subKey.GetValue("UpdateSet", "0") + "" == "True" ? true : false; SmallClient = subKey.GetValue("SmallClient", "0") + "" == "1" || subKey.GetValue("SmallClient", "0") + "" == "True" ? true : false; Internet = subKey.GetValue("Internet", "1") + "" == "0" ? true : false; try { DataBook = subKey.GetValue("DataBook", "") + ""; } catch (Exception) { } } /// /// 说明:写配置 /// 创建人:龚宇超 /// 创建日期:2017-08-07 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// public void WriteConfig() { RegistryKey regKey = Registry.CurrentUser; RegistryKey subKey = regKey.OpenSubKey(_regKey); subKey = subKey.OpenSubKey("File", true); subKey.SetValue("datastr", DataBase); subKey.SetValue("ServerName", ServerName); subKey.SetValue("LoginName", LoginName); subKey.SetValue("UpdateSet", UpdateSet); subKey.SetValue("SmallClient", SmallClient); subKey.SetValue("DataBook", DataBook); subKey.Close(); } /// /// 说明:获取C#连接字符串 /// 创建人:龚宇超 /// 创建日期:2017-08-07 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// System.String. public string GetConnection() { return string.Format("Server={0};Database={1};Persist Security Info=True;User ID=lserpAdmin;Password=lserp110;Connection Timeout=5", ServerName, DataBase); } /// /// 说明:获取delphi连接字符串 /// 创建人:龚宇超 /// 创建日期:2017-08-07 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// System.String. public string GetDelphiConnection() { return string.Format("Provider=SQLOLEDB.1;Server={0};Database={1};Persist Security Info=True;User ID=lserpAdmin;Password=lserp110;Connection Timeout=5", ServerName, DataBase); } /// /// 说明:获取 /// 创建人:龚宇超 /// 创建日期:2018-03-28 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// System.String. public string GetLsCrmConnection() { return Internet ? "Server=192.168.0.10,14330;Database=lscrm;Persist Security Info=True;User ID=Lserp_crm;Password=lserp_ERP_@#$_123;Connection Timeout=5" : "Server=lsucrm.ticp.net,14330;Database=lscrm;Persist Security Info=True;User ID=Lserp_crm;Password=lserp_ERP_@#$_123;Connection Timeout=5"; } /// /// 说明:创建数据库连接 /// 创建人:龚宇超 /// 创建日期:2017-08-07 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// true if XXXX, false otherwise. public bool CreateConnection() { string connStr = GetConnection(); if (SqlHelper._connection != null) { try { SqlHelper._connection.Close(); SqlHelper._connection.Dispose(); } catch (Exception) { } SqlHelper._connection = null; } try { SqlHelper._connection = new SqlConnection(connStr); SqlHelper._connection.Open(); return true; } catch (Exception ex) { } return false; } } }