我需要确定SQL Server的版本(2000年,2005年或2008年在这种特殊情况下),一个连接字符串连接一个C#控制台应用程序(.NET 2.0)。任何人都可以提供任何指导吗?
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?
谢谢,MagicAndi
Thanks, MagicAndi
更新
我希望能够确定SQL Server版本构成了ADO.NET连接对象,如果可能的。
I would like to be able to determine the SQL Server version form the ADO.NET connection object if possible.
这code将确定SQL Server数据库正在使用的版本 - 2000年,2005年或2008年:
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);
}
在code以下将做同样的,使用 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();
}
上一篇:好还是不好的经验CryptoLicensing?不好、经验、CryptoLicensing
下一篇:IDisposable的GC.Sup pressFinalize(本)的位置位置、GC、IDisposable、pressFinalize