起步软件技术论坛-X3

 找回密码
 立即注册
搜索
查看: 169|回复: 5

【结贴】平台用如何动态创建ODBC ?

[复制链接]
发表于 2009-12-24 10:40:02 | 显示全部楼层 |阅读模式
我使用第三方控件连接数据库,需要用到ODBC来连接。
我如何动态创建这个OCBC呢 ?
回复

使用道具 举报

发表于 2009-12-24 11:00:20 | 显示全部楼层
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-12-24 11:03:31 | 显示全部楼层
function CreateSQLServerDSN(const FDSN, FServer, FUser: string): boolean;
var
  RegisterTemp: TRegistry;
  s: string;
begin
  RegisterTemp := TRegistry.Create;
  with RegisterTemp do
  begin
    RootKey := HKEY_LOCAL_MACHINE;
    //首先判断此DSN是否已经存在,如在,则不用继续下去
    if OpenKey('Software\ODBC\ODBC.INI\' + FDSN, False) then
    begin
      CloseKey;
      Result := True;
      Free;
      exit;
    end;
    CloseKey;
    //判断SQL Server的驱动程序是否已经安装
    if OpenKey('Software\ODBC\ODBCINST.INI\ODBC Drivers', False) then
    begin
      s := ReadString('SQL Server');
      if UpperCase(S) <> 'INSTALLED' then
      begin
        CloseKey;
        Result := False;
        Free;
        exit;
      end;
    end
    else
    begin
      CloseKey;
      Result := True;
      Free;
      exit;
    end;
    CloseKey;
    //寻找SQL Server的驱动程序路径
    if OpenKey('Software\ODBC\ODBCINST.INI\SQL Server', False) then
    begin
      s := ReadString('Driver');
      if S = '' then
      begin
        CloseKey;
        Result := False;
        Free;
        exit;
      end;
    end
    else
    begin
      CloseKey;
      Result := True;
      Free;
      exit;
    end;
    CloseKey;
    //注册一个DSN名称
    if OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources', True) then
      WriteString(FDSN, 'SQL Server')
    else
    begin
      CloseKey;
      Result := True;
      Free;
      exit;
    end;
    CloseKey;
    if OpenKey('Software\ODBC\ODBC.INI\' + FDSN, True) then
    begin
      WriteString('Driver', S);
      WriteString('LastUser', FUser);
      WriteString('Server', FServer);
      Result := True;
    end
    else
    begin
      CloseKey;
      Result := False;
      Free;
      exit;
    end;
    CloseKey;
    Free;
  end;
end;


-----------------------
在平台上如何转换 ?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-12-24 11:05:03 | 显示全部楼层
RootKey := HKEY_LOCAL_MACHINE;
回复 支持 反对

使用道具 举报

发表于 2009-12-24 11:15:44 | 显示全部楼层
你用:HKEY_LOCAL_MACHINE
在帮助中搜索一下
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-12-24 17:36:00 | 显示全部楼层
OK 结贴
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Justep Inc.

GMT+8, 2025-7-12 21:25 , Processed in 0.039501 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表