动态创建ODBC数据源
时间:2011-05-01 来源:巅枫
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Memo1: TMemo;
Label1: TLabel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
Registry;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
reg:TRegistry;
data:array[0..0] of Byte;
begin
reg:=TRegistry.Create;
with reg do
begin
RootKey:=HKEY_CURRENT_USER; //设置根键值为HKEY_CURRENT_USER
if OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',True) then
//查找Software\ODBC\ODBC.INI\ODBC Data Sources目录,如果没有则创建
begin
WriteString('first','Microsoft Access Driver(*.mdb)');//注册一个DNS名称
end
else
begin
ShowMessage('数据源添加失败。');
Exit;
end;
CloseKey;
if OpenKey('Software\ODBC\ODBC.INI\first',True) then
begin
WriteString('DBQ','C:\My Documents\db1.mdb');//设置数据库目录
WriteString('description','我的数据源。');//数据库描述,也可不写此项
WriteString('Driver','c:\WINDOWS\SYSTEM\ODBCJT32.dll');//驱动程序动态连接库文件
WriteInteger('DriverId',25);//驱动程序标识
WriteString('FIL','MS Access;');//查找依据
WriteInteger('Safe Transaction',0);//支持的事务数目
WriteString('UID','');//用户名称
Data[0]:= 0 ;
WriteBinaryData('Exclusive',Data,1);//非独占方式
WriteBinaryData('ReadOnly',Data,1); //非只读方式
end
else
begin
ShowMessage('数据源添加失败。');
Exit;
end;
CloseKey;
if OpenKey('Software\ODBC\ODBC.INI\first\Engines\Jet',True) then
begin
WriteString('ImplicitCommitSync','');
WriteInteger('MaxBufferSize',2048);//缓冲区大小
WriteInteger('PageTimeout',5);//页超时
WriteInteger('Threads',3); //支持的线程数目
WriteString('UserCommitSync','yes');
end
else
begin
ShowMessage('数据源添加失败。');
Exit;
end;
CloseKey;
ShowMessage('数据源添加成功。');
free;
end;
end;
end.
相关阅读 更多 +










