浏览 SQL Server
我正在编写一个连接到 SQL Server 的数据库应用程序。 我想实现一个类似于 SQL Management Studio 中的连接对话框。 我已经找到了一种获取服务器上数据库列表的方法,但我真的很想获取网络上可用服务器的列表,这样最终用户就不必输入服务器的名称/IP 。
I'm writing a database application that connects to SQL server. I'd like to implement a similar connection dialog box like the one in SQL Management Studio. I've already found a way to get the list of databases on a server, but I'd really like to get the list of available servers on the network so end users won't have to type in the name/IP of the server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您正在.Net 中开发程序,则可以使用 SMO 对象来执行此操作。 使用 Microsoft.SqlServer.Management.Smo.SmoApplication.EnumAvailableSqlServers 方法获取本地网络中运行的所有 SQL Server 的列表。
扫描需要几秒钟,但不是很慢。
If you are developing your program in .Net you can use the SMO objects to do this. Use the Microsoft.SqlServer.Management.Smo.SmoApplication.EnumAvailableSqlServers method to get a list of all sql servers running in your local network.
The scanning taKes a few seconds, but its not very slow.
来自 此处:
替代 SQL DMO 方法 这里
From here:
Alternative SQL DMO method here
不确定是否有正确的方法,但一种方法是尝试将端口 1433(默认的 mssqlserver 端口)连接到网络中的计算机。
Not sure if there is a proper way but one hack would be to try to connect to port 1433 (default mssqlserver port) to machines in your network.
来自 sqlsecurity.com 的 SQLPing 工具
旧版本下载包含可能有用的源代码。
实际上,您需要查询网络上每个 IP 地址的端口 1434 UDP。
然而,防火墙和策略会阻止这一点。
注意:1434 是枚举服务器上 SQL 实例的位置。
如果您仅使用默认实例,则端口 1433 即可。 除非它“隐藏”在端口 2433 上...
The tool SQLPing from sqlsecurity.com
The older version downloads contains source code that may be useful.
Effectively, you need to query port 1434 UDP on every IP address on your network.
However, firewalls and policies will block this.
Note: 1434 is where you enumerate SQL instances on a server.
If you only use default instances, then port 1433 will do. Unless it's "hidden" on port 2433...
如果可以的话,您最好的猜测是提前填充该列表。
扫描网络中的 SQL 服务器可能不是最好的主意(缓慢、不可靠)。
Your best guess would be to populate the list in advance, if you can.
Scanning the network for SQL servers is probably not the best idea (slow, unreliable).
首先可能是获取 MS 网络监视器< /a> 并查看当您单击 MSSMS 中的下拉菜单时它会执行什么操作。
我的猜测是(当您单击该下拉菜单时有延迟)是它轮询本地网络。
话虽如此,在我的办公室,它似乎比本地网络轮询更多,因为它获取位于公司网络内但位于其他子网上的服务器。
祝你好运
A start might be to get MS Network monitor and see what it does when you click the dropdown in MSSMS.
My guess is (with the lag when you click that dropdown) is that it polls the local network.
Having said this, in my office, it appears to poll more than the local netowork in that it gets servers that are within the corporate network but on other subnets.
Good luck
也许 SQLCMD /L 适合你。
Perhaps SQLCMD /L will work for you.