(转载)将DELPHI数据库连接写进INI配置文件中

时间:2021-04-08 21:38:34

将DELPHI数据库连接写进INI配置文件中

procedure TDM.DataModuleCreate(Sender: TObject);
var piececonfg:Tinifile;
pathconfgstr,Providerstr,UserIDstr,
Passwordstr,DataSourceStr,DatabaseNamestr :string;
begin
pathconfgstr:= ExtractFilePath(Application.ExeName);
if pathconfgstr[length(pathconfgstr)]='/' then
begin
pathconfgstr:=pathconfgstr+'piececonfg.ini';
piececonfg:=Tinifile.Create(pathconfgstr);
end
else
begin
pathconfgstr:=pathconfgstr+'/piececonfg.ini';
piececonfg:=Tinifile.Create(pathconfgstr);
end;
if not FileExists(pathconfgstr) then
begin
application.MessageBox('配置文件不存在!','提示',mb_ok);
application.Terminate;
end;
Providerstr:=piececonfg.ReadString('DataBaseConfig','Provider','');
UserIDstr:=piececonfg.ReadString('DataBaseConfig','UserID','');
Passwordstr:=piececonfg.ReadString('DataBaseConfig','Password','');
DataSourceStr:=piececonfg.ReadString('DataBaseConfig','DataSource','');
DatabaseNamestr:=piececonfg.ReadString('DataBaseConfig','DatabaseName','');
if (Providerstr='') or (DataSourceStr='') or (DatabaseNamestr='') then
begin
application.MessageBox('数据库配置不正确,请重新配置!','提示',mb_ok);
Application.Terminate;
end;
adoconn.Close;
adoconn.ConnectionString:='';
adoconn.ConnectionString:='Provider='+Providerstr+';UserID='+UserIDstr+';
Password='+Passwordstr+';Data Source='+DataSourceStr+
Initial Catalog='+DatabaseNamestr;
try
adoconn.Connected:=true;
except
application.MessageBox('数据库配置不正确,请重新配置!','提示',mb_ok);
adoconn.Connected:=false;
application.Terminate;
end;
search:=TstringList.create;
end;