从ADO.NET确定SQL Server的版本

时间:2021-04-09 08:24:18

I need to determine the version of SQL Server (2000, 2005 or 2008 in this particular case) that a connection string connects a C# console application (.NET 2.0). Can anyone provide any guidance on this?

我需要确定连接字符串连接c#控制台应用程序的SQL Server版本(2000、2005或2008)。NET 2.0)。有人能对此提供指导吗?

Thanks, MagicAndi

谢谢,MagicAndi

Update

更新

I would like to be able to determine the SQL Server version form the ADO.NET connection object if possible.

我希望能够从ADO中确定SQL服务器版本。NET连接对象(如果可能的话)。

5 个解决方案

#1


16  

This code will determine the version of SQL Server database being used - 2000, 2005 or 2008:

本代码将确定所使用的SQL Server数据库的版本—2000、2005或2008:

try
{
    SqlConnection sqlConnection = new SqlConnection(connectionString);
    Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConnection));

    switch (server.Information.Version.Major)
    {
      case 8:
        MessageBox.Show("SQL Server 2000");
        break;
      case 9:
        MessageBox.Show("SQL Server 2005");
        break;
      case 10:
        MessageBox.Show("SQL Server 2008");
                break;
      default:
        MessageBox.Show(string.Format("SQL Server {0}", server.Information.Version.Major.ToString())); 
        break;   
    }
}
catch (Microsoft.SqlServer.Management.Common.ConnectionFailureException)
{
    MessageBox.Show("Unable to connect to server",
        "Invalid Server", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

The code below will do the same, this time using NinthSense's answer:

下面的代码也会这么做,这次使用NinthSense的回答:

try
{       
    SqlConnection sqlConnection = new SqlConnection(connectionString);
    sqlConnection.Open();

    string serverVersion = sqlConnection.ServerVersion;
    string[] serverVersionDetails = serverVersion.Split( new string[] {"."}, StringSplitOptions.None);

    int versionNumber = int.Parse(serverVersionDetails[0]);

    switch (versionNumber)
    {
        case 8:
            MessageBox.Show("SQL Server 2000");
            break;
        case 9:
            MessageBox.Show("SQL Server 2005");
            break;
        case 10:
            MessageBox.Show("SQL Server 2008");
            break;
        default:
            MessageBox.Show(string.Format("SQL Server {0}", versionNumber.ToString()));  
            break;  
    }
}
catch (Exception ex)
{
    MessageBox.Show(string.Format("Unable to connect to server due to exception: {1}", ex.Message),
        "Invalid Connection!", MessageBoxButtons.OK, MessageBoxIcon.Error);

}
finally
{
    sqlConnection.Close();
}

#2


11  

SqlConnection con = new SqlConnection("Server=localhost;Database=test;user=admin;password=123456;");
con.Open();
Text = con.ServerVersion;
con.Close();

con.ServerVersion will give you:

con.ServerVersion会给你:

  • 9.x.x for SQL Server 2005
  • 9. x。SQL Server 2005的x。
  • 10.x.x for SQL Server 2008
  • 10. x。x表示SQL Server 2008

#3


6  

Run this script from a normal SqlCommand - it's quite extensive and useful!

从一个普通的SqlCommand运行这个脚本——它非常广泛和有用!

SELECT  
    SERVERPROPERTY('productversion') as 'Product Version', 
    SERVERPROPERTY('productlevel') as 'Patch Level',  
    SERVERPROPERTY('edition') as 'Product Edition',
    SERVERPROPERTY('buildclrversion') as 'CLR Version',
    SERVERPROPERTY('collation') as 'Default Collation',
    SERVERPROPERTY('instancename') as 'Instance',
    SERVERPROPERTY('lcid') as 'LCID',
    SERVERPROPERTY('servername') as 'Server Name'

Marc

马克

#4


5  

Try

试一试

Select @@version

http://msdn.microsoft.com/en-us/library/ms177512(SQL.90).aspx

http://msdn.microsoft.com/en-us/library/ms177512(SQL.90). aspx

#5


2  

The Server version is also available as a (string) property on the Connection object and as a SqlVersion property on the ServerConnection.

服务器版本也可以作为连接对象上的(string)属性和服务器连接上的SqlVersion属性使用。

And SQl2008 is version >= 10

SQl2008版本是>= 10

#1


16  

This code will determine the version of SQL Server database being used - 2000, 2005 or 2008:

本代码将确定所使用的SQL Server数据库的版本—2000、2005或2008:

try
{
    SqlConnection sqlConnection = new SqlConnection(connectionString);
    Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConnection));

    switch (server.Information.Version.Major)
    {
      case 8:
        MessageBox.Show("SQL Server 2000");
        break;
      case 9:
        MessageBox.Show("SQL Server 2005");
        break;
      case 10:
        MessageBox.Show("SQL Server 2008");
                break;
      default:
        MessageBox.Show(string.Format("SQL Server {0}", server.Information.Version.Major.ToString())); 
        break;   
    }
}
catch (Microsoft.SqlServer.Management.Common.ConnectionFailureException)
{
    MessageBox.Show("Unable to connect to server",
        "Invalid Server", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

The code below will do the same, this time using NinthSense's answer:

下面的代码也会这么做,这次使用NinthSense的回答:

try
{       
    SqlConnection sqlConnection = new SqlConnection(connectionString);
    sqlConnection.Open();

    string serverVersion = sqlConnection.ServerVersion;
    string[] serverVersionDetails = serverVersion.Split( new string[] {"."}, StringSplitOptions.None);

    int versionNumber = int.Parse(serverVersionDetails[0]);

    switch (versionNumber)
    {
        case 8:
            MessageBox.Show("SQL Server 2000");
            break;
        case 9:
            MessageBox.Show("SQL Server 2005");
            break;
        case 10:
            MessageBox.Show("SQL Server 2008");
            break;
        default:
            MessageBox.Show(string.Format("SQL Server {0}", versionNumber.ToString()));  
            break;  
    }
}
catch (Exception ex)
{
    MessageBox.Show(string.Format("Unable to connect to server due to exception: {1}", ex.Message),
        "Invalid Connection!", MessageBoxButtons.OK, MessageBoxIcon.Error);

}
finally
{
    sqlConnection.Close();
}

#2


11  

SqlConnection con = new SqlConnection("Server=localhost;Database=test;user=admin;password=123456;");
con.Open();
Text = con.ServerVersion;
con.Close();

con.ServerVersion will give you:

con.ServerVersion会给你:

  • 9.x.x for SQL Server 2005
  • 9. x。SQL Server 2005的x。
  • 10.x.x for SQL Server 2008
  • 10. x。x表示SQL Server 2008

#3


6  

Run this script from a normal SqlCommand - it's quite extensive and useful!

从一个普通的SqlCommand运行这个脚本——它非常广泛和有用!

SELECT  
    SERVERPROPERTY('productversion') as 'Product Version', 
    SERVERPROPERTY('productlevel') as 'Patch Level',  
    SERVERPROPERTY('edition') as 'Product Edition',
    SERVERPROPERTY('buildclrversion') as 'CLR Version',
    SERVERPROPERTY('collation') as 'Default Collation',
    SERVERPROPERTY('instancename') as 'Instance',
    SERVERPROPERTY('lcid') as 'LCID',
    SERVERPROPERTY('servername') as 'Server Name'

Marc

马克

#4


5  

Try

试一试

Select @@version

http://msdn.microsoft.com/en-us/library/ms177512(SQL.90).aspx

http://msdn.microsoft.com/en-us/library/ms177512(SQL.90). aspx

#5


2  

The Server version is also available as a (string) property on the Connection object and as a SqlVersion property on the ServerConnection.

服务器版本也可以作为连接对象上的(string)属性和服务器连接上的SqlVersion属性使用。

And SQl2008 is version >= 10

SQl2008版本是>= 10