从 ADO.NET 确定 SQL Server 的版本

发布于 2024-07-23 01:03:28 字数 203 浏览 2 评论 0原文

我需要确定连接字符串连接 C# 控制台应用程序 (.NET 2.0) 的 SQL Server 版本(在本例中为 2000、2005 或 2008)。 任何人都可以提供任何指导吗?

谢谢,MagicAndi

更新

如果可能的话,我希望能够从 ADO.NET 连接对象确定 SQL Server 版本。

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?

Thanks, MagicAndi

Update

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

少女情怀诗 2024-07-30 01:03:28

此代码将确定正在使用的 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);

}

下面的代码将执行相同的操作,这次使用 第九感的回答:

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();
}

This code will determine the version of SQL Server database being used - 2000, 2005 or 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:

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();
}
画中仙 2024-07-30 01:03:28

从普通的 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'

马克

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

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

花之痕靓丽 2024-07-30 01:03:28
SqlConnection con = new SqlConnection("Server=localhost;Database=test;user=admin;password=123456;");
con.Open();
Text = con.ServerVersion;
con.Close();

con.ServerVersion 将为您提供:

  • SQL Server 2005 为 9.xx
  • SQL Server 2008 为 10.xx
SqlConnection con = new SqlConnection("Server=localhost;Database=test;user=admin;password=123456;");
con.Open();
Text = con.ServerVersion;
con.Close();

con.ServerVersion will give you:

  • 9.x.x for SQL Server 2005
  • 10.x.x for SQL Server 2008
诗酒趁年少 2024-07-30 01:03:28

服务器版本还可以作为 Connection 对象上的(字符串)属性和 ServerConnection 上的 SqlVersion 属性使用。

并且SQl2008是版本>=10

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

And SQl2008 is version >= 10

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文