SQLServer连接配置工具(配置保存到ini文件或系统注册表)
SQLServer连接配置工具(配置保存到ini文件或系统注册表)
//存储SQL连接配置的接口
public interface IWriteSQLConfigValue
{
/// <summary>
/// 写入SQL的连接配置信息
/// </summary>
void Write();
/// <summary>
/// 读取SQL的连接配置信息
/// </summary>
void Read();
/// <summary>
/// SQL Server Name/IP
/// </summary>
string ServerName { get; set; }
/// <summary>
/// 连接的数据库
/// </summary>
string InitialCatalog { get; set; }
/// <summary>
/// 用户名
/// </summary>
string UserName { get; set; }
/// <summary>
/// 密码
/// </summary>
string Password { get; set; }
/// <summary>
/// 生成连接字符串
/// </summary>
/// <returns></returns>
string BuildConnectionString();
}
// 来源:www.CSFramework.com, C/S结构框架学习网
连接参数保存在INI文件
连接参数保存在注册表
//存储SQL连接配置的接口
public interface IWriteSQLConfigValue
{
/// <summary>
/// 写入SQL的连接配置信息
/// </summary>
void Write();
/// <summary>
/// 读取SQL的连接配置信息
/// </summary>
void Read();
/// <summary>
/// SQL Server Name/IP
/// </summary>
string ServerName { get; set; }
/// <summary>
/// 连接的数据库
/// </summary>
string InitialCatalog { get; set; }
/// <summary>
/// 用户名
/// </summary>
string UserName { get; set; }
/// <summary>
/// 密码
/// </summary>
string Password { get; set; }
/// <summary>
/// 生成连接字符串
/// </summary>
/// <returns></returns>
string BuildConnectionString();
}
// 来源:www.CSFramework.com, C/S结构框架学习网
连接参数保存在INI文件
//连接参数保存在INI文件
public class IniFileWriter : IWriteSQLConfigValue
{
#region IWriteConfigValue Members
private string _iniFile;
private string _ServerName;
private string _Password;
private string _UserName;
private string _InitialCatalog;
public IniFileWriter(string iniFile)
{
_iniFile = iniFile;
if (!System.IO.File.Exists(iniFile))
Write();
Read();
}
public string BuildConnectionString()
{
return new SqlConfiguration().GetConnectionString(this);
}
public void Read()
{
IniFile cfg = new IniFile(_iniFile);
if (cfg != null)
{
_ServerName = cfg.IniReadValue("SQL Configuration", "SqlServer");
_InitialCatalog = cfg.IniReadValue("SQL Configuration", "database");
_UserName = cfg.IniReadValue("SQL Configuration", "SqlUser");
_Password = CEncoder.Decode(cfg.IniReadValue("SQL Configuration", "SqlPwd"));//解密
}
}
public void Write()
{
IniFile cfg = new IniFile(_iniFile);
if (cfg != null)
{
cfg.IniWriteValue("SQL Configuration", "SqlServer", _ServerName);
cfg.IniWriteValue("SQL Configuration", "Database", _InitialCatalog);
cfg.IniWriteValue("SQL Configuration", "SqlUser", _UserName);
cfg.IniWriteValue("SQL Configuration", "SqlPwd", CEncoder.Encode(_Password)); //加密
}
}
//配置文件
public static string ConfigFile
{
get { return Application.StartupPath "\\csframework3.ini"; }
}
//加载连接配置
public static void LoadConfiguration()
{
if (System.IO.File.Exists(ConfigFile))
{
IWriteSQLConfigValue cfg = new IniFileWriter(ConfigFile);
SqlConfiguration.SetSQLConfig(cfg);
}
else
throw new Exception("Program cann't run without a SQL configuration.You should config the SQL connection by running CKG.TECH.Setup.exe!");
}
public string InitialCatalog { get { return _InitialCatalog; } set { _InitialCatalog = value; } }
public string ServerName { get { return _ServerName; } set { _ServerName = value; } }
public string UserName { get { return _UserName; } set { _UserName = value; } }
public string Password { get { return _Password; } set { _Password = value; } }
#endregion
}
// 来源:www.CSFramework.com, C/S结构框架学习网
public class IniFileWriter : IWriteSQLConfigValue
{
#region IWriteConfigValue Members
private string _iniFile;
private string _ServerName;
private string _Password;
private string _UserName;
private string _InitialCatalog;
public IniFileWriter(string iniFile)
{
_iniFile = iniFile;
if (!System.IO.File.Exists(iniFile))
Write();
Read();
}
public string BuildConnectionString()
{
return new SqlConfiguration().GetConnectionString(this);
}
public void Read()
{
IniFile cfg = new IniFile(_iniFile);
if (cfg != null)
{
_ServerName = cfg.IniReadValue("SQL Configuration", "SqlServer");
_InitialCatalog = cfg.IniReadValue("SQL Configuration", "database");
_UserName = cfg.IniReadValue("SQL Configuration", "SqlUser");
_Password = CEncoder.Decode(cfg.IniReadValue("SQL Configuration", "SqlPwd"));//解密
}
}
public void Write()
{
IniFile cfg = new IniFile(_iniFile);
if (cfg != null)
{
cfg.IniWriteValue("SQL Configuration", "SqlServer", _ServerName);
cfg.IniWriteValue("SQL Configuration", "Database", _InitialCatalog);
cfg.IniWriteValue("SQL Configuration", "SqlUser", _UserName);
cfg.IniWriteValue("SQL Configuration", "SqlPwd", CEncoder.Encode(_Password)); //加密
}
}
//配置文件
public static string ConfigFile
{
get { return Application.StartupPath "\\csframework3.ini"; }
}
//加载连接配置
public static void LoadConfiguration()
{
if (System.IO.File.Exists(ConfigFile))
{
IWriteSQLConfigValue cfg = new IniFileWriter(ConfigFile);
SqlConfiguration.SetSQLConfig(cfg);
}
else
throw new Exception("Program cann't run without a SQL configuration.You should config the SQL connection by running CKG.TECH.Setup.exe!");
}
public string InitialCatalog { get { return _InitialCatalog; } set { _InitialCatalog = value; } }
public string ServerName { get { return _ServerName; } set { _ServerName = value; } }
public string UserName { get { return _UserName; } set { _UserName = value; } }
public string Password { get { return _Password; } set { _Password = value; } }
#endregion
}
// 来源:www.CSFramework.com, C/S结构框架学习网
连接参数保存在注册表
//连接参数保存在注册表
public class RegisterWriter : IWriteSQLConfigValue
{
#region IWriteConfigValue Members
private string _keyPath;
private string _ServerName;
private string _Password;
private string _UserName;
private string _InitialCatalog;
//构造器
public RegisterWriter(string keyPath)
{
_keyPath = keyPath;
Read();
}
public string BuildConnectionString()
{
return new SqlConfiguration().GetConnectionString(this);
}
public void Read()
{
RegistryKey key = Registry.LocalMachine.CreateSubKey(_keyPath);
if (key != null)
{
_ServerName = ConvertEx.ToString(key.GetValue("SqlServer", "."));
_InitialCatalog = ConvertEx.ToString(key.GetValue("SqlDatabase", ""));
_UserName = ConvertEx.ToString(key.GetValue("SqlUser", "sa"));
_Password = ConvertEx.ToString(key.GetValue("SqlPwd", ""));
if (_Password != string.Empty) _Password = CEncoder.Decode(_Password);
key.Close();
}
}
public void Write()
{
RegistryKey key = Registry.LocalMachine.CreateSubKey(_keyPath);
if (key != null)
{
key.SetValue("SqlServer", _ServerName);
key.SetValue("SqlDatabase", _InitialCatalog);
key.SetValue("SqlUser", _UserName);
key.SetValue("SqlPwd", CEncoder.Encode(_Password));
key.Close();
}
}
public string InitialCatalog { get { return _InitialCatalog; } set { _InitialCatalog = value; } }
public string ServerName { get { return _ServerName; } set { _ServerName = value; } }
public string UserName { get { return _UserName; } set { _UserName = value; } }
public string Password { get { return _Password; } set { _Password = value; } }
#endregion
}
// 来源:www.CSFramework.com, C/S结构框架学习网
public class RegisterWriter : IWriteSQLConfigValue
{
#region IWriteConfigValue Members
private string _keyPath;
private string _ServerName;
private string _Password;
private string _UserName;
private string _InitialCatalog;
//构造器
public RegisterWriter(string keyPath)
{
_keyPath = keyPath;
Read();
}
public string BuildConnectionString()
{
return new SqlConfiguration().GetConnectionString(this);
}
public void Read()
{
RegistryKey key = Registry.LocalMachine.CreateSubKey(_keyPath);
if (key != null)
{
_ServerName = ConvertEx.ToString(key.GetValue("SqlServer", "."));
_InitialCatalog = ConvertEx.ToString(key.GetValue("SqlDatabase", ""));
_UserName = ConvertEx.ToString(key.GetValue("SqlUser", "sa"));
_Password = ConvertEx.ToString(key.GetValue("SqlPwd", ""));
if (_Password != string.Empty) _Password = CEncoder.Decode(_Password);
key.Close();
}
}
public void Write()
{
RegistryKey key = Registry.LocalMachine.CreateSubKey(_keyPath);
if (key != null)
{
key.SetValue("SqlServer", _ServerName);
key.SetValue("SqlDatabase", _InitialCatalog);
key.SetValue("SqlUser", _UserName);
key.SetValue("SqlPwd", CEncoder.Encode(_Password));
key.Close();
}
}
public string InitialCatalog { get { return _InitialCatalog; } set { _InitialCatalog = value; } }
public string ServerName { get { return _ServerName; } set { _ServerName = value; } }
public string UserName { get { return _UserName; } set { _UserName = value; } }
public string Password { get { return _Password; } set { _Password = value; } }
#endregion
}
// 来源:www.CSFramework.com, C/S结构框架学习网
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网