SQL Server 网络实例探测器






4.86/5 (21投票s)
2007年11月7日
2分钟阅读

88640

4384
一个简单易用的 C# 初学者控件,用于检测网络中的所有 SQL Server 实例,然后列出所选服务器上托管的所有数据库。
这是什么?

控件在示例应用程序中使用

您需要提供用户名和密码才能检索所有托管数据库。注意:SQL_Detect_dll.zip 是包含 SQLServersDetector
控件的程序集。SQL_Detect_source.zip 包含控件的源代码,以及使用该控件的示例应用程序。
引言
这是一个简单的窗口控件,可以加载本地网络中的所有 SQL Server 实例,然后加载所选服务器上的所有数据库。我使用了 SqlDataSourceEnumerator
来从网络加载服务器。
背景
我正在编写 C# 代码,涉及使用多个数据库的数据操作。我发现用户在文本输入字段中键入服务器和数据库地址,然后连接到它们进行进一步操作时比较困难,因为服务器名称或路径总是可能被拼写错误。如果用户错误地输入了服务器或数据库名称的错误拼写,然后单击连接,则需要很长时间才能确定路径无效并抛出异常。然后用户必须再次检查拼写并重新输入,我认为这总是很烦人的。因此,我决定自动检测服务器实例并从服务器加载数据库等等...
使用控件
该控件易于使用,就像 .NET 中的任何其他 Windows 控件一样。您只需添加提供的 DLL 的引用即可。或者,将项目添加到您的解决方案,并添加对项目 SqlServersDetector
的引用。然后,该控件将出现在您的工具箱中,就像这样。

现在您可以简单地将其拖放到您的窗体上。
控件属性
如果您需要,可以使用以下控件属性。
//Property to get the connection string that is created
//by using selected server, selected database, username and password.
lblConnectionString.Text = sqlServersDetector1.DBConnectionString;
//Property to get or set the text that is displayed on
//upper left corner of the control
sqlServersDetector1.MainLabel = "All Servers";
//Property to get or set the text that of the label
//against Servers combo box.
sqlServersDetector1.ServersLabel = "Servers";
//Property to get or set the text that of the label
//against Databases combo box.
sqlServersDetector1.DatabasesLabel = "Database";
//Property to get the selected server instance.
string server = sqlServersDetector1.SelectedServer;
//Property to get the selected database.
string database = sqlServersDetector1.SelectedDatabase;
使用文章提供的示例解决方案
如果任何初学者不知道如何使用该解决方案,只需下载 ZIP 文件 SQL_Detect_source.zip 并解压缩即可。然后打开 SQLServerDetectionControl.sln 解决方案文件并运行名为 TestAppForSQLServerControl
的项目。如果您仍然在使用此控件时遇到问题,请随时提问。
历史
- 2007 年 11 月 7 日 -- 发布原始版本