0

Say, if what I have is only a server name obtained from this enumeration:

//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
}

How can I know the SQL Server version installed on that server?

4

1 回答 1

5

Checking the official MSDN documentation for GetDataSources() would have easily revealed that there is a Version column in the result set:

// 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!

    ..........
}
于 2013-10-19T07:26:34.223 回答