可以在程序中动态创建配置。以下的程序片断创建数据库的目录为程序执行目录:
var
Temp : TRegistry;
s:string;
i,nXx,nCy:integer;
begin
Temp := TRegistry.Create; //建立一个Registry实例
with Temp 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( 'MyHz', 'Microsoft FoxPro VFP Driver (*.dbf)' );
end
else
begin//创建键值失败
SHOWMESSAGE('增加ODBC数据源失败');
exit;
end;
CloseKey;
//找到或创建Software/ODBC/ODBC.INI/myaccess97,写入DSN配置信息
if OpenKey('Software/ODBC/ODBC.INI/MyHz',True) then
begin
WriteString( 'BackgroundFetch', 'Yes' );//数据库目录
WriteString( 'Collate', 'Machine' );
WriteString( 'Deleted', 'Yes' );
WriteString( 'Description','My Reg' );//数据源描述
WriteString( 'Driver', 'C:/WINDOWS/SYSTEM/VFPODBC.DLL' );//驱动程序DLL文件 ODBCJT32.DLL文件依据你的存放路径
WriteString( 'Exclusive', 'No' );
WriteString( 'SourceDB',ExtractFileDir(ExtractFilePath(Application.EXEName)));
WriteString( 'SourceType', 'DBF' );
WriteString( 'Null', 'Yes' );
WriteString( 'SetNoCountOn', 'No' );
end
else//创建键值失败
begin
showmessage('增加ODBC数据源失败');
exit;
end;
CloseKey;
end;