Microsoft.SqlServer.Types的几何数据类型在SQL Server 2012版本中是否向后兼容SQL Server 2008?

12
如果我本地同时安装了SQL Server 2008和SQL Server 2012,我会自己尝试一下;但是我只安装了更新版本,并希望保持这种状态。
  • SQL Server 2008附带一个名为Microsoft.SqlServer.Types.dll的程序集,主要版本为10。
  • SQL Server 2012附带一个名为Microsoft.SqlServer.Types.dll的程序集,主要版本为11。
除其他外,两个程序集都公开了一个SqlGeometryBuilder类型。两个程序集版本之间唯一的显著区别是2012类型有一个额外的重载方法AddCircularArc,而2008类型没有。 由于同时引用这两个程序集并不是很容易(也许是个坏主意),因此我想知道是否可以使用2012版本,即使针对SQL Server 2008实例,只要我不使用AddCircularArc就行。 是否有人可以分享他们的经验,看他们是否尝试过这样做?

1
MSDN页面《SQL Server 2012数据库引擎功能变更》中的部分_"SQL CLR数据类型(几何、地理和层次结构)"_并未涉及此问题。 - stakx - no longer contributing
2个回答

22
默认情况下,SqlClient使用Microsoft.SqlServer.Types程序集的版本10.0(即使您在项目中引用了更新的版本)。当同时加载该程序集的两个不同版本时,您可能会看到奇怪的运行时异常,如“System.InvalidCastException:无法将类型为'Microsoft.SqlServer.Types.SqlGeometry'的对象强制转换为类型'Microsoft.SqlServer.Types.SqlGeometry'”。...
以下文章描述了一些使用新的Microsoft.SqlServer.Types程序集与SqlClient的可能性: SQL Server 2012中数据库引擎功能的破坏性更改 选项包括:
  • 调用GetSqlBytes方法,而不是Get方法(例如,SqlGeometry.Deserialize(reader.GetSqlBytes(0)))
  • 在应用程序配置中使用程序集重定向
  • 将“Type System Version”属性的值指定为“SQL Server 2012”,以强制SqlClient加载程序集的11.0版本
我个人更喜欢“Type System Version”连接字符串关键字。请参见MSDN文章: SqlConnection.ConnectionString Property,并搜索“Type System Version”。

2
在“类型系统版本”属性上+1,以解决在两个版本的SQL Server之间进行转换时出现的问题。 - Brent Keller
你的回答中缺少两个事实(我在此期间学到的),即版本11向后兼容版本10,但版本10仅部分向前兼容版本11。尽管如此,我仍然接受你的答案,因为重要的是要意识到必须进行一些明确的工作才能加载版本11。 - stakx - no longer contributing
还有可能您的计算机上没有安装版本11。在这种情况下,您还需要下载2012 SQL功能包:https://www.microsoft.com/en-us/download/details.aspx?id=29065 - Byte11
如果您没有安装v11(或更高版本),请参见评论以获取其他选项。 - user3085342

7

我已经尝试使用SQL Server 2012的Microsoft.SqlServer.Types.dll来连接SQL Server 2008 Express。

  • Geometries can be INSERT ed as long as they don't contain circular strings; if they do contain circular strings, which SQL Server 2008 does not support, this exception gets thrown:

    System.Data.SqlClient.SqlException: The incoming tabular data stream (TDS) protocol stream is incorrect. Parameter 1 (@geometry): The supplied value is not a valid instance of data type geometry. Check the source data for invalid values.

  • Geometries can be SELECT ed, but apparently only via Well-Known Text (WKT):

    // SELECT [Geometry].STAsText() FROM …
    var geometry = SqlGeometry.STGeomFromText(sqlDataReader.GetSqlChars(…), …);
    

    If one attempts to read the geometry directly:

    // SELECT [Geometry] FROM …
    var geometry = (SqlGeometry)sqlDataReader[…];
    

    then the following exception gets thrown (even if no circular strings are present in the geometries):

    System.InvalidCastException: [A]Microsoft.SqlServer.Types.SqlGeometry cannot be cast to [B]Microsoft.SqlServer.Types.SqlGeometry.

    • Type A originates from Microsoft.SqlServer.Types, Version=10.
    • Type B originates from Microsoft.SqlServer.Types, Version=11.

    (That exception is not thrown when Microsoft.SqlServer.Types.dll version 10 is used.)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接