上面代码翻译成vb.net(未经测试)
<DllImport("odbc32.dll")> _
Private Shared Function SQLAllocHandle(ByVal hType As Short, ByVal inputHandle As IntPtr, ByRef outputHandle As IntPtr) As Short
End Function
<DllImport("odbc32.dll")> _
Private Shared Function SQLSetEnvAttr(ByVal henv As IntPtr, ByVal attribute As Integer, ByVal valuePtr As IntPtr, ByVal strLength As Integer) As Short
End Function
<DllImport("odbc32.dll")> _
Private Shared Function SQLFreeHandle(ByVal hType As Short, ByVal handle As IntPtr) As Short
End Function
<DllImport("odbc32.dll"CharSet=CharSet.Ansi)> _
Private Shared Function SQLBrowseConnect(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 Function
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"
Private Const Oracle_DRIVER_STR As String = "DRIVER=Oracle"
Public Shared Function GetDataBaseServer(ByVal dbType As DataBaseType) As String()
Dim retval As String() = Nothing
Dim ar As ArrayList = New ArrayList
Select dbType
Case DataBaseType.SQLServer
Dim txt As String = String.Empty
Dim henv As IntPtr = IntPtr.Zero
Dim hconn As IntPtr = IntPtr.Zero
Dim inString As StringBuilder = New StringBuilder(SQL_DRIVER_STR)
Dim outString As StringBuilder = New StringBuilder(DEFAULT_RESULT_SIZE)
Dim inStringLength As Short = CType(inString.Length, Short)
Dim lenNeeded As Short = 0
Try
If SQL_SUCCESS = SQLAllocHandle(SQL_HANDLE_ENV, henv, henv) Then
If SQL_SUCCESS = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, CType(SQL_OV_ODBC3, IntPtr), 0) Then
If SQL_SUCCESS = SQLAllocHandle(SQL_HANDLE_DBC, henv, hconn) Then
If SQL_NEED_DATA = SQLBrowseConnect(hconn, inString, inStringLength, outString, DEFAULT_RESULT_SIZE, lenNeeded) Then
If DEFAULT_RESULT_SIZE < lenNeeded Then
outString.Capacity = lenNeeded
If Not (SQL_NEED_DATA = SQLBrowseConnect(hconn, inString, inStringLength, outString, lenNeeded, lenNeeded)) Then
Throw New ApplicationException("Unabled to aquire SQL Servers from ODBC driver.")
End If
End If
txt = outString.ToString
Dim start As Integer = txt.IndexOf("{") + 1
Dim len As Integer = txt.IndexOf("}") - start
If (start > 0) AndAlso (len > 0) Then
txt = txt.Substring(start, len)
Else
txt = String.Empty
End If
End If
End If
End If
End If
Catch
txt = String.Empty
Finally
If Not (hconn = IntPtr.Zero) Then
SQLFreeHandle(SQL_HANDLE_DBC, hconn)
End If
If Not (henv = IntPtr.Zero) Then
SQLFreeHandle(SQL_HANDLE_ENV, hconn)
End If
End Try
If txt.Length > 0 Then
retval = txt.Split(",".ToCharArray)
End If
' break
Case DataBaseType.Oracle
Dim machineKey As RegistryKey = Registry.LocalMachine
Dim serversKey As RegistryKey = machineKey.OpenSubKey("SOFTWARE\ORACLE\ALL_HOMES")
If Not (serversKey Is Nothing) AndAlso serversKey.SubKeyCount > 0 Then
Dim i As Integer = 0
While i < serversKey.SubKeyCount
Dim serverKey As RegistryKey = serversKey.OpenSubKey("ID" + i)
If Not (serverKey Is Nothing) AndAlso serverKey.ValueCount > 0 Then
Dim configFilePath As String = serverKey.GetValue("PATH") + "\network\admin\tnsnames.ora"
If File.Exists(configFilePath) Then
Dim stream As FileStream = File.OpenRead(configFilePath)
Dim byteContent(CType(stream.Length, Integer)) As Byte
stream.Read(byteContent, 0, byteContent.Length)
Dim strContent As StringBuilder = New StringBuilder(Encoding.Default.GetString(byteContent, 0, byteContent.Length))
Dim iPosition As Integer = 0
While iPosition <= strContent.Length
Dim iState As Integer = strContent.ToString.IndexOf("=" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " (DESCRIPTION", iPosition)
If iState > 0 Then
Dim iStartPosition As Integer = strContent.ToString.IndexOf("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "", iPosition) + 4
Dim iEndPosition As Integer = iState - 1
ar.Add(strContent.ToString.Substring(iStartPosition, iEndPosition - iStartPosition))
iPosition = iState
End If
System.Math.Min(System.Threading.Interlocked.Increment(iPosition),iPosition-1)
End While
stream.Close
End If
End If
System.Math.Min(System.Threading.Interlocked.Increment(i),i-1)
End While
End If
' break
Case DataBaseType.ODBC
Dim machineKey As RegistryKey = Registry.LocalMachine
Dim serversKey As RegistryKey
serversKey = machineKey.OpenSubKey("SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources")
ar.AddRange(serversKey.GetValueNames)
Dim userKey As RegistryKey = Registry.CurrentUser
serversKey = userKey.OpenSubKey("SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources")
ar.AddRange(serversKey.GetValueNames)
' break
End Select
Dim iValueCount As Integer = ar.Count
If iValueCount > 0 Then
retval = New String(iValueCount) {}
Dim i As Integer = 0
While i < iValueCount
retval(i) = CType(ar(i), String)
System.Math.Min(System.Threading.Interlocked.Increment(i),i-1)
End While
End If
Return retval
End Function