简体   繁体   English

将SQL Server 2012地理数据类型与ASP.Net一起使用-DataReader.GetFieldType(x)返回null

[英]Using SQL Server 2012 Geography Data Type with ASP.Net - DataReader.GetFieldType(x) returned null

I've written an ASP.Net 4 Application which has been working perfectly. 我编写了一个ASP.Net 4应用程序,该应用程序运行良好。 However, I decided to make use of the new Geography Data Type in Sql Sever 2012. This worked perfectly on my local machine but fails when I upload. 但是,我决定在Sql Sever 2012中使用新的Geography数据类型。这在我的本地计算机上运行良好,但是在我上载时失败。

I get there error "DataReader.GetFieldType(9) returned null." 我到达错误“ DataReader.GetFieldType(9)返回空值”。 when doing a usual "select * FROM Table" query on a table that contains one of these datatypes. 在包含这些数据类型之一的表上执行常规的“ select * FROM Table”查询时。

I have searched the various threads regarding adding a reference to the Microsoft.SqlServer.Types.dll into my project and changing Copy Local to true. 我搜索了有关在我的项目中添加对Microsoft.SqlServer.Types.dll的引用并将“将本地复制”更改为true的各种线程。 However I am getting the same error. 但是,我得到同样的错误。

I run the database and the web server on two separate servers so I don't think there are any assemblies on the web server unless I upload them with my project. 我在两个单独的服务器上运行数据库和Web服务器,因此除非我将其与项目一起上传,否则我认为Web服务器上没有任何程序集。

Am I missing any other assemblies that are needed? 我是否缺少任何其他需要的程序集? or any other settings? 或其他设置?

I've taken days trying to solve this and uploading variouse libraries. 我花了几天时间试图解决这个问题并上传各种库。 Any help would be appreciated. 任何帮助,将不胜感激。

From what I can tell you will at least need SQL Server installed locally when you initially add the reference. 据我所知,当您最初添加引用时,至少将需要在本地安装SQL Server。 If you can I would try and install SQL Express locally first (you do not need to use it just install it) and then try your code again to see if that is the problem. 如果可以的话,我将首先尝试在本地安装SQL Express(无需使用它,只需安装它),然后再次尝试代码以查看是否存在问题。

References: 参考文献:

DataReader.GetFieldType returned null DataReader.GetFieldType返回null

http://blogs.bing.com/maps/2013/08/05/advance-spatial-queries-using-entity-framework-5/ http://blogs.bing.com/maps/2013/08/05/advance-spatial-queries-using-entity-framework-5/

Edit: 编辑:

I am more familiar with using spatial types via the entity framework. 我更熟悉通过实体框架使用空间类型。 It seems if you want to use them directly you need to use a nuget package in order to get the required DLLs into the project. 似乎,如果您想直接使用它们,则需要使用nuget包才能将所需的DLL放入项目中。 From the Microsoft ADO.NET blog page 从Microsoft ADO.NET博客页面

SqlServerSpatial110.dll – This is a native assembly so it cannot be added as a project reference. SqlServerSpatial110.dll –这是一个本机程序集,因此无法将其添加为项目引用。

References: 参考文献:

Microsoft ADO.NET Blog Microsoft ADO.NET博客

http://blogs.msdn.com/b/adonet/archive/2013/12/09/microsoft-sqlserver-types-nuget-package-spatial-on-azure.aspx http://blogs.msdn.com/b/adonet/archive/2013/12/09/microsoft-sqlserver-types-nuget-package-spatial-on-azure.aspx

Relevant SO Questions 相关SO问题

'Microsoft.SqlServer.Types' version 10 or higher could not be found on Azure https://gis.stackexchange.com/questions/382/how-can-i-use-sql-servers-spatial-types-from-a-net-application 在Azure https://gis.stackexchange.com/questions/382/how-can-i-use-sql-servers-spatial-types-from-a 上找不到'Microsoft.SqlServer.Types'版本10或更高版本 网络应用

msdn msdn

When the compatibility level is 100 or below in SQL Server 2012 then the geography data type has the following restrictions: 如果SQL Server 2012中的兼容级别为100或更低,则地理数据类型具有以下限制:

•Each geography instance must fit inside a single hemisphere. •每个地理实例必须适合一个半球。 No spatial objects larger than a hemisphere can be stored. 不能存储大于半球的空间对象。

•Any geography instance from an Open Geospatial Consortium (OGC) Well-Known Text (WKT) or Well-Known Binary (WKB) representation that produces an object larger than a hemisphere throws an ArgumentException. •来自开放地理空间联盟(OGC)的已知文本(WKT)或已知的二进制(WKB)表示形式的任何地理实例,如果产生的对象大于半球,都会引发ArgumentException。

•The geography data type methods that require the input of two geography instances, such as STIntersection(), STUnion(), STDifference(), and STSymDifference(), will return null if the results from the methods do not fit inside a single hemisphere. •需要输入两个地理实例(例如STIntersection(),STUnion(),STDifference()和STSymDifference())的地理数据类型方法,如果方法的结果不适合单个半球,则将返回null 。 STBuffer() will also return null if the output exceeds a single hemisphere. 如果输出超过单个半球,则STBuffer()也将返回null。

Spent heaps of time on this, adding references as well, but I found a workaround i want to share. 为此花了很多时间,也添加了参考,但是我发现了一个我想分享的解决方法。 Should work fine on asp.net as well. 也应该在asp.net上正常工作。

I use the following query to parse me the string representation of the object: 我使用以下查询来解析对象的字符串表示形式:

DECLARE @result geography;
SELECT @result = area FROM news WHERE id LIKE @id ;
SELECT ...,  @result.STAsText() as area FROM news WHERE id LIKE @id;

And in the asmx (c#) webservice i retrieved my SQLGeography with a parser method: 在asmx(c#)网络服务中,我使用解析器方法检索了我的SQLGeography:

row["area"].ToString()

After that you can use a parser method to retrieve the SQLGeography object. 之后,您可以使用解析器方法检索SQLGeography对象。

/// <summary>
/// Converts a String into an sql geography object.</summary>
/// <param name="pText">The string representation of the sql geography object. </param>
public static SqlGeography GetGeographyFromText(String pText)
{
    SqlString ss = new SqlString(pText);
    SqlChars sc = new SqlChars(ss);
    try
    {
        if (pText.Contains("POINT"))
        {
            return SqlGeography.STPointFromText(sc, 4326);
        }
        return SqlGeography.STPolyFromText(sc, 4326);
    }
    catch (Exception ex)
    {
        return SqlGeography.STMPolyFromText(sc, 4326);
        throw ex;
    }
}

Hope it helps anyone! 希望它能帮助任何人!

In the end I couldn't get it to work as it does on my development machine and I don't want to install Visual Studio or SQL Server Express on the web server so instead I just removed all the queries where I did "SELECT * FROM Table" and the error went away. 最后,我无法像在开发计算机上那样使它正常工作,并且我不想在Web服务器上安装Visual Studio或SQL Server Express,因此我只删除了所有执行“ SELECT *”的查询。 FROM Table”,错误消失了。 It still let me do my calculations using the geography field, but doesn't like you trying to show it on the screen. 它仍然可以让我使用“地理位置”字段进行计算,但不喜欢您尝试在屏幕上显示它。 Thanks for all your help! 感谢你的帮助!

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

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