使用 Delphi 以编程方式获取 ODBC 数据源名称列表

发布于 2024-12-08 15:16:04 字数 158 浏览 0 评论 0原文

我看到几个示例,其中源名称列表取自注册表(HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI\ODBC Data Sources)。还有其他方法可以获取 ODBC 数据源名称列表吗?

我只需要使用 Delphi 标准组件,因此无法使用 3d 方解决方案。

I saw several examples where the list of the source names were took from registry (HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI\ODBC Data Sources). Is there any other way to get the list of ODBC data sources names?

I need to work only with Delphi standard components, so I can not use 3d party solutions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惟欲睡 2024-12-15 15:16:04

您必须使用 ODBC32 中的 SQLDataSources 函数.DLL。对于示例

You have to use SQLDataSources function from ODBC32.DLL. For example.

七婞 2024-12-15 15:16:04

正如 @da-soft 在他们的回答中所述,SQLDataSources 将执行此操作。然而,由于他们提供的链接不再有效,这里有一个实际的例子(改编自 Menno Avegaar 对 一篇旧的 Delphi 小组帖子):

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

function SQLAllocEnv(var EnvironmentHandle: Pointer): SmallInt; stdcall; external 'odbc32.dll';
function SQLFreeEnv(EnvironmentHandle: Pointer): SmallInt; stdcall; external 'odbc32.dll';
function SQLDataSources(
  EnvironmentHandle: Pointer;
  Direction: Word;
  ServerName: PAnsiChar;
  BufferLength1: SmallInt;
  var NameLength1: SmallInt;
  Description: PAnsiChar;
  BufferLength2: SmallInt;
  var NameLength2: SmallInt): SmallInt; stdcall; external 'odbc32.dll';

const
  SQL_SUCCESS = 0;
  SQL_NO_DATA = 100;
  SQL_FETCH_NEXT = 1;
  SQL_FETCH_FIRST = 2;
  SQL_MAX_DSN_LENGTH = 32;
  SQL_MAX_OPTION_STRING_LENGTH = 256;

var
  EnvironmentHandle: Pointer;
  Buffer1: array[0..SQL_MAX_DSN_LENGTH] of AnsiChar;
  Buffer2: array[0..SQL_MAX_OPTION_STRING_LENGTH] of AnsiChar;
  Len1, Len2: SmallInt;
  ServerName, Description: String;
begin
  if SQLAllocEnv(EnvironmentHandle) = SQL_SUCCESS then
  begin
    try
      if SQLDataSources(
        EnvironmentHandle,
        SQL_FETCH_FIRST,
        Buffer1,
        SizeOf(Buffer1),
        Len1,
        Buffer2,
        SizeOf(Buffer2),
        Len2) = SQL_SUCCESS then
      repeat
        SetString(ServerName, Buffer1, Len1);   
        SetString(Description, Buffer2, Len2);
        Writeln('Name:'+ServerName);
        Writeln('Description:'+Description);
        Writeln('');
      until SQLDataSources(
        EnvironmentHandle,
        SQL_FETCH_NEXT,
        Buffer1,
        SizeOf(Buffer1),
        Len1,
        Buffer2,
        SizeOf(Buffer2),
        Len2) = SQL_NO_DATA;
    finally
      SQLFreeEnv(EnvironmentHandle);
    end;
  end;
  Readln;
end.

As @da-soft stated in their answer, SQLDataSources in ODBC32.DLL will do this. However, as the link they provided is no longer working, here's an actual example (adapted from Menno Avegaar's answer to an old delphi groups post):

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

function SQLAllocEnv(var EnvironmentHandle: Pointer): SmallInt; stdcall; external 'odbc32.dll';
function SQLFreeEnv(EnvironmentHandle: Pointer): SmallInt; stdcall; external 'odbc32.dll';
function SQLDataSources(
  EnvironmentHandle: Pointer;
  Direction: Word;
  ServerName: PAnsiChar;
  BufferLength1: SmallInt;
  var NameLength1: SmallInt;
  Description: PAnsiChar;
  BufferLength2: SmallInt;
  var NameLength2: SmallInt): SmallInt; stdcall; external 'odbc32.dll';

const
  SQL_SUCCESS = 0;
  SQL_NO_DATA = 100;
  SQL_FETCH_NEXT = 1;
  SQL_FETCH_FIRST = 2;
  SQL_MAX_DSN_LENGTH = 32;
  SQL_MAX_OPTION_STRING_LENGTH = 256;

var
  EnvironmentHandle: Pointer;
  Buffer1: array[0..SQL_MAX_DSN_LENGTH] of AnsiChar;
  Buffer2: array[0..SQL_MAX_OPTION_STRING_LENGTH] of AnsiChar;
  Len1, Len2: SmallInt;
  ServerName, Description: String;
begin
  if SQLAllocEnv(EnvironmentHandle) = SQL_SUCCESS then
  begin
    try
      if SQLDataSources(
        EnvironmentHandle,
        SQL_FETCH_FIRST,
        Buffer1,
        SizeOf(Buffer1),
        Len1,
        Buffer2,
        SizeOf(Buffer2),
        Len2) = SQL_SUCCESS then
      repeat
        SetString(ServerName, Buffer1, Len1);   
        SetString(Description, Buffer2, Len2);
        Writeln('Name:'+ServerName);
        Writeln('Description:'+Description);
        Writeln('');
      until SQLDataSources(
        EnvironmentHandle,
        SQL_FETCH_NEXT,
        Buffer1,
        SizeOf(Buffer1),
        Len1,
        Buffer2,
        SizeOf(Buffer2),
        Len2) = SQL_NO_DATA;
    finally
      SQLFreeEnv(EnvironmentHandle);
    end;
  end;
  Readln;
end.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文