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

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

问题描述

在写一个内部API,我也遇到过下面的错误。我所试图做的是阅读下面的时尚 SqlGeography 值(SQL Server 2012中):

 字段:公共SqlGeography XY {搞定;组; } 

对象dbValue =读卡器[XY];
的PropertyInfo信息= type.GetProperty(COLUMNNAME:XY);
info.SetValue(实体,dbValue);

现在虽然这可能看起来很奇怪,我以这种方式阅读的原因是因为它的一部分的包装,我写的,我们用它来加快SQL读写。这需要一个匿名对象,并把它或者基于属性名称或属性名称读取所有的SQL值。



这工作正常的一切,除了 SqlGeography 。我做了一个类型比较和失败,以及使哈克,因为它是我甚至不能做一次检查,看是否该列的类型为 SqlGeography 因为它总是失败。比较 Microsoft.SqlServer.Types.SqlGeography



最初的例外是如下:




类型'Microsoft.SqlServer.Types.SqlGeography'的对象不能转换至T
YPE'Microsoft.SqlServer.Types.SqlGeography。




如果有不清楚的地方,那么请离火,我会尽量详细说明。



< !p>感谢




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


解决方案

您可能对类型的版本不匹配组件。这是版本10和11之间的一个已知的问题不幸的是,错误消息不包含版本信息,这就是为什么它看起来像废话!



要绕过它,你可以deseralize类型的二进制表示,即是这样的(如果你的地理列是结果集中的第一):

 变种地理位置= SqlGeography.Deserialize(reader.GetSqlBytes(0)); 

有其他解决方法,包括做一个绑定重定向程序集。



此处了解详情:的 https://connect.microsoft.com/SQLServer/feedback/details/685654/invalidcastexception-retrieving-sqlgeography-column-in-ado-net-data-reader


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);

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.

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.

The initial exception is as follows:

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.

Thanks!

  • Accepted Solution: Install-Package Microsoft.SqlServer.Types -Version 10.50.1600.1

解决方案

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!

To get around it, you can deseralize 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.

More info here: https://connect.microsoft.com/SQLServer/feedback/details/685654/invalidcastexception-retrieving-sqlgeography-column-in-ado-net-data-reader

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

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