请问如何读取.ini文件(在delphi中)
请问我要从中读取access有很多个数据库,这样的ini文件怎么样写
请问我要用ado如果得到.ini中的数据库联结的用户名和密码。还有如果查询。

解决方案 »

  1.   

    procedure TForm1.FormActivate(Sender: TObject);var
      MyIniFile: TIniFile;
    begin
      MyIniFile := TIniFile.Create('myapp.ini');
      with MyIniFile do
      begin
        if ReadBool('AutoLoad', 'FormProperties', False) = True then
        begin
          Visible := ReadBool('FormOptions', 'Visible', True);
          Color := TColor(ReadInteger('FormOptions', 'Color', clNormalBackground));
          Caption := ReadString('FormOptions', 'Caption', 'Main');
      end;
      MyIniFile.Free;end;
      

  2.   

    function TDM.ConnWaterMCDB(): Boolean;
    var
      ServerName,DBName,LoginType:string;
      F:TIniFile;
      S:string;
    begin
      F:=TIniFile.Create(frmMain.GetAppPath()+'Config.INI');
      ServerName:=F.ReadString('DataBase','ServerName','127.0.0.1');
      DBName:=F.ReadString('DataBase','DataBaseName','WaterMC');
      LoginType:=F.ReadString('DataBase','LoginType','0');
      F.Free;
      s:='Provider=SQLOLEDB.1;Integrated Security=SSPI;'+
        'Persist Security Info=False;Initial Catalog='+DBName+';'+
        'Data Source='+ServerName;
      if LoginType='1'then S:=S+';Integrated Security=SSPI';
      ADOCon.Connected:=false;
      ADOCon.ConnectionString:=S;  try
        ADOCon.Connected:=true;
      except
        {if Application.MessageBox('连接数据库失败,'+#13+
                                  '请检查服务器是否启动或设置是否正确!'+#13#13+
                                  '是否现在进行服务器设置?',
                                  '信息',MB_OKCANCEL+MB_ICONQUESTION)=IDOK then
            WinExec('CenSetSvr.exe',SW_SHOW); }
        Result:=false;
        ShowMessage('与数据库建立连接失败,请在系统配置中重新初始化变量!');
        frmMain.A_SystemInitExecute(self);
      end;
    end;
      

  3.   

    读取
    procedure TForm1.FormActivate(Sender: TObject);
    var
    filename:string;
    begin
     filename:=ExtractFilePath(paramstr(0))+'zdqreg.ini';
     myinifile:=TInifile.Create(filename);
     suiedit1.Text:= myinifile.readstring('main','DBServerName','');
     suiedit2.text:=myinifile.readstring('main','DBName','');
     suiedit3.Text:= myinifile.readstring('main','userID','');
     suiedit4.text:=myinifile.readstring('main','password','');
    end;
      

  4.   

    做成DLL
    function GetRegInfo(Flag:Integer):ShortString;
    var
        filename,p:string;
        fp:Tinifile;
    begin
      filename:= ExtractFilePath(ParamStr(0))+'zdqreg.ini';
       fp:=Tinifile.create(filename);
       case Flag of
          0:begin   //0应用程序服务器名称
              p := 'AppServerName';
            end;
          1:begin   //1数据库服务器名称
             p := 'DBServerName';
            end;
          2:begin   //数据库名称
              p := 'DBName';
            end;
          3:begin   //登陆帐号
              p := 'userID';
            end;
          4:begin   //登陆密码
              p := 'password';
            end;
         5:begin   //连接方式
              p := 'LinkAppsMode';
           end;
       end;
      if Flag=4 then begin
          Result:=pwDecrypt(fp.readstring('main',p,''))
       end else  Result:=fp.readstring('main',p,'');
    end;
      

  5.   

    使用:
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    ADOConnection1.ConnectionString:=' Provider=SQLOLEDB.1;'+'Persist Security Info=False;User ID='+GetRegInfo(3)+';Password='+GetRegInfo(4)+';'+
                      'Initial Catalog='+GetRegInfo(2)+';Data Source='+GetRegInfo(1);
    end;end.