var
registerTemp : TRegistry;
bData : array[ 0..0 ] of byte;
MySysPath : PCHAR ;
begin
GetMem(MySysPath,255);
GetSystemDirectory(MySysPath,255);
registerTemp := TRegistry.Create; //建立一个Registry实例
with registerTemp do
begin
RootKey:=HKEY_CURRENT_USER;//设置根键值为HKEY_LOCAL_MACHINE
if OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources',True) then
begin //注册一个DSN名称
WriteString( 'sheyban', 'Microsoft Access Driver (*.mdb)' );
end
else
begin//创建键值失败
showmessage('增加ODBC数据源失败');
exit;
end;
CloseKey;
//找到或创建Software/ODBC/ODBC.INI/MyAccess,写入DSN配置信息
if OpenKey('Software/ODBC/ODBC.INI/sheyban',True) then
begin
if opendialog1.Execute then
WriteString( 'DBQ', opendialog1.FileName)//数据库目录
else
WriteString( 'DBQ', 'C:/sg/sgyfq.mdb' );
WriteString( 'Description', '交流采样数据源' );//数据源描述
WriteString( 'Driver', MySysPath+'/odbcjt32.dll' );//驱动程序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
showmessage('增加ODBC数据源失败');
exit;
end;
CloseKey;
//找到或创建Software/ODBC/ODBC.INI/MyAccess/Engines/Jet
//写入DSN数据库引擎配置信息
if OpenKey('Software/ODBC/ODBC.INI/sheyban/Engines/Jet',True) then
begin
WriteString( 'ImplicitCommitSync', 'Yes' );
WriteInteger( 'MaxBufferSize', 512 );//缓冲区大小
WriteInteger( 'PageTimeout', 10 );//页超时
WriteInteger( 'Threads', 3 );//支持的线程数目
WriteString( 'UserCommitSync', 'Yes' );
end
else//创建键值失败
begin
showmessage('增加ODBC数据源失败');
exit;
end;
CloseKey;
showmessage('增加新ODBC数据源成功');
Free;
end;
form1.Close;
end;