鉴于代码和思想都是抄袭别人的, 这里只能算作是转载了
网上有很多获取SQL SERVER服务器列表的文章, 但事实上很多都是使用SQLDMO, 我自己试了一下, 因为本地装了SQL SERVER, 所以用的挺好, 结果部署下去后, 没有装SQL SERVER的机子全部不能使用这个功能
也就是说, 要么你装个SQL SERVER(我没有测试是否只要安装服务端就好), 要么就不能用SQLDMO, 说的难听点, 用SQLDMO就是自娱自乐了.
废话不多说, 直接上代码了
原文是C#的代码, 我把它转成了VB, 方便像我这样, 比较喜欢VB的同志....
Imports System.Runtime.InteropServices
Imports System.Text
Imports Microsoft.Win32
Imports System.IO
Imports System.Text
Imports Microsoft.Win32
Imports System.IO
Public Class GetServers
<DllImport("odbc32.dll")> _
Private Shared Function SQLAllocHandle(ByVal hType As Short, ByVal inputHandle As IntPtr, <Out()> ByRef outputHandle As IntPtr) As Short
End Function
<DllImport("odbc32.dll")> _
Private Shared Function SQLAllocHandle(ByVal hType As Short, ByVal inputHandle As IntPtr, <Out()> 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
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
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, <Out()> ByRef outLengthNeeded As Short) As Short
End Function
Private Shared Function SQLBrowseConnect(ByVal hconn As IntPtr, ByVal inString As StringBuilder, ByVal inStringLength As Short, ByVal outString As StringBuilder, ByVal outStringLength As Short, <Out()> 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 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"
Public Shared Function GetServers() As ArrayList
Dim list 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 = inString.Length
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 (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
list = outString.ToString()
Dim start As Integer = list.IndexOf("{") + 1
Dim len As Integer = list.IndexOf("}") - start
If ((start > 0) AndAlso (len > 0)) Then
list = list.Substring(start, len)
Else
list = String.Empty
End If
End If
End If
End If
End If
Catch ex As Exception
list = String.Empty
Finally
If (hconn <> IntPtr.Zero) Then
SQLFreeHandle(SQL_HANDLE_DBC, hconn)
End If
If (henv <> IntPtr.Zero) Then
SQLFreeHandle(SQL_HANDLE_ENV, hconn)
End If
End Try
Dim arr() As String = Nothing
Dim arrList As New ArrayList
Dim arrList As New ArrayList
'个人比较喜欢ArrayList, 所以硬是转了过来
If (list.Length > 0) Then
arr = list.Split(",")
For i As Byte = 1 To arr.Length
arrList.Add(arr(i - 1).ToString)
Next
arrList.Add(arr(i - 1).ToString)
Next
End If
Return arrList
End Function
End Class
由于编辑器的问题, 这里有些地方多了换行
转载于:https://blog.51cto.com/kaliking/24198