uses Registry;
function get_driver_pathname(driver_mainkey:string):string;//获得一个驱动器的路径
var
reg:TRegistry;
begin
reg:= TRegistry.Create; //建立一个Registry实例
with reg do
begin
RootKey:=HKEY_LOCAL_MACHINE;//设置根键值为HKEY_LOCAL_MACHINE
if OpenKey('SOFTWARE/ODBC/ODBCINST.INI/'+driver_mainkey,True) then
begin
result:=readstring('Driver')
end;
free;
end;
end;
function create_syodbc(allias:string;Description:string;server_name:string;database_name:string;pwd:string;dbf_type:string):boolean;//sybase
var
registerTemp : TRegistry;
bData : array[ 0..0 ] of byte;
begin
registerTemp := TRegistry.Create; //建立一个Registry实例
with registerTemp do
begin
RootKey:=HKEY_LOCAL_MACHINE;//设置根键值为HKEY_CURRENT_USER在用户区
//找到Software/ODBC/ODBC.INI/ODBC Data Sources
if OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources',True) then
begin //注册一个DSN名称
WriteString( allias, dbf_type);
end
else
begin
//创建键值失败
result:=false;
exit;
end;
CloseKey;
//找到或创建Software/ODBC/ODBC.INI/MyAccess,写入DSN配置信息
if OpenKey('Software/ODBC/ODBC.INI/'+allias,True) then
begin
WriteString( 'Description', Description );//数据源描述
WriteString( 'Driver', get_driver_pathname(dbf_type));//驱动程序DLL文件
// WriteInteger( 'DriverId', 25 );//驱动程序标识
// WriteString( 'FIL', 'Ms Access;' );//Filter依据
//WriteInteger( 'SafeTransaction', 0 );//支持的事务操作数目
//WriteString( 'UID', uid );//用户名称
WriteString( 'LogonID', pwd);//用户密码
bData[0] := 0;
WriteString('ServerName',server_name);
WriteString('Servers',server_name);
WriteString('Databases',database_name);
WriteString('Database',database_name);
end
else
//创建键值失败
begin
result:=false;
exit;
end;
CloseKey;
//找到或创建Software/ODBC/ODBC.INI/MyAccess/Engines/Jet
//写入DSN数据库引擎配置信息
result:=true;
Free;
end;
end;
function create_dbfodbc(allias:string;dbf_type:string;data_path:string):boolean;//dbf
var
registerTemp : TRegistry;
begin
registerTemp := TRegistry.Create; //建立一个Registry实例
with registerTemp do
begin
RootKey:=HKEY_CURRENT_USER;//设置根键值为HKEY_LOCAL_MACHINE
//找到Software/ODBC/ODBC.INI/ODBC Data Sources
if OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources',True) then
begin //注册一个DSN名称
WriteString( allias, dbf_type);
end
else
begin
//创建键值失败
result:=false;
exit;
end;
CloseKey;
//找到或创建Software/ODBC/ODBC.INI/MyAccess,写入DSN配置信息
if OpenKey('Software/ODBC/ODBC.INI/'+allias,True) then
begin
WriteString('DefaultDir',data_path);
WriteString('Driver', get_driver_pathname(dbf_type));//驱动程序DLL文件
writeinteger('DriverId',533);
WriteString('FIL', 'dBase 5.0');//驱动程序DLL文件
writeinteger('SafeTransactions',0);
WriteString('UID', '');//驱动程序DLL文件
end
else
//创建键值失败
begin
result:=false;
exit;
end;
CloseKey;
if OpenKey('Software/ODBC/ODBC.INI/'+allias+'/Engines/xbase',True) then
begin
WriteString( 'CollatingSequence', 'ASCII' );
WriteInteger( 'Deleted', 01 );//缓冲区大小
Writestring( 'ImplicitCommitSync', ' ');//页超时
WriteInteger( 'PageTimeout', 600 );//支持的线程数目
Writeinteger('Statistics', 0 );
Writeinteger('Threads',3);
WriteString('UserCommitSync','YES');
end
else//创建键值失败
begin
result:=false;
exit;
end;
CloseKey;
result:=true;
Free;
end;
end;
作者:阎磊 摘自《程序人生》