.NET 2.0: Abfrage verfügbarer ODBC Treiber

08/04/2010 - 14:08 von Stefan M. | Report spam
Hallo,

kann ich mit dem .NET Framework die installierten ODBC Treiber
auslesen um diese dann dynamisch im ConnectionString (Driver=) zu
verwenden?

Gruß
Stefan
 

Lesen sie die antworten

#1 SeriTools
08/04/2010 - 14:57 | Warnen spam
Hallo,

ja, das geht:

#Region "Konstanten und API-Funktionen für OdbcInstances"
Private Const SQL_HANDLE_ENV As Short = 1
Private Const SQL_HANDLE_DBC As Short = 2
Private Const SQL_ATTR_ODBC_VERSION As Integer = 200
Private Const SQL_OV_ODBC3 As Integer = 3
Private Const SQL_SUCCESS As Short = 0
Private Const SQL_NEED_DATA As Short = 99
Private Const DEFAULT_RESULT_SIZE As Short = 1024
Private Const SQL_DRIVER_STR As String = "DRIVER=SQL SERVER"

'ODBC-API-Funktionen
Private Declare Ansi Function SQLAllocHandle Lib "odbc32.dll" _
(ByVal hType As Short, ByVal inputHandle As IntPtr, ByRef outputHandle
As IntPtr) As Short
Private Declare Ansi Function SQLSetEnvAttr Lib "odbc32.dll" _
(ByVal henv As IntPtr, ByVal attribute As Integer, ByVal valuePtr As
IntPtr, _
ByVal strLength As Integer) As Short
Private Declare Ansi Function SQLFreeHandle Lib "odbc32.dll" _
(ByVal hType As Short, ByVal handle As IntPtr) As Short
Private Declare Ansi Function SQLBrowseConnect Lib "odbc32.dll" _
(ByVal hconn As IntPtr, ByVal inString As StringBuilder, _
ByVal inStringLength As Short, ByVal outString As StringBuilder, _
ByVal outStringLength As Short, ByRef outLengthNeeded As Short) As
Short
#End Region

'liefert String-Array mit den Namen aller gefundenen SQL-Server-Instanzen
Public Shared Function OdbcInstances() As String()
Dim result As String() = {}
Dim txt As String = String.Empty
Dim henv As IntPtr = IntPtr.Zero
Dim hconn As IntPtr = IntPtr.Zero
Dim inString As New StringBuilder(SQL_DRIVER_STR)
Dim outString As New StringBuilder(DEFAULT_RESULT_SIZE)
Dim inStringLength As Short = CShort(inString.Length)
Dim lenNeeded As Short = 0

Try
'Verbindung zur ODBC-Bibliothek herstellen
If SQLAllocHandle(SQL_HANDLE_ENV, henv, henv) = SQL_SUCCESS Then
If SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, CType(SQL_OV_ODBC3,
IntPtr), 0) = SQL_SUCCESS Then
If SQLAllocHandle(SQL_HANDLE_DBC, henv, hconn) = SQL_SUCCESS Then
'Liste der bekannten SQL Server ermitteln (--> outstring)
If SQLBrowseConnect(hconn, inString, inStringLength, outString,
DEFAULT_RESULT_SIZE, lenNeeded) = SQL_NEED_DATA Then
If lenNeeded > DEFAULT_RESULT_SIZE Then
outString.Capacity = lenNeeded
If SQLBrowseConnect(hconn, inString, inStringLength,
outString, lenNeeded, lenNeeded) <> SQL_NEED_DATA Then
MessageBox.Show("Die Liste der SQL Server kann nicht vom
ODBC-Treiber gelesen werden.")
End If
End If
'Server-Namen aus Zeichenkette extrahieren; befinden sich
zwischen { und }
txt = outString.ToString()
Dim start As Integer = txt.IndexOf("{") + 1
Dim len As Integer = txt.IndexOf("}") - start
txt = IIf(start > 0 And len > 0, txt.Substring(start, len),
String.Empty)
End If
End If
End If
End If

Catch ex As Exception
'Fehler
MessageBox.Show("Fehler beim Lesen der SQL-Server-Liste: " +
ex.Message)
txt = ""

Finally
'Aufràumarbeiten
If hconn <> IntPtr.Zero Then SQLFreeHandle(SQL_HANDLE_DBC, hconn)
If henv <> IntPtr.Zero Then SQLFreeHandle(SQL_HANDLE_ENV, hconn)
End Try

If txt.Length > 0 Then
' txt = txt.Replace("(local)", System.Environment.MachineName)
'aus Zeichenkette "a,b,c" ein Array mit den Elementen {"a", "b", "c"}
machen
result = txt.Split(",")
End If

Return result
End Function

lg SeriTools

"Stefan M." schrieb im Newsbeitrag
news:
Hallo,

kann ich mit dem .NET Framework die installierten ODBC Treiber
auslesen um diese dann dynamisch im ConnectionString (Driver=) zu
verwenden?

Gruß
Stefan

Ähnliche fragen