//界面打开显示时判断是否连接到数据库
procedure TFRMMAIN.FormShow(Sender: TObject);
var
MyReg : TRegistry;
constr, FWQMC, SJKMC, YHMC, YHMM: string;
begin
EDTSERVER.Enabled:=false;//服务器名称
EDTDATA.Enabled:=false;//数据库名称
EDTUSER.Enabled:=false;//SQL用户
EDTPSW.Enabled:=false;//密码
MyReg:=TRegistry.Create;
MyReg.RootKey:=HKEY_LOCAL_MACHINE;
if MyReg.OpenKey('\SOFTWARE\SW\SWDB', false) then
begin
if MyReg.ValueExists('connectstr') then
begin
constr:=MyReg.ReadString('ConnectStr');
FWQMC:=MyReg.ReadString('Server');
SJKMC:=MyReg.ReadString('DataBase');
YHMC:=MyReg.ReadString('User');
YHMM:=MyReg.ReadString('PassWord');
EDTSERVER.Text:=DecryStrHex(FWQMC,'※★');//得到注册表中的服务器名
EDTDATA.text:=DECRYSTRHEX(SJKMC,'※★');//得到注册表中的数据库名
EDTUSER.text:=DECRYSTRHEX(YHMC,'※★');//得到注册表中的sql用吗名
EDTPSW.text:=DECRYSTRHEX(YHMM,'※★');//得到注册表中的密码
end;
end;
AConnt.Close;
constr := 'Provider=SQLOLEDB;Password=' + trim(EDTPSW.Text) + ';Persist Security Info=True;User ID=' + trim(EDTUSER.Text) +
';Initial Catalog=' + trim(EDTDATA.Text) + ';Data Source=' + trim(EDTSERVER.Text);
AConnt.ConnectionString := constr;
try
AConnt.Open;
//Application.MessageBox('数据库连接成功!', '提示', 0);
except
Application.MessageBox('数据库连接失败!', '提示', mb_iconerror + mb_ok);
end;
AConnt.Close;
end;
//-----------------------------------------------------------------------------------------------------------------------
//------数据库连接测试---
procedure TFRMMAIN.BTNTESTClick(Sender: TObject);
var
ss,s1,s2,s3,s4: string;
begin
s1:=Trim(EDTSERVER.text);
s2:=Trim(EDTDATA.text);
s3:=Trim(EDTUSER.text);
s4:=Trim(EDTPSW.text);
ss := 'Provider=SQLOLEDB;Password=' + s4 + ';Persist Security Info=True;User ID=' + s3 +
';Initial Catalog=' + s2 + ';Data Source=' + s1;
try
AConnt.Close;
AConnt.ConnectionString := ss;
AConnt.Open;
Application.MessageBox('数据库连接成功!', '提示', 0);
except
Application.MessageBox('数据库连接失败,请重新配置!', '提示', mb_iconerror + mb_ok);
exit;
end;
end;
//--------------------------------------------------------------------------------------------------------------------
//--------修改连接的配置-----
procedure TFRMMAIN.BTNBCClick(Sender: TObject);
var
MyReg: TRegistry;
sw,ssbl,ssba,sshs,FWQMC,SJKMC,YHMC,YHMM: string;
begin
sw := 'Provider=SQLOLEDB;Password=' + trim(EDTPSW.Text) + ';Persist Security Info=True;User ID=' + trim(EDTUSER.Text) +
';Initial Catalog=' + trim(EDTDATA.Text) + ';Data Source=' + trim(EDTSERVER.Text);
sw := EncryStrHex(sw, '※★');
FWQMC := EncryStrHex(TRIM(EDTSERVER.Text), '※★');
SJKMC := EncryStrHex(TRIM(EDTDATA.Text), '※★');
YHMC := EncryStrHex(TRIM(EDTUSER.Text), '※★');
YHMM := EncryStrHex(TRIM(EDTPSW.Text), '※★');
MyReg := TRegistry.Create;
MyReg.RootKey := HKEY_LOCAL_MACHINE;
if MyReg.OpenKey('\SOFTWARE\SW\SWDB', true) then
begin
MyReg.WriteString('ConnectStr', sw);
MyReg.WriteString('SERVER', FWQMC);
MyReg.WriteString('DATABASE', SJKMC);
MyReg.WriteString('USER', YHMC);
MyReg.WriteString('PASSWORD', YHMM);
end;
MyReg.Free;
Application.MessageBox('数据库设置已保存!-', '提示', 0);
EDTSERVER.Enabled:=false;
EDTDATA.Enabled:=false;
EDTUSER.Enabled:=false;
EDTPSW.Enabled:=false;
end;
//-----------------------------------------------------------------------------------------------------
//----------------保存数据库到注册表------------
procedure TFRMMAIN.BTNBCClick(Sender: TObject);
var
MyReg: TRegistry;
sw,ssbl,ssba,sshs,FWQMC,SJKMC,YHMC,YHMM: string;
begin
sw := 'Provider=SQLOLEDB;Password=' + trim(EDTPSW.Text) + ';Persist Security Info=True;User ID=' + trim(EDTUSER.Text) +
';Initial Catalog=' + trim(EDTDATA.Text) + ';Data Source=' + trim(EDTSERVER.Text);
sw := EncryStrHex(sw, '※★');
FWQMC := EncryStrHex(TRIM(EDTSERVER.Text), '※★');
SJKMC := EncryStrHex(TRIM(EDTDATA.Text), '※★');
YHMC := EncryStrHex(TRIM(EDTUSER.Text), '※★');
YHMM := EncryStrHex(TRIM(EDTPSW.Text), '※★');
MyReg := TRegistry.Create;
MyReg.RootKey := HKEY_LOCAL_MACHINE;
if MyReg.OpenKey('\SOFTWARE\SW\SWDB', true) then
begin
MyReg.WriteString('ConnectStr', sw);
MyReg.WriteString('SERVER', FWQMC);
MyReg.WriteString('DATABASE', SJKMC);
MyReg.WriteString('USER', YHMC);
MyReg.WriteString('PASSWORD', YHMM);
end;
MyReg.Free;
Application.MessageBox('数据库设置已保存!-', '提示', 0);
EDTSERVER.Enabled:=false;
EDTDATA.Enabled:=false;
EDTUSER.Enabled:=false;
EDTPSW.Enabled:=false;
end;
本文出自 “delphi” 博客,谢绝转载!