ab56a9bcf7
SVN-Revision: r240
214 lines
7.3 KiB
C#
214 lines
7.3 KiB
C#
/******************************
|
|
* 说明:数据库连接参数配置类
|
|
* 创建人:龚宇超
|
|
* 创建日期: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
|
|
{
|
|
/// <summary>
|
|
/// 数据库连接参数配置类
|
|
/// </summary>
|
|
public sealed class DBConfig
|
|
{
|
|
private static string _regKey = "AA_LS_Erp V2.0";
|
|
private static DBConfig _instance = null;
|
|
/// <summary>
|
|
/// DBConfig静态单例对象
|
|
/// </summary>
|
|
/// <value>The instance.</value>
|
|
public static DBConfig Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = new DBConfig();
|
|
_instance.ReadConfig();
|
|
|
|
_instance.WriteConfig();
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据库名
|
|
/// </summary>
|
|
public string DataBase;
|
|
/// <summary>
|
|
/// 帐套
|
|
/// </summary>
|
|
public string DataBook;
|
|
/// <summary>
|
|
/// 服务器名
|
|
/// </summary>
|
|
public string ServerName;
|
|
/// <summary>
|
|
/// 登录名
|
|
/// </summary>
|
|
public string LoginName;
|
|
/// <summary>
|
|
/// 文件升级标志
|
|
/// </summary>
|
|
public bool UpdateSet;
|
|
///<summary>
|
|
///智能客户端启动
|
|
///</summary>
|
|
public bool SmallClient;
|
|
/// <summary>
|
|
/// 内外外网标志
|
|
/// </summary>
|
|
public bool Internet;
|
|
|
|
/// <summary>
|
|
/// <para>说明:读取注册表配置</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-07 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
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)
|
|
{
|
|
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:写配置</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-07 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
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();
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:获取C#连接字符串</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-07 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns>System.String.</returns>
|
|
public string GetConnection()
|
|
{
|
|
return string.Format("Server={0};Database={1};Persist Security Info=True;User ID=lserpAdmin;Password=lserp110;Connection Timeout=5", ServerName, DataBase);
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:获取delphi连接字符串</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-07 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns>System.String.</returns>
|
|
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);
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:获取</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-03-28 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns>System.String.</returns>
|
|
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";
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:创建数据库连接</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-07 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|