简体   繁体   English

SqlGeography类型不匹配

[英]SqlGeography Type Mismatch

I have encountered the following error while writing an internal API. 编写内部API时遇到以下错误。 What I am trying to do is read a SqlGeography value (SQL Server 2012) in the following fashion: 我正在尝试以以下方式读取SqlGeography值(SQL Server 2012):

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. 现在,尽管这看起来有些奇怪,但我以这种方式阅读的原因是因为它是我编写的包装程序的一部分,我们使用它来加快sql的读写速度。 It takes an anonymous object and reads all the sql values into it based either on the property names or the attribute names. 它接受一个匿名对象,并根据属性名称或属性名称将所有sql值读入其中。

This works fine for everything except SqlGeography . 这对于SqlGeography以外的所有对象都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 . 我进行了类型比较,但它也同样失败,因此很hacky,我什至无法检查该列是否为SqlGeography类型,因为它总是无法与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'. 类型为“ Microsoft.SqlServer.Types.SqlGeography”的对象不能转换为“ 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 接受的解决方案:Install-Package Microsoft.SqlServer.Types-版本10.50.1600.1

You could have a version mismatch on the Types assembly. 您可能在Types程序集上存在版本不匹配的情况。 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! 这是版本10和11之间的一个已知问题。不幸的是,该错误消息不包含版本信息,这就是为什么它看起来很废话!

To get around it, you can deserialize the type's binary representation, ie 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 此处的更多信息: https : //connect.microsoft.com/SQLServer/feedback/details/685654/invalidcastexception-retrieving-sqlgeography-column-in-ado-net-data-reader

I am using 我在用

DataTable dt;
//...
dt.Load(reader)

to load all rows from a sql table into a DataTable, so i cannot use the solution proposed by Dave R. My solution is to modify the sql select query used to create the SqlDataReader to transform the SqlGeography column to a string. 将所有行从sql表加载到DataTable,所以我不能使用Dave R提出的解决方案。我的解决方案是修改用于创建SqlDataReader的sql select查询,以将SqlGeography列转换为字符串。 example: 例:

Original sql query: 原始sql查询:

SELECT [SpatialColumn] as SpatialData FROM [SpatialTable]

Modified sql query: 修改后的sql查询:

SELECT [SpatialColumn].STAsText() as SpatialData FROM [SpatialTable]

then you have to change your c# code to parse the string into a real SqlGeography like this: 那么您必须更改c#代码以将字符串解析为真正的SqlGeography,如下所示:

using Microsoft.SqlServer.Types.SqlGeography;

string spatialData = (string) dt.Rows[0]["SpatialData"];
SqlGeography geoGraphy = SqlGeography.Parse(new System.Data.SqlTypes.SqlString(spatialData ));

Anben. 鞍本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM