1、我使用ACCESS做數據庫,每次換電腦或用其它用戶登陸時,又要通過ODBC重新在連過,怎麼樣解決?        
2、幾個按鍵,分別寫成了新增、修改、刪除、保存,如何在新增,保存後,數據能顯示在DBGRID,好像是讓數據刷新一遍,如何刷新,萬望詳細一點。

解决方案 »

  1.   

    1、可以动态的创建ODBC。其实就是对注册表的操作。网上很多例子。
    2、把你的数据集关闭再打开就可以了。
      

  2.   

    1.把ODBC设置为系统DSN.
    2.在操作之后,用Refresh刷新,如果不行就close,open一次刷新。
      

  3.   

    樓上兩位是正确的.
    我再附加一段動態創建dsn的代碼function Create_ODBC_DSN(const aDSNName, aDriverName, aDatabasePath: string): boolean;
    var
      mReg: TRegistry;
      bData: array[0..0] of byte;
    begin
      Result := False;
      mReg := TRegistry.Create;
      try
        mReg.RootKey := HKEY_CURRENT_USER; {!!!!HKEY_LOCAL_MACHINE???}
        if mReg.OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources', True) then
        begin //註冊一個DSN名稱
          mReg.WriteString(aDSNName, aDriverName);
        end
        else
        begin //創建鍵值失敗
          Exit;
        end;
        mReg.CloseKey;
        {找到或創建Software\ODBC\ODBC.INI\MyAccess, 寫入DSN配置資訊}
        if mReg.OpenKey('Software\ODBC\ODBC.INI\' + aDSNName, True) then
        begin
          mReg.WriteString('DBQ', aDatabasePath); //資料庫目錄
          mReg.WriteString('Description', DSN_DTS_DESCRIPT); //資料源描述
          mReg.WriteString('Driver', GetSystem32Dir() + '\SYSTEM32\odbcjt32.dll'); //驅動程式DLL文件
          mReg.WriteInteger('DriverId', 25); //驅動程式標識
          mReg.WriteString('FIL', 'Ms Access;'); //!!!Filter依據
          mReg.WriteInteger('SafeTransaction', 0); //支援的事務運算元目
          mReg.WriteString('UID', ''); //用戶名稱
          bData[0] := 0;
          mReg.WriteBinaryData('Exclusive', bData, 1); //非獨占方式
          mReg.WriteBinaryData('ReadOnly', bData, 1); //非唯讀方式
        end
        else //創建鍵值失敗
        begin
          Exit;
        end;
        mReg.CloseKey;
        //找到或創建Software\ODBC\ODBC.INI\ %s \Engines\Jet寫入DSN資料庫引擎配置資訊
        if mReg.OpenKey(Format('Software\ODBC\ODBC.INI\%s\Engines\Jet', [aDSNName]), True) then
        begin
          mReg.WriteString('ImplicitCommitSync', 'Yes');
          mReg.WriteInteger('MaxBufferSize', 512); //緩衝區大小
          mReg.WriteInteger('PageTimeout', 10); //頁超時
          mReg.WriteInteger('Threads', 3); //支援的線程數目
          mReg.WriteString('UserCommitSync', 'Yes');
        end
        else //創建鍵值失敗
        begin
          Exit;
        end;
        Result := True;
      finally
        mReg.CloseKey;
        mReg.Free;
      end;
    end;
      

  4.   

    舉個例吧,,,還有查詢,針對一個字段查詢可以用locate,那針對多條記錄呢
      

  5.   

    1,最好把数据库的连接保存在一个文件或一个注册表中,每次运行的时候去读取连接,如果连接失败则重新提示设置连接,如果设置后成功就把新的连接更新到文件或注册表中!
    2,用一个adoquery1就可以了,每次通过对数据库的增加,删除,修改等操作就可以很方便的搞顶
    具体来说,网上很多例子!
    比如删除:
    with adoquery1 do
    begin
      sql.close;
      sql.clear;
      sql.add('delete from table where 字段A=a');//这样就可以删除了,其他地方的操作类似
      execsql;  
    end;
      

  6.   

    如果是连接access 我劝你用adoConnection with adoConnection do
      begin
        if Connected = True then
          Close;
        ConnectionString := GetConnectString('access文件完整路径!');
        LoginPrompt := False;
        Open('Admin', '');
      endfunction GetConnectString(dataPath: string): string;//数据库的路径
    begin
      Result := '';
      if sSource <> '' then
        Result := 'Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;' +
          'Data Source=' + dataPath+ ';User ID=Admin;Jet OLEDB:Database Password=' +
          密码;
    end;我觉得这种连接方法更适合你
      

  7.   

    1,不要用ODBC  直接ADO方式连接数据库 同上代码 适合ACCESS