如何用API得到局域网中的SERVER服务器

charmw 2004-03-27 11:20:41
主要是找SQL SERVER
谢谢
...全文
35 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
charmw 2004-03-28
  • 打赏
  • 举报
回复
我要这个局域网内所有的SQL服务器名(不是某个工作组也不是域) 下面引号内的该怎么填??
domain = "dltech" & vbNullChar
华芸智森 2004-03-27
  • 打赏
  • 举报
回复
再来一段.

列举SQL Server

Option Explicit

Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Private Declare Function NetServerEnum Lib "netapi32" (strServername As Any, ByVal level As Long, bufptr As Long, ByVal prefmaxlen As Long, entriesread As Long, totalentries As Long, ByVal servertype As Long, strDomain As Any, resumehandle As Long) As Long
Private Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal lpBuffer As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Const SV_TYPE_SERVER As Long = &H2
Private Const SV_TYPE_SQLSERVER As Long = &H4
Private Type SV_100
platform As Long
name As Long
End Type

Public Sub GetSQLServers()
Dim l As Long
Dim entriesread As Long
Dim totalentries As Long
Dim hREsume As Long
Dim bufptr As Long
Dim level As Long
Dim prefmaxlen As Long
Dim lType As Long
Dim domain() As Byte
Dim i As Long
Dim sv100 As SV_100

level = 100
prefmaxlen = -1
lType = SV_TYPE_SQLSERVER
domain = "dltech" & vbNullChar
l = NetServerEnum(ByVal 0&, level, bufptr, prefmaxlen, entriesread, totalentries, lType, domain(0), hREsume)
If l = 0 Or l = 234& Then
For i = 0 To entriesread - 1
CopyMemory sv100, ByVal bufptr, Len(sv100)
Debug.Print Pointer2stringw(sv100.name)
bufptr = bufptr + Len(sv100)
Next i
End If
NetApiBufferFree bufptr
End Sub

Private Function Pointer2stringw(ByVal l As Long) As String
Dim buffer() As Byte
Dim nLen As Long
nLen = lstrlenW(l) * 2
If nLen Then
ReDim buffer(0 To (nLen - 1)) As Byte
CopyMemory buffer(0), ByVal l, nLen
Pointer2stringw = buffer
End If
End Function

Private Sub Form_Load()
GetSQLServers
End Sub
华芸智森 2004-03-27
  • 打赏
  • 举报
回复

一段别人的程序...

安装 MS SQL Server server/client
引用 Microsoft SQLDMO Object Library

'CODE
Option Explicit

Private Sub Command1_Click()
On Error GoTo Err1
Dim dmoObj As New SQLDMO.Application
Dim I As Long

For I = 1 To dmoObj.ListAvailableSQLServers.Count
List1.AddItem dmoObj.ListAvailableSQLServers.Item(I)
Next
Err1:
End Sub


获取当前可以访问的sql server,引用sqldmo。

Dim Server As SQLDMO.NameList
Dim appDMO As New SQLDMO.Application
Dim i As Integer

On Error GoTo Error

Set Server = appDMO.ListAvailableSQLServers

For i = 1 To Server.Count
cmbServer.AddItem Server(i)
'Debug.Print Server(i)
Next

7,765

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧