Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Querying the registry for database type of ODBC DSN

Status
Not open for further replies.

AlanHearnshaw

Programmer
Mar 10, 1999
2
0
0
GB
I am trying to determine the database type of a database from the ODBC DSN that is set up on the client computer.<br>
I cannot seem to get enough reliable information from the connect string once the database is opened and am trying to query the value:<br>
"HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources".<br>
I can open the key using the "RegOpenKey" API but cannot either enumerate the key values or retrieve any of the key values. I have tried both "RegEnumValueEx" and "RegQueryValueEx" but both return an error. Am I right in thinking that if the API call asks for a data type "Byte" then I set up a Byte array and send the first element of the array to the API call? This is necessary with "RegQueryValueEx".<br>
Thanks in advance to any API experts out there.
 
In the absence of any replies I managed to sort the problem out myself. Here is the source for the problem. I was trying to query an ODBC DSN to determine whether the database type was Access or SQL Server:-<br>
Public Function GetDatabaseType(vsConnect As String, vtType As DatabaseType) As Boolean<br>
<br>
'This function will Query the registry for the string value of the DSN passed and will analyse the<br>
'string to return one of the database types.<br>
<br>
Dim lKeyHandle As Long<br>
Dim sData As String<br>
Dim lRetHandle As Long<br>
Dim lReturn As Long<br>
Dim lLenReturnValue As Long<br>
Dim lType As Long<br>
Dim sRetString As String<br>
Dim sDSNName As String<br>
<br>
'Get the DSN name from the connect string<br>
sDSNName = Mid(vsConnect, InStr(vsConnect, "DSN=") + 4)<br>
sDSNName = Left(sDSNName, InStr(sDSNName, ";") - 1)<br>
<br>
lReturn = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.ini\ODBC Data Sources", 0, KEY_QUERY_VALUE, lRetHandle)<br>
If lReturn &lt;&gt; 0 Then<br>
Err.Raise erRegistryError, "clsArchive::GetDatabaseType", "Unable to open the ODBC Data Sources registry key."<br>
Exit Function<br>
End If<br>
<br>
sData = String$(1024, 0)<br>
lLenReturnValue = 1024<br>
lReturn = RegQueryValueEx(lRetHandle, sDSNName, 0, lType, sData, lLenReturnValue)<br>
If lReturn &lt;&gt; 0 Then<br>
Err.Raise erRegistryError, "clsArchive::GetDatabaseType", "Unable to retrieve the registry key value (" & sDSNName & ")."<br>
RegCloseKey lRetHandle<br>
Exit Function<br>
Else<br>
GetDatabaseType = True<br>
sRetString = Left(sData, lLenReturnValue - 1)<br>
If InStr(UCase(sRetString), "ACCESS") &gt; 0 Then<br>
vtType = dtAccess<br>
ElseIf InStr(UCase(sRetString), "SQL SERVER") &gt; 0 Then<br>
vtType = dtSQLServer<br>
Else<br>
vtType = dtUnknown<br>
End If<br>
GetDatabaseType = True<br>
End If<br>
<br>
RegCloseKey lRetHandle<br>
<br>
End Function<br>
<br>
Well, somebody may be wondering!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top