动态创建ODBC ACCESS数据源

 
unit LinkDBAccess;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,  StdCtrls,Registry, ExtCtrls;
type
  TLinkDB_info = class(TForm)
    Memo1: TMemo;
    pnllink: TPanel;
    pnlclose: TPanel;
    procedure pnllinkClick(Sender: TObject);
    procedure pnlcloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  LinkDB_info: TLinkDB_info;
  //连接Access数据库
  const  databaseName='\data\ShipDB.mdb';   //数据库名 (SQL中的)
       ODBCName='ShipDB';       //ODBC数据源名
       Aboutname='提成管理';//数据源描述
implementation
{$R *.dfm}

procedure TLinkDB_info.pnllinkClick(Sender: TObject);
var
  registerTemp : TRegistry;
begin
registerTemp := TRegistry.Create; //建立一个Registry实例
with registerTemp do
begin
RootKey:=HKEY_LOCAL_MACHINE;//设置根键值为HKEY_LOCAL_MACHINE
//找到Software\ODBC\ODBC.INI\ODBC Data Sources
if OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',True) then
begin //注册一个DSN名称
WriteString( ODBCName,'Driver do Microsoft Access (*.mdb)');
end
else
begin//创建键值失败
memo1.lines.add('连接ODBC数据源失败');
exit;
end;
CloseKey;
//找到或创建Software\ODBC\ODBC.INI\MGMIS_XDB,写入DSN配置信息
if OpenKey('Software\ODBC\ODBC.INI\'+ODBCName,True) then
begin
WriteString( 'DBQ', GetCurrentDir+databaseName);
WriteString( 'DriverId', '25' );
WriteString('Driver','C:\WINNT\System32\odbcjt32.dll' );//驱动程序DLL文件
Writestring( 'FIL', 'MS Access;');
WriteString( 'SafeTransactions','0' );
Writestring( 'UID', '' );
end
else//创建键值失败
begin
memo1.lines.add('连接ODBC数据源失败');
exit;
end;
CloseKey;
if OpenKey('Software\ODBC\ODBC.INI\'+ODBCName+'\Engines\Jet',True) then
begin
WriteString( 'ImplicitCommitSync', '');
WriteString( 'MaxBufferSize', '2048' );
WriteString('PageTimeout','5' );
Writestring( 'Threads', '3');
WriteString( 'UserCommitSync','Yes' );
end
else//创建键值失败
begin
memo1.lines.add('连接ODBC数据源失败');
exit;
end;
CloseKey;
CloseKey;
memo1.lines.add('连接ODBC数据源成功');
Free;
end;
end;
procedure TLinkDB_info.pnlcloseClick(Sender: TObject);
begin
Close ;
end;
procedure TLinkDB_info.FormCreate(Sender: TObject);
begin
Self.Caption:=Aboutname;
end;
end.
 
其它数据库也一样,只要按照注册表写进去就可以

你可能感兴趣的:(sql,windows,Microsoft,配置管理,Access)