unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms, Dialogs, Buttons, Registry, DB, ADODB;
type
TForm1 = class(TForm)
SpeedButton1: TSpeedButton;
ADOConnection1: TADOConnection;
procedure SpeedButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
const
ProMsg='测试注册ODBC数据源';
implementation
{$R *.dfm}
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
wReg :TRegistry;
tmpPath :array[0..255] of Char;
SysPath :string;
tmpDBPath :string;
tPath :string;
begin
GetSystemDirectory(@tmpPath,255);
sysPath :=StrPas(tmpPath);
tPath :=ExtractFilePath(Application.Exename);
tmpDBPath :=tPath+'../MDB/Test.mdb'; //指定连接数据库的路径
if not FileExists(tmpDBPath) then
begin
MessageBox(handle,pChar('找不到存储在 '+tmpDBPath+' 的数据文件!'),
ProMsg,mb_ok+mb_IconWarning);
Exit;
end;
//建立一个Registry实例
wReg := TRegistry.Create;
//-==注册ODBC==-
with wReg do
begin
RootKey:=HKEY_LOCAL_MACHINE;
if OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources',True) then
WriteString('RegTest', 'Microsoft Access Driver (*.mdb)')
else
begin
Application.MessageBox('ODBC 初始化错误',proMsg,
mb_ok+MB_ICONEXCLAMATION);
exit;
end;
CloseKey;
//写入DSN配置信息
if OpenKey('Software/ODBC/ODBC.INI/RegTest',True) then
begin
WriteString('DBQ',tmpDBPath); //数据库目录
WriteString('Description','Pacs数据源'); //数据源描述
WriteString('Driver',SysPath+'/odbcjt32.dll'); //驱动程序DLL文件
WriteInteger('DriverId',25 );
WriteInteger('SafeTransaction', 0); //支持的事务操作数目
WriteString('UID', ''); //用户名称
end
else
begin
Application.MessageBox('ODBC 初始化错误',proMsg,
mb_ok+MB_ICONEXCLAMATION);
exit;
end;
CloseKey;
//写入DSN数据库引擎配置信息
if OpenKey('Software/ODBC/ODBC.INI/RegTest/Engines/Jet',True) then
begin
WriteString('ImplicitCommitSync', 'Yes');
WriteInteger('MaxBufferSize',2048); //缓冲区大小
WriteInteger('PageTimeout',10); //页超时
WriteInteger('Threads',3); //支持的线程数目
WriteString('UserCommitSync','Yes');
end
else
begin//创建键值失败
Application.MessageBox('ODBC 初始化错误',proMsg,mb_ok+
MB_ICONEXCLAMATION);
exit;
end;
CloseKey;
Free;
end;
Application.MessageBox('用户信息注册成功',proMsg,
mb_ok+mb_iconinformation);
end;
end.