private void BtnConn_Click(object sender, EventArgs e)
{
if (CheckDBCon())
{
ConnHelper.IP = TxtIP.Text.Trim();
ConnHelper.DBName = TxtDBName.Text.Trim();
ConnHelper.UserID = TxtUserID.Text.Trim();
ConnHelper.UserPwd = TxtPWD.Text.Trim();
//判断是否能够连接数据
if (ConnHelper.CheckCon())
{
MessageBox.Show("数据库连接成功!");
toolStripStatusLabel1.Text = "数据库已连接!";
//判断是否需要保持连接信息
if (chBConn.Checked == true)
{
string AppPath = Application.StartupPath + @"\";
string IniFile = "kernel32.ini";
string iniPath = AppPath + IniFile; //获取路径
INIClass GetPath = new INIClass(iniPath); //创建实例化对象
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string ServerIP = "ServerIP";
string ServerIPKey = "ServerIPKey";
string ServerIPKeyValue = Encode(TxtIP.Text);
GetPath.IniWriteValue(ServerIP, ServerIPKey, ServerIPKeyValue); //写入ServerIP的值
string DBName = "DBName";
string DBNameKey = "DBNameKey";
string DBNameKeyValue = Encode(TxtDBName.Text);
GetPath.IniWriteValue(DBName, DBNameKey, DBNameKeyValue); //写入DBName的值
string UserID = "UserID";
string UserIDKey = "UserIDKey";
string UserIDKeyValue = Encode(TxtUserID.Text);
GetPath.IniWriteValue(UserID, UserIDKey, UserIDKeyValue); //写入UserID的值
string PWD = "PWD";
string PWDKey = "PWDKey";
string PWDKeyValue = Encode(TxtPWD.Text);
GetPath.IniWriteValue(PWD, PWDKey, PWDKeyValue); //写入PWD的值
}
else
{
string AppPath = Application.StartupPath + @"\";
string IniFile = "kernel32.ini";
string iniPath = AppPath + IniFile; //获取路径
INIClass GetPath = new INIClass(iniPath); //创建实例化对象
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string ServerIP = "ServerIP";
string ServerIPKey = "ServerIPKey";
string ServerIPKeyValue = Encode("");
GetPath.IniWriteValue(ServerIP, ServerIPKey, ServerIPKeyValue); //写入ServerIP的值
string DBName = "DBName";
string DBNameKey = "DBNameKey";
string DBNameKeyValue = Encode("");
GetPath.IniWriteValue(DBName, DBNameKey, DBNameKeyValue); //写入DBName的值
string UserID = "UserID";
string UserIDKey = "UserIDKey";
string UserIDKeyValue = Encode("");
GetPath.IniWriteValue(UserID, UserIDKey, UserIDKeyValue); //写入UserID的值
string PWD = "PWD";
string PWDKey = "PWDKey";
string PWDKeyValue = Encode("");
GetPath.IniWriteValue(PWD, PWDKey, PWDKeyValue); //写入PWD的值
}
}
else
{
MessageBox.Show("数据库连接失败!请检查连接信息是否正确!");
}
}
}
//加密算法的方法
const string KEY_64 = "VavicApp";//注意了,是8个字符,64位
const string IV_64 = "VavicApp";
public string Encode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
}
//解密算法的方法
public string Decode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
private bool CheckDBCon()
{
if (TxtIP.Text.Trim() == "")
{
MessageBox.Show("IP地址不能为空,请从新输入!");
TxtIP.Focus();
return false;
}
if (TxtDBName.Text.Trim() == "")
{
MessageBox.Show("连接数据库名不能为空,请从新输入!");
TxtDBName.Focus();
return false;
}
if (TxtUserID.Text.Trim() == "")
{
MessageBox.Show("连接数据用户名不能为空,请从新输入!");
TxtUserID.Focus();
return false;
}
if (TxtPWD.Text.Trim() == "")
{
MessageBox.Show("连接数据库密码不能为空,请从新输入!");
TxtPWD.Focus();
return false;
}
return true;
}
19
2012
04
C# 进行登录数据库连接并保持连接信息
发布:郑德才博客 | 分类:学习之路 | 评论:0 | 浏览:
相关文章:
C# 进行数据库连接并将获得的表绑定到DataGridView中 (2012-4-19 7:51:34)
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。