Identify the database name in the connected ODBC DSN application

I have a Delphi 6 application that uses DSN ODBC to connect to target databases. I want to include text that indicates the name of the database to which the DSN is connected. I tried using the db_name () SQL command, but only got zero, despite the fact that it works when I log in to the SQL server.

Is there a way in Delphi to determine which database I am connected to? I can pull out the sys.databases table, but I don’t know how to determine which database is the one with which I am connected,

As an example:

If I connect to dsn LocalDSN, I want to show the user that they are connected to the database, where the database is the name of the SQL database with which they interact.

+5
source share
2 answers

DSN ODBC is stored in the Windows registry. Keep in mind that the Windows registry and therefore the ODBC DSN settings are shared between 32 and 64 bit versions. You can access this information through HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI\[YOUR_DSN_NAME]and then read the value Databaseor Serverto find out the name of the database or server.

You can read the server and database names using the following 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 which database engine and drivers you are using, the contents of this registry key may be different, and therefore it is possible that Serveror Databasemay not be a necessary registry parameter, but check it and find your values ​​in the registry to find out how to read them.

+3
source

API SQLGetPrivateProfileString ODBC DSN.

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

lpszSection= , . DSN .

lpszEntry= , . , HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI [YOUR_DSN_NAME], , . , .

lpszDefault= , (lpszEntry), .

RetBuffer= , .

cbRetBuffer= , RetBuffer .

lpszFilename= , . odbc.ini .

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);

HKEY_CURRENT_USER HKEY_LOCAL_MACHINE ( API- SQLSetConfigMode ODBC). , HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE. . https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlgetprivateprofilestring-function.

+1

All Articles