方法一:调用API函数法
摘自: http://www.pcbookcn.com/article/2360.htm
Private Declare Function NetServerEnum Lib "netapi32" _
(lpServer As Any, ByVal lLevel As Long, vBuffer As Any, _
lPreferedMaxLen As Long, lEntriesRead As Long, lTotalEntries As Long, _
ByVal lServerType As Long, ByVal sDomain$, vResume As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination _
As Any, Source As Any, ByVal Length As Long)
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Private Type SV_100
platform As Long
name As Long
End Type
Private Sub FillServerList()
Dim sv100 As SV_100, nRet As Long, i As Long
Dim lServerInfo As Long, lPreferedMaxLen As Long, lEntriesRead As Long
Dim lTotalEntries As Long, sDomain As String, vResume As Variant
Dim buffer() As Byte, nLen As Long
lPreferedMaxLen = 65536
nRet = NetServerEnum(0, 101, lServerInfo, lPreferedMaxLen, lEntriesRead, lTotalEntries, 4, sDomain, vResume)
If nRet = 0 Or nRet = 234& Then
For i = 0 To lEntriesRead - 1
CopyMemory sv100, ByVal lServerInfo, Len(sv100)
nLen = lstrlenW(sv100.name) * 2
If nLen Then
ReDim buffer(0 To (nLen - 1)) As Byte
CopyMemory buffer(0), ByVal sv100.name, nLen
End If
'将服务器名加入列表控件中
cboServer.AddItem buffer '服务器名
lServerInfo = lServerInfo + 24
Next i
End If
End Sub
'--------------------------------------------------------------------------------------------------------------------
方法二:使用SQLDMO的Application对象
摘自Microsoft自带的SQLDMO示例
Private Sub FillServerList()
Dim i As Integer
'Use the SQL DMO object to find available SQL Servers
Dim oSQLServerDMOApp As SQLDMO.Application
'Use the SQL DMO Application Object to find the available SQL Servers
Set oSQLServerDMOApp = New SQLDMO.Application
Dim namX As NameList
Set namX = oSQLServerDMOApp.ListAvailableSQLServers
For i = 1 To namX.Count
cboServer.AddItem namX.Item(i)
Next
If cboServer.ListCount > 0 Then
'Show top server
cboServer.ListIndex = 0
End If
oSQLServerDMOApp.Quit
Set oSQLServerDMOApp = Nothing
End Sub