C#/.NET通过SqlConnection的属性获取MS SQL Server的版本信息[SqlConnection.ServerVersion]

C#开发 2015-08-16 8121阅读 0评论 0收藏 收藏本文

在C#/.NET的项目开发中,我们需要通过代码来获取MS SQL Server的版本信息,应该怎么样来获取? 其实非常简单,不知道怎么获取是因为你可能对SqlConnection这个类还不熟悉。SqlConnection提供了一个名为ServerVersion的属性,通过ServerVersion这个属性,我们就可以非常方便地得到SQL Server的版本信息。当然,SqlConnection这个类还提供了其他一些属性,比如: Database:获取当前数据库或连接打开后要使用的数据库的名称。 ClientConnectionId:最近连接尝试连接的 ID,无论该尝试是成功还是失败。 DataSource:获取要连接的 SQL Server 实例的名称。 等等。 具体的C#/.NET通过SqlConnection的属性[SqlConnection.ServerVersion]获取MS SQL Server的版本信息代码如下:

private static void CreateSqlConnection(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
        Console.WriteLine("State: {0}", connection.State );
    }
}
注:版本采用 ##.##.#### 形式,其中前两位数字是主版本号,中间两位是次版本号,最后四位是发布版本号。该字符串采用主版本号.次版本号.内部版本号形式,其中主版本号和次版本号必须为两位数字,内部版本号必须为四位数字。 以下贴出SQL Server中的版本号,希望对你有用 SQL Server 2005版本(9.X) --   SQL 2005 SP2 Builds             SQL 2005 SP3 Builds     SQL 2005 SP4 Builds -- Build       Description        Build       Description    Build        Description -- 3042        SP2 RTM              4035        SP3 RTM -- 3161        SP2 CU1              4207        SP3 CU1 -- 3175        SP2 CU2              4211        SP3 CU2 -- 3186        SP2 CU3              4220        SP3 CU3 -- 3200        SP2 CU4              4226        SP3 CU4 -- 3215        SP2 CU5              4230          SP3 CU5 -- 3228           SP2 CU6              4266        SP3 CU6 -- 3239        SP2 CU7              4273          SP3 CU7 -- 3257        SP2 CU8              4285        SP3 CU8 -- 3282        SP2 CU9              4294          SP3 CU9 -- 3294           SP2 CU10              4305        SP3 CU10 -- 3301           SP2 CU11              4309          SP3 CU11  ---> 5000        SP4 RTM -- 3315        SP2 CU12           4311        SP3 CU12  ---> 5254       SP4 CU1 -- 3325           SP2 CU13              4315          SP3 CU13 -- 3328        SP2 CU14              4317          SP3 CU14    ---> 5259        SP4 CU2 -- 3330        SP2 CU15              4325          SP3 CU15    ---> 5266        SP4 CU3 -- 3355        SP2 CU16 -- 3356           SP2 CU17     SQL Server 2008版本(10.X) -- SQL Server 2008 RTM Builds   SQL Server 2008 SP1 Builds     SQL Server 2008 SP2 Builds        SQL Server 2008 SP3 Builds -- Build       Description      Build       Description           Build     Description            Build        Description -- 1600        Gold RTM -- 1763        RTM CU1 -- 1779        RTM CU2 -- 1787        RTM CU3    -->    2531        SP1 RTM -- 1798        RTM CU4    -->    2710        SP1 CU1 -- 1806        RTM CU5    -->    2714        SP1 CU2 -- 1812           RTM CU6    -->    2723        SP1 CU3 -- 1818        RTM CU7    -->    2734        SP1 CU4 -- 1823        RTM CU8    -->    2746        SP1 CU5 -- 1828           RTM CU9    -->    2757        SP1 CU6 -- 1835           RTM CU10   -->    2766        SP1 CU7 -- RTM Branch Retired     -->    2775        SP1 CU8        -->  4000       SP2 RTM --                                2789        SP1 CU9 --                                2799        SP1 CU10 --                                2804        SP1 CU11    -->  4266      SP2 CU1 --                                2808        SP1 CU12    -->  4272       SP2 CU2 --                                2816        SP1 CU13    -->  4279      SP2 CU3 --                                2821        SP1 CU14    -->  4285       SP2 CU4    -->                5500        SP3 RTM --                                2847        SP1 CU15    -->  4316       SP2 CU5 --                                2850        SP1 CU16    -->  4321       SP2 CU6    -->                5766        SP3 CU1 --                              SP1 Branch Retired      -->  4323      SP2 CU7  -->             5768        SP3 CU2 --                                                           4326       SP2 CU8  -->             5770        SP3 CU3 --                                                             4330       SP2 CU9  -->                5775        SP3 CU4 --                                                             4332       SP2 CU10 -->             5785        SP3 CU5 --                                                             4333      SP2 CU11 -->                5788        SP3 CU6          7/16/2012 --                                                             SP2 Branch Retired                    5794        SP3 CU7          9/17/2012 --                                                                                                5828        SP3 CU8          11/19/2012 --                                                                                                5829        SP3 CU9       1/21/2013 --                                                                                                5835        SP3 CU10      3/19/2013 --                                                                                              5841        SP3 CU11      5/20/2013 --                                                                                              5844        SP3 CU12      7/15/2013 --                                                                                              5846        SP3 CU13      9/16/2013     SQL Server 2008R2(10.5.X) -- SQL Server 2008 R2 Builds                SQL Server 2008 R2 SP1 Builds            SQL Server 2008 R2 SP2 Builds -- Build            Description                Build        Description                    Build        Description -- 10.50.1092        August 2009 CTP2 -- 10.50.1352        November 2009 CTP3 -- 10.50.1450        Release Candidate -- 10.50.1600        RTM -- 10.50.1702        RTM CU1 -- 10.50.1720        RTM CU2 -- 10.50.1734        RTM CU3 -- 10.50.1746        RTM CU4 -- 10.50.1753        RTM CU5 -- 10.50.1765        RTM CU6     --->            10.50.2500    SP1 RTM -- 10.50.1777        RTM CU7 -- 10.50.1797        RTM CU8     --->            10.50.2769  SP1 CU1 -- 10.50.1804       RTM CU9  --->            10.50.2772  SP1 CU2 -- 10.50.1807        RTM CU10 --->           10.50.2789  SP1 CU3 -- 10.50.1809       RTM CU11 --->            10.50.2796  SP1 CU4 -- 10.50.1810        RTM CU12 --->            10.50.2806    SP1 CU5        --->            10.50.4000    SP2 RTM -- 10.50.1815        RTM CU13 --->           10.50.2811  SP1 CU6 -- 10.50.1817        RTM CU14 --->            10.50.2817  SP1 CU7        --->            10.50.4260    SP2 CU1                     7/24/2012 -- RTM Branch Retired        --->            10.50.2822  SP1 CU8     --->            10.50.4263  SP2 CU2                  8/31/2012 --                                            10.50.2866  SP1 CU9     --->            10.50.4266  SP2 CU3                    10/15/2012 --                                          10.50.2868  SP1 CU10    --->            10.50.4270  SP2 CU4                    12/17/2012 --                                          10.50.2869  SP1 CU11    --->            10.50.4276  SP2 CU5                     2/18/2013 --                                          10.50.2874  SP1 CU12    --->            10.50.4279  SP2 CU6                  4/15/2013 --                                          10.50.2876  SP1 CU13    --->            10.50.4286  SP2 CU7                     6/17/2013 --                                          10.50.2881  SP1 CU14    --->            10.50.4290  SP2 CU8                  8/22/2013     SQL Server 2012(11.X) -- SQL Server 2012 RTM Branch Builds                          SQL Server 2012 SP1 Branch Builds -- Build            Description            Release Date           Build            Description        Release Date -- 11.0.2300        RTM -- 11.0.2316        RTM CU1                 4/12/2012 -- 11.0.2325        RTM CU2                 6/18/2012 -->        11.0.3000        SP1 RTM            11/7/2012 -- 11.0.2332        RTM CU3                 8/31/2012 -- 11.0.2376        RTM CU3 + QFE         10/9/2012 -- 11.0.2383        RTM CU4                10/15/2012 -->        11.0.3321        SP1 CU1            11/20/2012 -- 11.0.2395        RTM CU5                12/17/2012 -->      11.0.3339        SP1 CU2            1/21/2013 -- 11.0.2401        RTM CU6              2/18/2013 -->      11.0.3349       SP1 CU3            3/18/2013 -- 11.0.2405        RTM CU7              4/15/2013 -->      11.0 3368       SP1 CU4         5/30/2013 -- 11.0.2410        RTM CU8              6/17/2013 -->      11.0.3373       SP1 CU5         7/15/2013 -- 11.0.2419        RTM CU9              8/20/2013 -->      11.0.3381        SP1 CU6            9/16/2013     SQL Server 2014(12.X) -- Build            Description            Release Date -- 11.0.9120        CTP1                6/2/2013 -- 12.0.1524        CTP2                10/15/2013 SQL Server版本号资料来源:http://www.cnblogs.com/CareySon/p/3497422.html

转载请注明:图享网 » C#/.NET通过SqlConnection的属性获取MS SQL Server的版本信息[SqlConnection.ServerVersion]

文章评论

获取验证码