Groups | Blog | Home
all groups > dotnet odbc.net > january 2004 >

dotnet odbc.net : How to get list of DSN's defined on client machine?



Robb Gilmore
1/9/2004 8:31:51 AM
Is there a function somewhere in the .NET framework that
will return me a list of the DSN's defined on the machine
where the app is running?

thanks
HussAb NO[at]SPAM online.microsoft.com
1/10/2004 2:10:44 AM
Hi Robb,

There is no built-in function that will accomplish this task. You can
write your own function according to the following:

- If the list of DSNs is a System DSN then it is stored in the registry.
You could use the following link in the .NET Framework SDK to help you read
the registry:

"Reading from and Writing to the Registry Using the Microsoft.Win32
Namespace"

- If you want a list of the User DSNs then you could read the ODBC.INI in
C:\WINNT folder.

- If you want a list of File DSNs then they are located at C:\Program
Files\Common Files\ODBC\Data Sources.

I hope this information helps.


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. © 2002 Microsoft Corporation. All rights
reserved

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
Paul Clement
1/12/2004 8:31:30 AM
[quoted text, click to view]

¤ Is there a function somewhere in the .NET framework that
¤ will return me a list of the DSN's defined on the machine
¤ where the app is running?

See if the following works for you:

Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv As Integer, ByVal
fDirection As Short, ByVal szDSN As String, ByVal cbDSNMax As Short, ByRef pcbDSN As Short, ByVal
szDescription As String, ByVal cbDescriptionMax As Short, ByRef pcbDescription As Short) As Short
Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (ByRef env As Integer) As Short
Const SQL_SUCCESS As Integer = 0
Const SQL_FETCH_NEXT As Integer = 1

Public Sub FetchDSNs()

Dim ReturnValue As Short
Dim DSNName As String
Dim DriverName As String
Dim DSNNameLen As Short
Dim DriverNameLen As Short
Dim SQLEnv As Integer 'handle to the environment


If SQLAllocEnv(SQLEnv) <> -1 Then
Do Until ReturnValue <> SQL_SUCCESS
DSNName = Space(1024)
DriverName = Space(1024)
ReturnValue = SQLDataSources(SQLEnv, SQL_FETCH_NEXT, DSNName, 1024, DSNNameLen,
DriverName, 1024, DriverNameLen)
DSNName = Left(DSNName, DSNNameLen)
DriverName = Left(DriverName, DriverNameLen)

If DSNName <> Space(DSNNameLen) Then
System.Diagnostics.Debug.WriteLine(DSNName)
System.Diagnostics.Debug.WriteLine(DriverName)
End If
Loop
End If

End Sub


Paul ~~~ pclement@ameritech.net
AddThis Social Bookmark Button