高分求救!怎样搜索本地与局域网上的SQL Server服务器???

flashstar 2002-11-02 03:26:53
因为需要,我想能够搜索出位于本地、局域网甚至Internet(已知IP地址)上的SQL Server服务器名称。当选择一个服务器时,能够取出该服务器中的数据库。
绝对高分!不够再加。
...全文
96 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
GaoLun 2002-12-17
  • 打赏
  • 举报
回复
#ifndef UNICODE
#define UNICODE
#endif

#include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <lm.h>

int wmain(int argc, wchar_t *argv[])
{
LPSERVER_INFO_101 pBuf = NULL;
LPSERVER_INFO_101 pTmpBuf;
DWORD dwLevel = 101;
DWORD dwPrefMaxLen = -1;
DWORD dwEntriesRead = 0;
DWORD dwTotalEntries = 0;
DWORD dwTotalCount = 0;
DWORD dwServerType = SV_TYPE_SERVER; // all servers,据说改成SV_TYPE_SQLSERVER就可以了
DWORD dwResumeHandle = 0;
NET_API_STATUS nStatus;
LPTSTR pszServerName = NULL;
DWORD i;

if (argc > 2)
{
fwprintf(stderr, L"Usage: %s [\\\\ServerName]\n", argv[0]);
exit(1);
}
// The server is not the default local computer.
//
if (argc == 2)
pszServerName = argv[1];
//
// Call the NetServerEnum function to retrieve information
// for all servers, specifying information level 101.
//
nStatus = NetServerEnum(pszServerName,
dwLevel,
(LPBYTE *) &pBuf,
dwPrefMaxLen,
&dwEntriesRead,
&dwTotalEntries,
dwServerType,
NULL,
&dwResumeHandle);
//
// If the call succeeds,
//
if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
{
if ((pTmpBuf = pBuf) != NULL)
{
//
// Loop through the entries and
// print the data for all server types.
//
for (i = 0; i < dwEntriesRead; i++)
{
assert(pTmpBuf != NULL);

if (pTmpBuf == NULL)
{
fprintf(stderr, "An access violation has occurred\n");
break;
}

printf("\tPlatform: %d\n", pTmpBuf->sv101_platform_id);
wprintf(L"\tName: %s\n", pTmpBuf->sv101_name);
printf("\tVersion: %d.%d\n",
pTmpBuf->sv101_version_major,
pTmpBuf->sv101_version_minor);
printf("\tType: %d", pTmpBuf->sv101_type);
//
// Check to see if the server is a domain controller;
// if so, identify it as a PDC or a BDC.
//
if (pTmpBuf->sv101_type & SV_TYPE_DOMAIN_CTRL)
wprintf(L" (PDC)");
else if (pTmpBuf->sv101_type & SV_TYPE_DOMAIN_BAKCTRL)
wprintf(L" (BDC)");

printf("\n");
//
// Also print the comment associated with the server.
//
wprintf(L"\tComment: %s\n\n", pTmpBuf->sv101_comment);

pTmpBuf++;
dwTotalCount++;
}
// Display a warning if all available entries were
// not enumerated, print the number actually
// enumerated, and the total number available.

if (nStatus == ERROR_MORE_DATA)
{
fprintf(stderr, "\nMore entries available!!!\n");
fprintf(stderr, "Total entries: %d", dwTotalEntries);
}

printf("\nEntries enumerated: %d\n", dwTotalCount);
}
}
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
//
// Free the allocated buffer.
//
if (pBuf != NULL)
NetApiBufferFree(pBuf);

return 0;
}
梦回童年001 2002-11-23
  • 打赏
  • 举报
回复
mark
flashstar 2002-11-06
  • 打赏
  • 举报
回复
但关键是我要在连接数据库之前就要找出服务器和数据库啊。
CDSoftwareWj 2002-11-06
  • 打赏
  • 举报
回复
核心代码部分

Function TMainform.IsSQLServer(IP: String; var SQLName: String) : Boolean;
Var
Sock : TSocket;
SockAddr : SockAddr_In;
IP_Address_Array : Array[0..32] of Char; // Don't need more than 15 though... ;)
Error : Integer;
Timer : TDateTime;
HostEnt : PHostEnt;

Begin
Result := False;

Sock := Socket(PF_INET, SOCK_STREAM, 0); // Open a socket
IF (Sock <> INVALID_SOCKET) Then
Begin
Strpcopy(IP_Address_Array, IP);

SockAddr.Sin_Addr.S_addr := Inet_Addr(IP_Address_Array);
SockAddr.Sin_Port := HtoNS(1433); // Service: 'ms-sql-s' ???
SockAddr.Sin_Zero[0] := Char(0);
SockAddr.Sin_Family := AF_INET;
End;
WSAAsyncSelect(Sock, self.Handle, Socket_WM_Hook, FD_READ or FD_CONNECT or FD_CLOSE);


Error := Connect(Sock, TSockaddr(SockAddr), Sizeof(SockAddr));
IF (Error = SOCKET_ERROR) Then
Begin
IF (WSAGetLastError = WSAEWOULDBLOCK) Then Error := 0;
End
Else Error := 0;

IF (Error = 0) Then
Begin
ConnectionStatus := 0;

Timer := Now;
While (ConnectionStatus = 0) and (Timer+(0.01/86400) > Now) do Application.ProcessMessages;
Result := (ConnectionStatus = 1);


IF (Result) Then
Begin
HostEnt := GetHostByAddr(@SockAddr.sin_addr.S_addr, 4, PF_INET);
IF (Assigned(HostEnt)) Then
Begin
SQLName := HostEnt.h_name;
End
Else SQLName := IP;
End;
End;
CloseSocket(Sock);
End;
flashstar 2002-11-06
  • 打赏
  • 举报
回复
要啊。快来啊。
CDSoftwareWj 2002-11-06
  • 打赏
  • 举报
回复

其原理是用Socket检索MS SQL Server 1433 服务端口
CDSoftwareWj 2002-11-06
  • 打赏
  • 举报
回复
要不要??
CDSoftwareWj 2002-11-06
  • 打赏
  • 举报
回复
可以,我整理一下,不过我是用Delphi写的,你自己转转看吧
invalid 2002-11-06
  • 打赏
  • 举报
回复
没有连接服务器就想获得数据库?实现不了
flashstar 2002-11-04
  • 打赏
  • 举报
回复
那具体一个服务器上的数据库呢?
oyxiaoyu0 2002-11-04
  • 打赏
  • 举报
回复
楼上的方法可以!
另一种方法也可以通过sp_databases这个系统存储过程来显示所有数据库的
invalid 2002-11-04
  • 打赏
  • 举报
回复
服务器上的数据库可以通过select系统表获得,有这方面的老帖子!搜索一下吧.
oyxiaoyu0 2002-11-03
  • 打赏
  • 举报
回复
这个SERVER列表这个可以在一个sql.INI文件中找到的!
这个文件在 SYBASE文件夹里的一个文件夹中!忘记了!
每个扇区都是它的服务器的!SYBASE会把所有连接的服务器写到这个INI文件中的!
你只要对扇区就可以了!
invalid 2002-11-02
  • 打赏
  • 举报
回复
在NowCan的网站有demo的.
http://vip.6to23.com/NowCan1/csdn/BCB_frame.htm

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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