简体   繁体   English

需要在ODBC DSN连接的应用程序中标识数据库名称

[英]Need to Identify the Database name in an ODBC DSN connected application

I have a Delphi 6 application that uses an ODBC DSN to connect to target databases. 我有一个Delphi 6应用程序,它使用ODBC DSN连接到目标数据库。 I want to include text that lists the name of the Database the DSN is connected to. 我想要包含列出DSN所连接的数据库名称的文本。 I tried using the SQL command db_name() but only received a nil in response despite having it work when I log into the SQL server. 我尝试使用SQL命令db_name(),但只有在我登录SQL服务器时才能使用它才响应nil。

Is there a way within Delphi to identify which Database I'm connected to? Delphi中有没有办法识别我连接的数据库? I can pull up the sys.databases table, but am not certain how to identify which database is the one I'm connected to 我可以提取sys.databases表,但不确定如何识别哪个数据库是我连接的数据库

As an Example: 举个例子:

if I am connecting to the dsn LocalDSN I want to be able to display to the user that they are connected to Database , where database is the name of the sql database they are communicating with. 如果我连接到dsn LocalDSN,我希望能够向用户显示它们已连接到Database ,其中database是与之通信的sql数据库的名称。

The ODBC DSN is stored in the Windows Registry. ODBC DSN存储在Windows注册表中。 Keep in mind that the Windows Registry, and therefore the ODBC DSN settings, are separated between 32 and 64 bit versions. 请记住,Windows注册表以及ODBC DSN设置在32位和64位版本之间分开。 You can access this information through HKEY_LOCAL_MACHINE\\Software\\ODBC\\ODBC.INI\\[YOUR_DSN_NAME] and then read the value Database or Server to know the database or server name. 您可以通过HKEY_LOCAL_MACHINE\\Software\\ODBC\\ODBC.INI\\[YOUR_DSN_NAME]访问此信息,然后读取值DatabaseServer以了解数据库或服务器名称。

You can read the server and database name with these functions: 您可以使用以下函数读取服务器和数据库名称:

uses
  Registry;

function ServerOfDSN(const Name: String): String;
var
  R: TRegistry;
  K: String;
begin
  K:= 'Software\ODBC\ODBC.INI\'+Name;
  R:= TRegistry.Create(KEY_READ);
  try
    R.RootKey:= HKEY_LOCAL_MACHINE;
    if R.KeyExists(K) then begin
      if R.OpenKey(K, False) then begin
        if R.ValueExists('Server') then
          Result:= R.ReadString('Server');
        R.CloseKey;
      end;
    end;
  finally
    R.Free;
  end;
end;

function DatabaseOfDSN(const Name: String): String;
var
  R: TRegistry;
  K: String;
begin
  K:= 'Software\ODBC\ODBC.INI\'+Name;
  R:= TRegistry.Create(KEY_READ);
  try
    R.RootKey:= HKEY_LOCAL_MACHINE;
    if R.KeyExists(K) then begin
      if R.OpenKey(K, False) then begin
        if R.ValueExists('Database') then
          Result:= R.ReadString('Database');
        R.CloseKey;
      end;
    end;
  finally
    R.Free;
  end;
end;

Depending on what database engine and drivers you're using, the contents of this registry key may be different, and therefore there's a possibility that Server or Database might not be the registry value you need, but inspect it yourself and find your value names in the registry to know how to read it. 根据您使用的数据库引擎和驱动程序,此注册表项的内容可能不同,因此ServerDatabase可能不是您需要的注册表值,但您自己检查并找到您的值名称注册表知道如何阅读它。

You can use SQLGetPrivateProfileString ODBC API to get the contents of DSN created. 您可以使用SQLGetPrivateProfileString ODBC API来获取创建的DSN的内容。

int SQLGetPrivateProfileString(  
 LPCSTR   lpszSection,  
 LPCSTR   lpszEntry,  
 LPCSTR   lpszDefault,  
 LPCSTR   RetBuffer,  
 INT      cbRetBuffer,  
 LPCSTR   lpszFilename);

Here, 这里,

lpszSection = registry section you want details for. lpszSection =您想要详细信息的注册表部分。 it will be DSN name in your case. 在您的情况下它将是DSN名称。

lpszEntry = key which you want to extract value from. lpszEntry =要从中提取值的键。 you want to get database name information so you need to check registry entry HKEY_LOCAL_MACHINE\\Software\\ODBC\\ODBC.INI[YOUR_DSN_NAME] to know what is the key name to store database name information. 您想获取数据库名称信息,因此您需要检查注册表项HKEY_LOCAL_MACHINE \\ Software \\ ODBC \\ ODBC.INI [YOUR_DSN_NAME]以了解存储数据库名称信息的密钥名称。 This is because different driver can have different key name to store database name. 这是因为不同的驱动程序可以使用不同的键名来存储数据库名称。

lpszDefault = Default value for the key specified in last argument(lpszEntry) if key is not found. lpszDefault =如果找不到密钥,则在最后一个参数(lpszEntry)中指定的密钥的默认值。

RetBuffer = Pointer to Output buffer in which value for the specified key is received. RetBuffer =指向输出缓冲区的指针,其中接收指定键的值。

cbRetBuffer = size of buffer pointed to by RetBuffer in characters. cbRetBuffer = RetBuffer以字符指向的缓冲区大小。

lpszFilename = File name where you search these entries in. It will be odbc.ini in your case. lpszFilename =您在其中搜索这些条目的文件名。在您的情况下将是odbc.ini。

Sample example 示例示例

CHAR *dsn_name = "Your DSN name";
CHAR db_name[20];
char *odbcini = NULL;
odbcini = "odbc.ini";

SQLGetPrivateProfileString(dsn_name, (CHAR*)"DATABASE", (CHAR*)"", db_name, 
sizeof(db_name), odbcini);

It will search registry entry HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE or both depending on the config mode set(It can be set using SQLSetConfigMode ODBC API). 它将根据配置模式集搜索注册表项HKEY_CURRENT_USER或HKEY_LOCAL_MACHINE或两者(可以使用SQLSetConfigMode ODBC API设置)。 If mode is not explicitly set, it will search both HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE. 如果未明确设置mode,它将同时搜索HKEY_CURRENT_USER和HKEY_LOCAL_MACHINE。 Please refer https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlgetprivateprofilestring-function for more information. 有关更多信息,请参阅https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlgetprivateprofilestring-function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM