如何动态建立ACCESS的ODBC数据源  建了 一天了也不会   急    谢谢了

解决方案 »

  1.   

    用ADO的方法不好吗?不用建什么数据源的,很方便,
      

  2.   

    Standard Security:"Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;"
      

  3.   

    procedure TForm1.PrepareDataBase(AdoConnection: TAdoConnection; Source: string);
    const
      strProvider='Provider=Microsoft.Jet.OLEDB.4.0;';
      strMode    ='Mode=ReadWrite|Share Deny None;';
      strSecurity='Persist Security Info=False';
    var
      strSource: string;
    begin
      strSource:='Data Source='+Source+';';
      try
        adoConnection.Connected:=false;
        adoConnection.ConnectionString:= strProvider+strSource+strMode+strSecurity;
        adoConnection.Connected:=true;
      finally
      end;
    end;
      

  4.   

    ODBC 是为了连接table 和database  其他的我用的是 ADO的 连接字符串连接的
    qxj(Diamondback) :能详细点吗?
    例如一个按钮的时间就是
      

  5.   

    怪我没有说明白    对楼上的朋友表示道歉  我想用table 连接ACCESS  是不是只能通过ODBC???????怎样动态建立DSN!!!????
      

  6.   

    unit UnitODBC ;interface
    uses Windows,Registry;function ExistAccessDSN(aDSN:string;aDBName:string):boolean;function ConfigAccessODBC(aDSN:string;aDBName:string):boolean;implementationfunction ExistAccessDSN(aDSN:string;aDBName:string):boolean;
    var
       Reg: TRegistry;
       StrOdbcIni:string;
    begin
      StrOdbcIni:='Software\ODBC\ODBC.INI\';
      Reg := TRegistry.Create;
      ExistAccessDSN:=false;
      try
        Reg.RootKey := HKEY_LOCAL_MACHINE;
        if Reg.OpenKey(StrOdbcIni+'ODBC Data Sources',True) then
          if (Reg.ReadString(aDSN)='Microsoft Access Driver (*.mdb)') then
            begin
             Reg.CloseKey;
             if (Reg.KeyExists(StrOdbcIni+aDSN)) then
               begin
                 if Reg.OpenKey(StrOdbcIni+aDSN,false) then
                   if Reg.ReadString('DBQ')=aDBName then
                     ExistAccessDSN:=true
                   else
                     ExistAccessDSN:=false;
               end
             else
               ExistAccessDSN:=false;
            end
          else
            ExistAccessDSN:=false
        else
          ExistAccessDSN:=false;
        finally
           Reg.Closekey;
           Reg.Free;
        end;
     end;
    function ConfigAccessODBC(aDSN:string;aDBName:string):boolean;
    var
      reg: TRegistry;
      bData : array[ 0..0 ] of byte;
      DrvPath : string;
    begin
      reg:= TRegistry.Create;
      with reg do
      begin
        RootKey:=HKEY_LOCAL_MACHINE;
        if OpenKey('SOFTWARE\ODBC\ODBCINST.INI\Microsoft Access Driver (*.mdb)',false) then
        begin
          DrvPath:=ReadString('Driver');
        end
        else
        begin //创建键值失败
          //ShowMessage('公共信息'+':'+chr(10)+chr(10)+'您没有正确安装驱动程序Microsoft Access Driver (*.mdb),请重新安装!','错误信息', mb_applmodal+mb_iconerror+mb_ok+mb_defbutton1);
          ConfigAccessOdbc:=false;
          exit;
        end;
        Reg.CloseKey;
        //找到Software\ODBC\ODBC.INI\ODBC Data Sources
        if OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',True) then
        begin
          WriteString(aDSN, 'Microsoft Access Driver (*.mdb)' );
        end
        else
        begin //创建键值失败
          //application.MessageBox('公共信息'+':'+chr(10)+chr(10)+'配置ODBC数据源失败!','错误信息', mb_applmodal+mb_iconerror+mb_ok+mb_defbutton1);
          ConfigAccessOdbc:=false;
          exit;
        end;
        CloseKey;
       //找到或创建Software\ODBC\ODBC.INI\msac_infodb,写入DSN配置信息
       if OpenKey('Software\ODBC\ODBC.INI\'+aDSN,True) then
       begin
         WriteString( 'DBQ', aDBName);//数据库目录,连接您的数据库
         WriteString( 'Description','系统信息数据库数据源' );//数据源描述
         //WriteString( 'Driver', 'C:\WINNT\System32\odbcjt32.dll' );
         WriteString( 'Driver', DrvPath );//驱动程序DLL文件
         WriteInteger( 'DriverId', 25 ); //驱动程序标识
         WriteString( 'FIL', 'Ms Access;' );//Filter依据
         WriteInteger( 'SafeTransaction', 0 );//支持的事务操作数目
         WriteString( 'UID', '' );//用户名称
         bData[0] := 0;
         WriteBinaryData( 'Exclusive', bData, 1 ); //非独占方式
         WriteBinaryData( 'ReadOnly', bData, 1 ); //非只读方式
       end
       else//创建键值失败
       begin
         //application.MessageBox('公共信息'+':'+chr(10)+chr(10)+'配置ODBC数据源失败!','错误信息', mb_applmodal+mb_iconerror+mb_ok+mb_defbutton1);
         ConfigAccessOdbc:=false;
         exit;
       end;
       CloseKey;
      //找到或创建Software\ODBC\ODBC.INI\msac_infodb\Engines\Jet
      //写入DSN数据库引擎配置信息
       if OpenKey('Software\ODBC\ODBC.INI\'+aDSN+'\Engines\Jet',True) then
       begin
         WriteString( 'ImplicitCommitSync', 'Yes' );
         WriteInteger( 'MaxBufferSize', 512 );//缓冲区大小
         WriteInteger( 'PageTimeout', 10 );//页超时
         WriteInteger( 'Threads', 3 );//支持的线程数目
         WriteString( 'UserCommitSync', 'Yes' );
       end
       else//创建键值失败
       begin
         //application.MessageBox('公共信息'+':'+chr(10)+chr(10)+'配置ODBC数据源失败!','错误信息', mb_applmodal+mb_iconerror+mb_ok+mb_defbutton1);
         ConfigAccessOdbc:=false;
         exit;
       end;
       CloseKey;
       ConfigAccessOdbc:=True;
       Free;
      end;
    end;
    end.
      

  7.   

    用table连接access数据库?MS Access Database就是你所许吧。