这问题困扰了我好久 ,我学了两次都未学会第一次  我连的数据库只要已更改位置就提示 未能找到数据库 (用的是access),网上一直在说可以通过ini、dll文件,但都没说怎么配置 ,只范范的说了下(这种就没学会)后有人告诉我如果数据库和程序在同一文件夹下,把前面路径 删了只留下数据库名称就行 ,这样我就可以把整个文件copy到其他人电脑上运行,位置也随意动(这样解决了!)。第二次  我改用sql sever,当我功能完成 把exe和sql数据库(同样在一文件夹)copy到其他电脑上(未安装sql)又出来连找不到 sql服务器(可能就是因为没装sql,具体啥原因不清楚),网上一查有事说通过ini、dll ,就是不说具体怎么做。最多告诉代码 ,也不知道代码要放哪 、那个文件怎么创建的。。肯定还有好多人和我一样想学学这到底怎么搞吧希望有高手帮我们指点一下(希望不要再告诉我“用ini文件配置一下数据库连接方式就可以了”)好人一生平安!(我只有一个积分 实在是没分送你 如果你要我全给你,全凭个人意愿)

解决方案 »

  1.   

    ini就行了,ini操作不用说了吧,你需要读取的就是保存到ini的信息,比如吧文件名,密码保存操ini,然后读取,组成链连接字符串
      

  2.   

    bdmh  兄 请教ini文件怎么写?我在哪调用、连接? 代码放哪呢 ?
      

  3.   

    Uses
      iniFile;
    procedure Test;
    var
      SysConfig : TiniFile;
    begin
      SysConfig := TiniFile.Create('文件路径');
      Conn.ConnectionStr := SysConfig.ReadString('DBConfig','ConnStr','');
      SysConfig.WriteString('DBConfig','ConnStr',Conn.ConnectionStr);
    end;
      

  4.   

    var
      MyIni:TIniFile;
      MyIni:=TIniFile.Create(AFilePath + 'my.ini');
      try
        MyIni.WriteString('Server','Name',SeverName);
        MyIni.WriteString('User','Name',Uid);
        MyIni.WriteString('Pwd','Password',Pwd);
      finally
       MyIni.Free;
      end;
    读取ini内容
    var
      ConnStr : string;
    begin
      try
        ConnStr := '';
        ConnStr := ConnStr + 'Provider = SQLOLEDB.1; ' ;
        ConnStr := ConnStr + 'Persist Security Info = True; ';
        ConnStr := ConnStr + 'Data Source = (local); ';
        ConnStr := ConnStr + 'Initial Catalog = '+SeverName+'; ';
        ConnStr := ConnStr + 'User ID = '+Uid+' ;';
        ConnStr := ConnStr + 'Password = '+Pwd+' ';
        Result  := Trim(ConnStr);
      finally  end;
    end;
    然后就拼一下adoconnect的连接字符串 就是上面的 类似 就ok了
      

  5.   

    var
      MyIni:TIniFile;
    begin
      AFilePath := ExtractFilePath(Application.Exename);  MyIni:=TIniFile.Create(AFilePath +'My.Ini');
    try
      SeverName := MyIni.ReadString('Server','Name','');
      Uid := MyIni.ReadString('User','Name','');
      Pwd := MyIni.ReadString('Pwd','Password','');
    finally
      MyIni.Free;
    end;
    刚才是写 这个是读 呵呵 搞错了
      

  6.   

    先感谢lengyuehui 、lante0301两位 你们说的已经很详细有点概念 我能再进一步问下:这些代码放在哪的 是delphi主窗口还是别的地方还有    MyIni.WriteString('Server','Name',SeverName);
      MyIni.WriteString('User','Name',Uid);
      MyIni.WriteString('Pwd','Password',Pwd);这里参数要配置吗,如果是 'Name'  'Name'  'Password'分别是什么?还是很感谢  我去试试吧  
      

  7.   

    它們一般代表用戶名、密碼和服務器名稱等。
    var
      ADOLink,DatIni:Tinifile;
      MutexHandle:Integer;
      DataBaseNm:string;
      IsCancel:Boolean=False;
      
    procedure  ExtractRes(ResType,ResName,ResNewName:String);
    var
     Res:TResourceStream;
    begin
      Res:=TResourceStream.Create(Hinstance,Resname,Pchar(ResType));
      Res.SavetoFile(ResNewName);
      Res.Free;
    end;begin
      if OpenMutex(MUTEX_ALL_ACCESS, FALSE, 'eHR') <> 0 then
      begin
        MessageDlg('eHr程式已經運行,請確認!',mtWarning,[mbOK],0);
        Exit;
      end;
      MutexHandle:=CreateMutex(nil,TRUE,'eHR');
      Application.Initialize;
      Application.Title:='';
      try
        DeleteFile(PChar(ExtractFilePath(ParamStr(0))+'Update.exe'));
        ExtractRes('EXEFILE','UpFile',ExtractFilePath(ParamStr(0))+'Update.exe');
      except
      end;
      if not FileExists(ExtractFilePath(Application.ExeName)+'Setup.ini') then
      begin
        Application.MessageBox('你沒有配置服務器連接,請重新配置!','提示',MB_OK+MB_ICONINFORMATION);
        sys_setup_f:=Tsys_setup_f.Create(Application);
        sys_setup_f.ShowModal;
        sys_setup_f.Free;
        sys_datamodule.Free;
        IsCancel:=True;
        Application.Run;
        Application.Terminate;
      end
      else
      begin
        DatIni:=Tinifile.Create(ExtractFilePath(Application.ExeName)+'Config.dll');
        try
          with DatIni do
          begin
            if ReadString('Config','Ver','')='' then
            begin
              WriteString('Config','Ver',MainVer);
              WriteBool('Config','ForcUpdate',False);
            end else
            begin
              MainVer:=ReadString('Config','Ver',MainVer);
              application.Title:=MainVer;
            end;    
          end;
        finally
          DatIni.Free;
        end;
        Application.CreateForm(Tsys_datamodule, sys_datamodule);
        ADOLink:=Tinifile.Create(ExtractFilePath(Application.ExeName)+'Setup.ini');
        with  ADOLink do
        begin
          DataBaseNm:=ReadString('Setup','DataBaseName','');
          _ServerNmae:=ReadString('Setup','ServerName','');
          sys_datamodule.con1.Connected:=False;
          sys_datamodule.con1.ConnectionString:='Provider=SQLNCLI10;Application Name=MES_'+MainVer+';Server='+_ServerNmae+';Database='+DataBaseNm+';Failover Partner='+_ServerNmae+';UID='+ReadString('Setup','UserName','')+';PWD='+Dec(ReadString('Setup','Password',''))+';';
          free;
        end;
        try
          if not IsCancel then
          begin
            try
              sys_datamodule.con1.Connected:=true;
            except
              Application.MessageBox('服務器連接配置錯誤,請重新配置!!!','提示',MB_OK+MB_ICONINFORMATION);
              sys_setup_f:=Tsys_setup_f.Create(Application);
              sys_setup_f.ShowModal;
              sys_setup_f.Free;
              sys_datamodule.Free;
              IsCancel:=True;
              Application.Run;
              Application.Terminate;
            end;
          end;
          if not IsCancel then
          begin
            sys_Login_f:=Tsys_Login_f.Create(Application);
            if sys_Login_f.ShowModal=mrOK then
            begin
              sys_Login_f.Free;
              sys_Login_f:=nil;
              Application.CreateForm(TMain_F, Main_F);
              RegisterClasses([TModule_F,Tsys_user_f,TShortCut_F,TRES_CQ_JB_F,TRes_Hr_JSD_F,TRes_Hr_BlackList_F,
                              TBuyerCode_Frm,TBuyerCodeSetting_Frm,TRES_HTMB_F,TRes_HrLZ_F,TRes_HrJsSH_F,TRes_Department_F,
                              TRES_HTQD_F]);
              Application.Run;
            end
            else
            begin
              Application.Run;
              Application.Terminate;
            end;
          end;
        except
          sys_datamodule.con1.Connected:=False;
          sys_datamodule.Free;
          Application.Run;
          Application.Terminate;
        end;
      end;
      ReleaseMutex(MutexHandle);