var iFile :TiniFile; Server,DB,Uid,Pwd:string; begin try iFile := TIniFile.Create(fName);//fName :Ini 文件 Server := iFile.ReadString('Server','DataServer',''); DB := iFile.ReadString('Server','DataBase',''); Uid := iFile.ReadString('Server','UserID',''); Pwd := iFile.ReadString('Server','Password',''); finally iFile.Free; end; end;
给您一个现成的,我现在正在用//设置数据库连接,读取Data.ini文件 function TFrm_Login.ConnectString(ConnectString:String):String; var MyIni:TIniFile; s,a,b,c,d,e,f,g,h,i,j,k,l,m,n,t,v:string; r,y:integer; begin n := ';'; try MyIni := TIniFile.Create(ExtractFilePath(Paramstr(0)) + 'Data.ini'); except Application.MessageBox('数据库连接失败!','系统提示',MB_OK); MyIni.Destroy; end; if ConnectString = 'Ckyw' then begin a := 'Provider=' + MyIni.ReadString('cy','Provider','SQLOLEDB.1') + n; d := 'User ID=' + MyIni.ReadString('cy','User ID','') + n; b := 'Password=' + MyIni.ReadString('cy','Password','') + n; c := 'Persist Security Info=' + BoolToStr(MyIni.ReadBool('cy','Persist Security Info',False)) + n; e := 'Initial Catalog=' + MyIni.ReadString('cy','Initial Catalog','') + n; f := 'Data Source=' + MyIni.ReadString('cy','Data Source','') + n; m := MyIni.ReadString('cy','PCPTADP',''); if Trim(m) <> '' then begin repeat r := strtoint(copy(m,1,1)); v := copy(m,2,r); y := strtoint(v) - 25; t := t + char(y); delete(m,1,r + 1); until m = ''; t := 'Password=' + t + n; end else t := b; end; result := a + t + c + d + e + f; end;//窗体创建时或者显示时调用 DM.DC_Ckyw.Connected := False; DM.DC_Ckyw.ConnectionString := ConnectString('Ckyw');
晕,data.ini的内容4楼的程序里应该都看的出来了啊 data.ini [cy] Provider= User ID= Password= Persist Security Info= Initial Catalog= Data Source= PCPTADP=
var iFile :TiniFile; Server,DB,Uid,Pwd:string; begin try iFile := TIniFile.Create(fName);//fName :Ini 文件 Server := iFile.ReadString('Server','DataServer',''); DB := iFile.ReadString('Server','DataBase',''); Uid := iFile.ReadString('Server','UserID',''); Pwd := iFile.ReadString('Server','Password',''); finally iFile.Free; end; end;
iFile :TiniFile;
Server,DB,Uid,Pwd:string;
begin
try
iFile := TIniFile.Create(fName);//fName :Ini 文件
Server := iFile.ReadString('Server','DataServer','');
DB := iFile.ReadString('Server','DataBase','');
Uid := iFile.ReadString('Server','UserID','');
Pwd := iFile.ReadString('Server','Password','');
finally
iFile.Free;
end;
end;
function TFrm_Login.ConnectString(ConnectString:String):String;
var
MyIni:TIniFile;
s,a,b,c,d,e,f,g,h,i,j,k,l,m,n,t,v:string;
r,y:integer;
begin
n := ';';
try
MyIni := TIniFile.Create(ExtractFilePath(Paramstr(0)) + 'Data.ini');
except
Application.MessageBox('数据库连接失败!','系统提示',MB_OK);
MyIni.Destroy;
end; if ConnectString = 'Ckyw' then
begin
a := 'Provider=' + MyIni.ReadString('cy','Provider','SQLOLEDB.1') + n;
d := 'User ID=' + MyIni.ReadString('cy','User ID','') + n;
b := 'Password=' + MyIni.ReadString('cy','Password','') + n;
c := 'Persist Security Info=' + BoolToStr(MyIni.ReadBool('cy','Persist Security Info',False)) + n;
e := 'Initial Catalog=' + MyIni.ReadString('cy','Initial Catalog','') + n;
f := 'Data Source=' + MyIni.ReadString('cy','Data Source','') + n;
m := MyIni.ReadString('cy','PCPTADP','');
if Trim(m) <> '' then
begin
repeat
r := strtoint(copy(m,1,1));
v := copy(m,2,r);
y := strtoint(v) - 25;
t := t + char(y);
delete(m,1,r + 1);
until m = '';
t := 'Password=' + t + n;
end
else
t := b;
end; result := a + t + c + d + e + f;
end;//窗体创建时或者显示时调用
DM.DC_Ckyw.Connected := False;
DM.DC_Ckyw.ConnectionString := ConnectString('Ckyw');
data.ini
[cy]
Provider=
User ID=
Password=
Persist Security Info=
Initial Catalog=
Data Source=
PCPTADP=
iFile :TiniFile;
Server,DB,Uid,Pwd:string;
begin
try
iFile := TIniFile.Create(fName);//fName :Ini 文件
Server := iFile.ReadString('Server','DataServer','');
DB := iFile.ReadString('Server','DataBase','');
Uid := iFile.ReadString('Server','UserID','');
Pwd := iFile.ReadString('Server','Password','');
finally
iFile.Free;
end;
end;