请问如何编程添加数据源?(0分)

  • 主题发起人 主题发起人 diyer
  • 开始时间 开始时间
D

diyer

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何编程添加数据源?
 
刚好我的程序里有,用的是vfp的dbf驱动,你自己改一改就好了,肯定能用的,。

procedure TfrmMain.RegisterODBC;
var
reg: TRegistry;
Driver: string;
begin
//done : write to system Register and setup an ODBC dsn name bsdsn and point to the dir Data

//建立和更新odbc数据源
//查找ODBCINST.INI键,如果FoxPro DBF的驱动程序没有安装,则提示退出
//如果存在,则进行配置
reg := TRegistry.Create;
try
with reg do
begin
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey('Software/ODBC/ODBCINST.INI/Microsoft dBase VFP Driver (*.dbf)', False) then
begin //如果存在FoxproDBF 驱动程序
Driver := ReadString('Driver');
CloseKey;
if OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources', True) then
begin //注册一个DSN名称
WriteString(DSNname, 'Microsoft dBase VFP Driver (*.dbf)');
end
else
begin //创建键值失败
Application.MessageBox(pchar('在创建DSN' + dsnName + '时发生错误'), '创建ODBC数据源失败', MB_ICONINFORMATION or MB_OK);
exit;
end;
CloseKey;
//end 建立dsn
if OpenKey('Software/ODBC/ODBC.INI/' + DSNName , True) then
begin
WriteString('BackgroundFetch', 'Yes');
WriteString('Collate', 'Machine');
WriteString('Deleted', 'Yes');
WriteString('Description', '博士录取信息管理系统数据库');
WriteString('Driver', Driver);
WriteString('Exclusive', 'No');
WriteString('Null', 'Yes');
WriteString('SetNoCountOn', 'No');
WriteString('SourceDB', ExtractFilePath(Application.ExeName)+'data/');
WriteString('SourceType', 'DBF');
end
else
begin //创建键值失败
Application.MessageBox(pchar('在创建DSN' + DSNname + '时发生错误'), '创建ODBC数据源失败', MB_ICONINFORMATION or MB_OK);
exit;
end;
CloseKey;
end
else
Application.MessageBox('在当前机器上没有安装 Microsoft VFP 的ODBC 驱动程序!,请安装相应的驱动程序', '驱动程序出错', MB_ICONINFORMATION or MB_OK);
CloseKey;
end;
finally
reg.Free;
end;
end;
 
非常感谢楼上兄。
原来通过注册表实现。
 
后退
顶部