我想将ADOCONNECTION的连接字符串写入到INI文件中,再在程序中读取字符串进行数据库的连接
请问具体代码如何?

解决方案 »

  1.   

    inifile:=Tinifile.create(文件名)
    inifile.readstring(程序,项目,值);
    inifile.writestring(程序,项目,值);
    用上面的方法将你的参数写到INI文件中就可以了。
    delphi帮助上有ini文件的操作
      

  2.   

    //得到连接字符串
    function tdmodule.getstr():string;
    var path:String;
        tmp:TStrings;
        counter:Integer;
        myIniFile:Tinifile;//配置文件
    begin
      tmp:=TStringList.Create;
      try
        path:=ExtractFilePath(Application.Exename);
        myIniFile:=TiniFile.Create(path+'database.ini');
        myIniFile.ReadSectionValues('database',tmp);
        Result:=' ';
        for counter:=0 to tmp.Count-1 do
          Result:=Result+tmp.Strings[counter];
      finally
        tmp.Free;
      end;
    end;
    //连接数据库
    function tdmodule.connectedDB(var msg:string):boolean;
    begin
      if ADOConnection1.Connected then
         Result:=True
      else
      begin
      try
        ADOConnection1.ConnectionString:=getStr;
        adoconnection1.LoginPrompt:=false;
        adoquery1.Connection:=ADOConnection1;
        if length(trim(ADOConnection1.ConnectionString))=0 then
        begin
          msg:='错误的连接字符串!';
          Result:=False;
        end
        else
        begin
          ADOConnection1.Connected:=True;
          Result:=True;
        end;
      except
        msg:='无法建立数据库连接!';
        Result:=False;
      end;
      end;
    end;