初学Delphi,如何用delphi编写ini文件设置SQL数据库的连接!急!(100分)

初学Delphi,如何用delphi编写ini文件设置SQL数据库的连接!急!
请各位专家帮助!十分感激!谢谢!

 

用Tintfile文件更简单。如下面是连接数据库的例子。
ini文件为(server.int):
[服务器]
服务器名=WJH
////////////////////////
var
myinifile:Tinifile;
servername:string;
begin
myinifile:=Tinifile.create(ExtractFilePath(paramstr(0))+‘Server.INI‘);
try
servername:=trim(myinifile.readstring(‘服务器‘,‘服务器名‘,‘error‘));
adoconnection1.Connected:=false;
adoconnection1.ConnectionString:=‘Provider=SQLOLEDB.1;Password=wjh;Persist Security Info=True;User ID=sa;Initial Catalog=wjh;Data Source=‘+servername+‘‘;
adoconnection1.Connected:=true;
except
begin
messagebox(0,‘Êý¾Ý¿âÁ¬½Óʧ°Ü£¬ÇëÓë¹ÜÀíÔ±ÁªÏµ‘,‘Á¬½Óʧ°Ü‘,mb_ok or mb_iconerror);
halt;
end;
end;

 

简单 使用TIniFile类!
var 
myini : TIniFile ;
begin
myini := TIniFile.Create(‘d:/abc.ini‘);
try
//写入整型值:
myini.WriteInteger(‘父类名称‘,‘子类名称‘,缺省值);
//读出整型值:
myini.ReadInteger(‘父类名称‘,‘子类名称‘,缺省值);
finally
myini.Free;
end;
end; 

 

ini文件格式
[DATABASE]
UserName=sa
Pwd= 
ServerName=p4
DatabaseName=spc
HostName=p4
然后在数据模块中如下写代码
procedure TDataModule1.DataModuleCreate(Sender: TObject);
var
ini: TIniFile;
ServerName, S,S1: string;
UserName, PWD: string;
DatabaseName,SqlqryMode,HostName:string;
begin
S := ExtractFilePath(Application.ExeName);
if S[Length(S)] <> ‘/‘ then
S := S +‘/‘ +‘Service.ini‘
else //‘C:/‘
S := S +‘Service.ini‘;
ini := TIniFile.Create(S);
try
UserName := ini.ReadString(‘Database‘, ‘UserName‘, ‘sa‘);
Pwd := ini.ReadString(‘Database‘, ‘Password‘, ‘‘);
ServerName := ini.ReadString(‘Database‘, ‘ServerName‘, ‘p4‘);
DatabaseName:=ini.ReadString(‘Database‘,‘DatabaseName‘,‘spc‘);
HostName:=ini.ReadString(‘Database‘,‘HostName‘,‘ ‘);
finally
ini.Free;
end;
try
ADOConnection1.Connected := false;
S1:=‘Provider=SQLOLEDB.1;‘+
‘Password=‘+PWD+‘;‘+
‘Persist Security Info=False;‘+
‘User ID=‘+UserName+‘;‘+
‘Initial Catalog=‘+DatabaseName+‘;‘+
‘Data Source=‘+ServerName+‘;‘+
‘Use Procedure for Prepare=1;‘+
‘Auto Translate=True;‘+
‘Packet Size=4096;‘+
‘Workstation ID=‘+HostName+‘;‘+
‘Use Encryption for Data=False;‘+
‘Tag with column collation when possible=False‘;
ADOConnection1.ConnectionString:=S1;
ADOConnection1.Connected := False;
ADOConnection1.Connected := True;
except
Application.MessageBox(‘连接数据库服务器失败, 程序异常终止!‘,
‘错误框‘, mb_Ok + mb_IconStop);
Application.Terminate;
end;
end;

初学Delphi,如何用delphi编写ini文件设置SQL数据库的连接!急!(100分)

上一篇:InfluxDB部署和使用


下一篇:Oracle 数据库坏块处理