简体   繁体   中英

Error selecting geography coordinate in Azure ASP.NET website

An ASP.net webform on an Azure website selects from a table containing a geography field. This field causes an error "DataReader.GetFieldType(0) returned null." Here is the SQL to create the table and the code from the .ASPX.CS file.

SQL to Create a Test Table [T]:

CREATE TABLE [dbo].[T](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [P] [geography] NULL,
 CONSTRAINT [PK_T] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

INSERT INTO T (P) VALUES (geography::Point(51.4618933852762, -0.926690306514502, 4326));

Test.aspx.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string ConnectionStr = "Server=tcp:Server1,1433;Database=DB;User ID=User1;Password=Password1;Trusted_Connection=False;Encrypt=True;Connection Timeout=5;";
        SqlConnection Connection = new SqlConnection(ConnectionStr);
        Connection.Open();

        //string SQL = "SELECT CONVERT(VARBINARY(100), P) P FROM T";
        string SQL = "SELECT P FROM T";

        SqlDataAdapter da = new SqlDataAdapter(SQL, Connection);

        DataTable dt = new DataTable();

        da.Fill(dt);

        Connection.Close();
        Response.Write(dt.Rows[0]["P"].ToString());
    }
}

First, do you have SqlServerTypes added to your project? This is required in order for your project to "understand" the geography data type. UPDATE: Detailed instructions here .

Second, if you're trying to retrieve lat/long from the geography field, you'll need to query it as a Point:

SELECT P.Lat, P.Long FROM T

If you must use SqlServerTypes be sure to pull it into your solution via it's NuGet package ( https://www.nuget.org/packages/Microsoft.SqlServer.Types/ ), and be aware it includes both 32- and 64-bit versions, be sure you are using the same version as your website in Azure is using (instructions can be found in the package's readme.htm file).

You also likely need to consider the following, taken from readme.htm:

Action required to load native assemblies

To deploy an application that uses spatial data types to a machine that does not have 'System CLR Types for SQL Server' installed you also need to deploy the native assembly SqlServerSpatial110.dll. Both x86 (32 bit) and x64 (64 bit) versions of this assembly have been added to your project under the SqlServerTypes\\x86 and SqlServerTypes\\x64 subdirectories. The native assembly msvcr100.dll is also included in case the C++ runtime is not installed.

You need to add code to load the correct one of these assemblies at runtime (depending on the current architecture).

ASP.NET applications For ASP.NET applications, add the following line of code to the Application_Start method in Global.asax.cs:

 SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin")); 

Desktop applications For desktop applications, add the following line of code to run before any spatial operations are performed:

 SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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