微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何从DataReader中检索SqlGeography类型时解决InvalidCastException?

我正在使用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.dll

Additional 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,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以后才能访问几何和地理可用的一些方法.

以下链接解释了解决此问题的一些更好的方法,请参阅Breaking Changes to Database Engine Features in SQL Server 2012

您将在页面下方找到关于三分之一的相关信息,分为“sql CLR数据类型(几何,地理和等级ID)”.我选择的方法是在app.config重定向程序集 – 但如果您愿意/需要,可以使用其他程序.

请注意,sql Server 2014需要相同的解决方案,但与指定Microsoft.sqlServer.Types程序集的版本12不兼容,也不能将“sql Server 2014”指定为连接字符串的Type System Version参数 – 使用2012.

写作时间正确.

为迟到的答案道歉,希望它有所帮助.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐