2011-10-29 57 views
3

在C#上工作,在我的一个应用程序中,我需要获取SQL Server名称。在网络中,我有SQL Server 2000,2005,2008。对于我的应用程序,我想获取服务器名称。如何获得没有连接字符串的SQL Server名称

private string strConnection = @"Data Source=xxx;Initial Catalog=xxx;Integrated Security=True"; 

SqlConnection objConnection = new SqlConnection(strConnection); 
objConnection.Open(); 
SqlCommand objCommand = new SqlCommand(“xxxxxxx”); 
objCommand.ExecuteNonQuery(); 
objConnection.Close(); 

如果我知道strConnection那么我可以使用上面的语法。但我不知道服务器名称。首先,我需要知道服务器名称。我需要帮助来获取服务器名称。

回答

5

有相当多的选择如何找到那些代码 - 看到:

还有很多方法可以找到你的SQL Server--这些选项是否适合你?

+0

+1 ADO.NET实用程序类非常好。我正在考虑nmaping,而不是。 :) – Icarus

0

您可以使用Use SmoApplication.EnumAvailableSqlServers方法来枚举SQL Server的可用实例的列表。

+0

Works - 但它需要SMO才能存在,这意味着a)要么使用SMO安装SQL Server客户端工具,要么b)分别安装SMO。 –

+0

是绝对正确的 –