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

来源:www.swgame.cn  作者:系统维护员
摘要:要将DELPHI数据库连接写进INI配置文件中,参数代码如下:DataBase.pas++++++++++++++++++++++++++++++++++++++++++++procedure TDM.DataModuleCreate(Se……

要将DELPHI数据库连接写进INI配置文件中,参数代码如下:

DataBase.pas
++++++++++++++++++++++++++++++++++++++++++++

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;

piececonfg.ini
++++++++++++++++++++++++++++++++++++++++++++

[DataBaseConfig]
   Provider=SQLOLEDB.1
   UserID=sa
   Password=sa
   DataSource=192.168.1.1
   DatabaseName=mjj

【相关文章】好搜一下
Delphi2010 体验国内首发

Delphi2010 体验国内首发

两天前拿到了Delphi2010的内测版光盘,然后装上了试用,经过将近两天的试用…