c00*_*0fd 0 .net c# sql-server
比方说,如果我拥有的只是从这个枚举获得的服务器名称:
//Collect server names
List<string> arrServerNames = new List<string>();
try
{
// Perform the enumeration
DataTable dataTable = null;
try
{
dataTable = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();
}
catch
{
dataTable = new DataTable();
dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
}
// Create the object array of server names (with instances appended)
for (int i = 0; i < dataTable.Rows.Count; i++)
{
string name = dataTable.Rows[i]["ServerName"].ToString();
string instance = dataTable.Rows[i]["InstanceName"].ToString();
if (instance.Length == 0)
{
arrServerNames.Add(name);
}
else
{
arrServerNames.Add(name + "\\" + instance);
}
}
}
catch
{
//Error
}
Run Code Online (Sandbox Code Playgroud)
我如何知道该服务器上安装的SQL Server版本?
检查官方MSDN文档GetDataSources()很容易就会发现Version结果集中有一列:
// Create the object array of server names (with instances appended)
for (int i = 0; i < dataTable.Rows.Count; i++)
{
string name = dataTable.Rows[i]["ServerName"].ToString();
string instance = dataTable.Rows[i]["InstanceName"].ToString();
string version = dataTable.Rows[i]["Version"].ToString(); // this gets the version!
..........
}
Run Code Online (Sandbox Code Playgroud)