我正在使用Visual Studio 2013和SQL Server 2012在C#中开发.我已经能够使用T-SQL在地理列中存储多边形,并且我没有尝试在代码中使用SqlGeography类来检索数据.
当我尝试:
SqlGeography polyB = (SqlGeography)dr["extent"]; // stored in OGC Well Known Binary format
从数据库中检索多边形我收到的消息:
An unhandled exception of type ‘System.InvalidCastException’ occurred
in GeoLib.dllAdditional information: [A]Microsoft.SqlServer.Types.SqlGeography
cannot be cast to [B]Microsoft.SqlServer.Types.SqlGeography. Type A originates from ‘Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91’ in the context ‘Default’ at location ‘C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Types\10.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll’.Type B originates from ‘Microsoft.SqlServer.Types, Version=11.0.0.0,
Culture=neutral, PublicKeyToken=89845dcd8080cc91’ in the context ‘Default’ at location ‘C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Types\11.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll’.我怀疑SQL Server和Visual Studio之间的版本不兼容,如不同的版本号所示.有没有人碰到这个?也许我需要安装SQL Server 2014?任何想法都表示赞赏!
虽然你自己的答案清楚地解决了你的问题(这很好),但通过做你所拥有的,你已经强迫自己只能在SQL 2012以后才能访问几何和地理可用的一些方法.您将在页面下方找到关于三分之一的相关信息,分为“SQL CLR数据类型(几何,地理和等级ID)”.我选择的方法是在app.config中重定向程序集 – 但如果您愿意/需要,可以使用其他程序.
请注意,SQL Server 2014需要相同的解决方案,但与指定Microsoft.SqlServer.Types程序集的版本12不兼容,也不能将“Sql Server 2014”指定为连接字符串的Type System Version参数 – 使用2012.
写作时间正确.
为迟到的答案道歉,希望它有所帮助.
查看更多关于c# – 如何从DataReader中检索SqlGeography类型时解决InvalidCastE的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did69301