Skip to content

Instantly share code, notes, and snippets.

@Ewerton
Created September 8, 2020 13:03
Show Gist options
  • Save Ewerton/408a7f8634648a6ed2382f481a1e2249 to your computer and use it in GitHub Desktop.
Save Ewerton/408a7f8634648a6ed2382f481a1e2249 to your computer and use it in GitHub Desktop.
Gets the server name from a DSN registered in the computer
'Gets the server name from a DSN registered in the computer
Public Shared Function GetServerNameFromDSN(ByVal nomeDSN As String) As String
Dim retorno As String = String.Empty
Try
Using key As RegistryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\ODBC\ODBC.INI\" & nomeDSN)
If key IsNot Nothing Then
Dim o As Object = key.GetValue("Server")
If o IsNot Nothing Then
retorno = o.ToString()
Return retorno
End If
End If
End Using
Dim DSNRecuperado As String = recuperarLocalMachineODBC(RegistryView.Registry64, nomeDSN)
If DSNRecuperado <> "" Then
Return DSNRecuperado
Else
Return recuperarLocalMachineODBC(RegistryView.Registry32, nomeDSN)
End If
Catch __unusedException1__ As Exception
Return String.Empty
End Try
End Function
Private Shared Function recuperarLocalMachineODBC(ByVal localRegistro As RegistryView, ByVal nomeDSN As String) As String
Using hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, localRegistro)
Using key = hklm.OpenSubKey("SOFTWARE\ODBC\ODBC.ini\" & nomeDSN)
If key IsNot Nothing Then
Dim o As Object = key.GetValue("Server")
If o IsNot Nothing Then
Return o.ToString()
End If
End If
End Using
End Using
Return String.Empty
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment