SqlGeography 类型不匹配 [英] SqlGeography Type Mismatch

查看:38
本文介绍了SqlGeography 类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写内部 API 时遇到以下错误.我想要做的是以下列方式读取 SqlGeography 值(SQL Server 2012):

I have encountered the following error while writing an internal API. What I am trying to do is read a SqlGeography value (SQL Server 2012) in the following fashion:

field: public SqlGeography XY { get; set; }

object dbValue = reader["xy"];
PropertyInfo info = type.GetProperty(columnName:"xy");
info.SetValue(entity, dbValue);

虽然这看起来很奇怪,但我以这种方式阅读的原因是因为它是我编写的 Wrapper 的一部分,我们用它来加速 sql 读写.它接受一个匿名对象,并根据属性名称或属性名称将所有 sql 值读入其中.

Now while this might look strange, the reason I am reading in this fashion is because it is part of a Wrapper that I wrote which we use to speed up sql read and write. It takes an anonymous object and reads all the sql values into it based either on the property names or the attribute names.

这适用于除 SqlGeography 之外的所有内容.我做了一个类型比较,它也失败了,所以我什至无法检查该列是否属于 SqlGeography 类型,因为它总是无法与 Microsoft 进行比较.SqlServer.Types.SqlGeography.

This works fine for everything except SqlGeography. I did a type comparison and it fails as well so as hacky as it is I can not even do a check to see if the column is of type SqlGeography as it always fails the comparison to Microsoft.SqlServer.Types.SqlGeography.

初始异常如下:

'Microsoft.SqlServer.Types.SqlGeography' 类型的对象无法转换为 type 'Microsoft.SqlServer.Types.SqlGeography'.

Object of type 'Microsoft.SqlServer.Types.SqlGeography' cannot be converted to t ype 'Microsoft.SqlServer.Types.SqlGeography'.

如果有任何不清楚的地方,请开火,我会尽量详细说明.

If anything is unclear then please fire away and I will try to elaborate.

谢谢!

  • 接受的解决方案:安装包 Microsoft.SqlServer.Types -Version 10.50.1600.1

推荐答案

Types 程序集的版本可能不匹配.这是版本 10 和 11 之间的已知问题.不幸的是,错误消息不包含版本信息,这就是为什么它看起来像废话!

You could have a version mismatch on the Types assembly. This was a known issue between versions 10 and 11. Unfortunately the error message doesn't include version information, which is why it looks like nonsense!

为了解决这个问题,您可以反序列化类型的二进制表示,即像这样(如果您的 geography 列是结果集中的第一个列):

To get around it, you can deserialize the type's binary representation, i.e. something like this (if your geography column is the first in the result set):

var geo = SqlGeography.Deserialize(reader.GetSqlBytes(0));

还有其他解决方法,包括为程序集执行绑定重定向.

There are other workarounds, include doing a binding redirect for the assembly.

更多信息:https://connect.microsoft.com/SQLServer/feedback/details/685654/invalidcastexception-retrieving-sqlgeography-column-in-ado-net-data-reader

这篇关于SqlGeography 类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆