Reputation: 5633
I have some VB code that connects to a DSN (data source) setup on this Windows machine. The code looks like:
Dim myConnection As OdbcConnection = New OdbcConnection()
myConnection.ConnectionString = "DSN=MYALERTS"
It still works & connects, however the user who set this up is no longer around. I need to view/edit this, but when I go into "ODBC Data Source Administrator" in Windows I see no DSNs listed. I assume this is because I'm under my own user. Even under "System DSN" tab, the list is blank.
Is there a Windows command (or even VB code) to view all DSNs on this Windows 7 machine?
Upvotes: 2
Views: 10388
Reputation: 41
I realize this is a little dated, but perhaps will help someone. When using the ODBC Manager on a 64 bit Windows OS, you will only see 64 ODBC connections. If the connection you are seeking was setup for 32 bit, you can browse down to \Windows\SysWOW64\odbcad32.exe applet and manage the legacy 32 bit DSNs. This is helpful when working with applications that are compiled to target 32 bit.
Upvotes: 4
Reputation: 3653
This prints user and system DSN (data source name) from Windows registry.
Module ModuleDsn
Public Enum DataSourceType
System
User
End Enum
Sub Main()
Dim SU As SortedList = GetDataSourceNames(DataSourceType.User)
Dim SS As SortedList = GetDataSourceNames(DataSourceType.System)
Dim count As Integer = SU.Count + SS.Count
Dim mKeys As [String]() = New String(count - 1) {}
SU.Keys.CopyTo(mKeys, 0)
SS.Keys.CopyTo(mKeys, SU.Keys.Count)
For i As Integer = 0 To mKeys.Length - 1
Console.WriteLine(mKeys(i))
Next
End Sub
Public Function GetDataSourceNames(ByVal dsnType As DataSourceType) As System.Collections.SortedList
Dim dsnList As New System.Collections.SortedList()
Dim reg As Microsoft.Win32.RegistryKey = Nothing
If dsnType = DataSourceType.User Then
reg = (Microsoft.Win32.Registry.CurrentUser).OpenSubKey("Software")
Else
reg = (Microsoft.Win32.Registry.LocalMachine).OpenSubKey("Software")
End If
If reg IsNot Nothing Then
reg = reg.OpenSubKey("ODBC")
If reg IsNot Nothing Then
reg = reg.OpenSubKey("ODBC.INI")
If reg IsNot Nothing Then
reg = reg.OpenSubKey("ODBC Data Sources")
If reg IsNot Nothing Then
For Each sName As String In reg.GetValueNames()
dsnList.Add(sName, DataSourceType.User)
Next
End If
Try
reg.Close()
Catch
End Try
End If
End If
End If
Return dsnList
End Function
End Module
Upvotes: 3
Reputation: 5633
I was able to view all DSNs in the registry:
HKEY_LOCAL_MACHINE->SOFTWARE->ODBC->ODBC.INI
Upvotes: 4