function SQLConfigDataSource(hwndParent: Integer; fRequest: Integer;
lpszDriverString: String; lpszAttributes: String): Integer;
stdcall;external 'ODBCCP32.DLL';
两个函数:
function AddAccessODBC(SourceName,DatabaseName,Desc,password:string):boolean;
var intRet:longint;
    strDriver:string;
    strAttributes: string;
begin
  strDriver:= 'Microsoft Access Driver (*.mdb)';//      'Set the attributes delimited by null.
  strAttributes:= ('UID=admin'#0'DESCRIPTION='+Desc+#0'UserCommitSync=Yes'#0'Threads=3'#0'SafeTransactions=0'#0'PageTimeout=5'#0'MaxScanRows=8'#0'DSN='+SourceName+#0'MaxBufferSize=512'#0'ImplicitCommitSync=Yes'#0'FIL=MS Access'#0'DriverId=25'#0'DBQ='+DatabaseName+#0'PWD='+password+#0);
//上面的默认参数你可以自己修改
  intRet:= SQLConfigDataSource(0, ODBC_ADD_DSN, strDriver, strAttributes);
  result:=(intRet<>0);
end;//删除一个数据源
function DelAccessODBC(SourceName,DatabaseName,Desc,password:string):boolean;
var intRet:longint;
    strDriver:string;
    strAttributes: string;
begin
  strDriver:= 'Microsoft Access Driver (*.mdb)';//      'Set the attributes delimited by null.
  strAttributes:= ('UID=admin'#0'DESCRIPTION='+Desc+#0'UserCommitSync=Yes'#0'Threads=3'#0'SafeTransactions=0'#0'PageTimeout=5'#0'MaxScanRows=8'#0'DSN='+SourceName+#0'MaxBufferSize=512'#0'ImplicitCommitSync=Yes'#0'FIL=MS Access'#0'DriverId=25'#0'DBQ='+DatabaseName+#0'PWD='+password+#0);
  intRet:= SQLConfigDataSource(0, ODBC_REMOVE_DSN, strDriver, strAttributes);
  result:=(intRet<>0);
end;
注:使用installshield for delphi6简单多了,直接配置好数据源.
另外如果使用的是BDE的话,根本不用odbc,直接把别名指向数据库所在路径就行了.例如:安装的时候把access数据库放到程序所在目录下,在程序启动的时候设置:  database1.Alias:=ExtractFilePath(Application.ExeName)一切搞定.

解决方案 »

  1.   

    在ODBC中添加数据源可以通过Delphi对注册表做修改后添加,数据库的口令可以放在Database控件中。下面的代码创建了一个名为Book的ODBC数据源
    procedure TMainForm.CreateODBC;
    var
      registerTemp:TRegistry;
      dData:array[0..0] of byte;  StrPath:String;
    begin
      StrPath:=ExtractFilePath(Application.ExeName);  registerTemp:=TRegistry.Create;
      With registerTemp do
        begin
          RootKey:=HKEY_LOCAL_MACHINE;
          if OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',True) then
            WriteString('Book','Microsoft Access Driver (*.mdb)')
          else
            begin
              MessageDlg('创建ODBC数据源出错!',mtInformation,[mbOK],0);
              MainForm.Close;
            end;
          CloseKey;      if OpenKey('Software\ODBC\ODBC.INI\Book',True) then
            begin
              WriteString('DBQ',StrPath+'Book.mdb');
              WriteString('Description','图书管理系统');
              WriteString('Driver','C:\WINDOWS\SYSTEM\ODBCJT32.DLL');
              WriteInteger('DriverId',25);
              WriteString('FIL','Ms Access');
              WriteInteger('SafeTransaction',0);
              WriteString('UID','');
              dData[0]:=0;
              WriteBinaryData('Exclusive',dData,0);
              WriteBinaryData('ReadOnly',dData,0);
            end
          else
            begin
              MessageDlg(创建ODBC数据源出错!',mtInformation,[mbOK],0);
              MainForm.Close;
            end;
          CloseKey;      if OpenKey('Software\ODBC\ODBC.INI\Book\Engines\Jet',True) then
            begin
              WriteString('ImplicitCommitSync','Yes');
              WriteInteger('MaxBufferSize',512);
              WriteInteger('PageTimeOUt',10);
              WriteInteger('Threads',3);
              WriteString('UserCommitSync','Yes');
            end
          else
            begin
              MessageDlg(创建ODBC数据源出错!',mtInformation,[mbOK],0);
              MainForm.Close;
            end;
          CloseKey;      Free;
        end;
    end;
      

  2.   

    谢谢~!我在ODBC中添加成功了一个数据源,但是打开该数据源配置发现并没有指向相应的数据库文件,何故?
      

  3.   

    Express For Delphi5或installshield for delphi6哪里有下呀?
      

  4.   

    在我的代码中的第二个if语句里,有把数据库所在目录写入注册表的。
        WriteString('DBQ',StrPath+'Book.mdb');
    StrPath是应用程序所在目录,我的数据库文件就放在这个目录下。如果你是制定了目录,也可以修改。