c# - 为什么 SQL Server 实例不总是出现在枚举中?

标签 c# sql-server

我正在尝试让 SQL Server 实例在我的 PC 上运行。

SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
DataTable table = instance.GetDataSources();
foreach (DataRow row in table.Rows)
{
   SqlInstanceCollection.Items.Add(row["ServerName"].ToString() + "\\" + row["InstanceName"].ToString());
   SqlInstanceCollection.Text = SqlInstanceCollection.Items[0].ToString();
}

我面临的问题是,有时我会获得实例,而有时却没有,即使服务器正在运行也是如此。

获取正在运行的 SQL Server 的实例列表需要多长时间?

任何帮助将不胜感激。

最佳答案

来自 MSDN:

Due to the nature of the mechanism used by SqlDataSourceEnumerator to locate data sources on a network, the method will not always return a complete list of the available servers, and the list might not be the same on every call. If you plan to use this function to let users select a server from a list, make sure that you always also supply an option to type in a name that is not in the list, in case the server enumeration does not return all the available servers. In addition, this method may take a significant amount of time to execute, so be careful about calling it when performance is critical.

SqlDataSourceEnumerator.GetDataSources Method

正如他们的注释中所述,方法在设计上是不可靠的;)奇怪!

编辑:如果我必须解决类似的问题,我会尝试使用具有适当参数集的 Windows API NetServerEnum。这确实不是微不足道的,但应该更可靠。

关于c# - 为什么 SQL Server 实例不总是出现在枚举中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7366367/

相关文章:

c# - 如何将此循环写成一行?

c# - 如何将 MailMesage 保存到 .msg 文件?

sql - 按不使用光标分组

sql-server - 将数据库表从一个 SQL Server 数据库表更新到另一个?

c# - 如何将插入符号位置设置为 WPF 密码框中的特定索引

c# - 在C#中获取用户输入

sql-server - 在 MS SQL 下使用关联迁移时的问题

sql-server - 如何确定数据库中行的大小

c# - MVC Controller : Using LINQ to check for duplicate value already existing in table before Save?

sql - 在 SQL Server 中查找任意 3 行的列的最大总和