C# 从 ADO.NET 确定 SQL Server 的版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/949852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine version of SQL Server from ADO.NET
提问by MagicAndi
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# 控制台应用程序 (.NET 2.0) 的 SQL Server 版本(在这种特殊情况下是 2000、2005 或 2008)。任何人都可以提供任何指导吗?
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.NET 连接对象中确定 SQL Server 版本。
采纳答案by MagicAndi
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'sanswer:
下面的代码将执行相同的操作,这次使用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();
}
回答by thijs
回答by Henk Holterman
The Server version is also available as a (string) property on the Connection object and as a SqlVersion property on the ServerConnection.
服务器版本也可用作 Connection 对象上的(字符串)属性和 ServerConnection 上的 SqlVersion 属性。
And SQl2008 is version >= 10
并且 SQL2008 是版本 >= 10
回答by marc_s
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
马克
回答by NinethSense
SqlConnection con = new SqlConnection("Server=localhost;Database=test;user=admin;password=123456;");
con.Open();
Text = con.ServerVersion;
con.Close();
con.ServerVersionwill give you:
con.ServerVersion会给你:
- 9.x.x for SQL Server 2005
- 10.x.x for SQL Server 2008
- 9.xx 用于 SQL Server 2005
- 10.xx 用于 SQL Server 2008