我如何使用System.Data.Sql.SqlDataSourceEnumerator类来了解可用的sql数据源……?

我如何使用System.Data.Sql.SqlDataSourceEnumerator类来了解可用的sql数据源……? 因为虽然我正在创建与sql server的连接,如果sql server还没有准备好,我们将得到exception…所以首先我想知道sql server是否已准备好接受请求…如何知道…

所以,根据以下参考:

http://social.msdn.microsoft.com/forums/en-US/sqlsmoanddmo/thread/49ba019f-e8b5-457c-80ea-fac5febb9d3d/

http://connect.microsoft.com/SQLServer/feedback/details/146323/enumavailablesqlservers-or-sqldatasourceenumerator-incorrect-list-of-available-databases

http://blogs.msdn.com/b/sushilc/archive/2004/10/14/242395.aspx

http://sqlblogcasts.com/blogs/jonsayce/archive/2008/02/10/programatically-listing-sql-servers.aspx

GetDataSources()不是一个完美的方法,这意味着它可能不会在第一次尝试时列出所有可用的数据源。 事实上,我发现它也没有列出你所有的本地资源。

出于我的目的,我在程序启动和我需要获取可用来源列表之间有一段时间,无论是在网络还是本地。 所以,我把代码放在一个永远收集所有源代码的线程中。 它在下面。 如果您取出while循环,您可以根据需要手动调用它。

private List sqlInstances = new List(); private void collectInstances() { while (true) { System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance; System.Data.DataTable dataTable = instance.GetDataSources(); foreach (DataRow row in dataTable.Rows) { string instanceName = String.Format(@"{0}\{1}", row["ServerName"].ToString(), row["InstanceName"].ToString()); //Do not add the local instance, we will add it in the next section. Otherwise, duplicated! if (!sqlInstances.Contains(instanceName) && !instanceName.Contains(Environment.MachineName)) { sqlInstances.Add(instanceName); } } /* * For some reason, GetDataSources() does not get local instances. So using code from here to get them * http://stackoverflow.com/questions/6824188/sqldatasourceenumerator-instance-getdatasources-does-not-locate-local-sql-serv */ List lclInstances = GetLocalSqlServerInstanceNames(); foreach (var lclInstance in lclInstances) { string instanceName = String.Format(@"{0}\{1}", Environment.MachineName, lclInstance); if (!sqlInstances.Contains(instanceName)) sqlInstances.Add(instanceName); } sqlInstances.Sort(); } } //Got code from: http://stackoverflow.com/questions/6824188/sqldatasourceenumerator-instance-getdatasources-does-not-locate-local-sql-serv ///  /// get local sql server instance names from registry, search both WOW64 and WOW3264 hives ///  /// a list of local sql server instance names public static List GetLocalSqlServerInstanceNames() { RegistryValueDataReader registryValueDataReader = new RegistryValueDataReader(); string[] instances64Bit = registryValueDataReader.ReadRegistryValueData(RegistryHive.Wow64, Registry.LocalMachine, @"SOFTWARE\Microsoft\Microsoft SQL Server", "InstalledInstances"); string[] instances32Bit = registryValueDataReader.ReadRegistryValueData(RegistryHive.Wow6432, Registry.LocalMachine, @"SOFTWARE\Microsoft\Microsoft SQL Server", "InstalledInstances"); //FormatLocalSqlInstanceNames(ref instances64Bit); //FormatLocalSqlInstanceNames(ref instances32Bit); List localInstanceNames = new List(instances64Bit); foreach (var item in instances32Bit) { if (!localInstanceNames.Contains(item)) localInstanceNames.Add(item); } //localInstanceNames = localInstanceNames.Union(instances32Bit).ToList(); return localInstanceNames; } 

GetDataSources()可以帮到你,你试过吗?

SqlDataSourceEnumerator.GetDataSources方法