在 Azure Function 中从 Azure SQL 访问几何列时出错

Error accessing Geometry column from Azure SQL in Azure Function

我有一个访问 Azure SQL 数据库的 Azure 函数。数据库中的第 42 列是 Geography 数据类型(这就是问题所在)。

这是我的代码 运行:

// var datatable = DataTable();
// Filled 'datatable' from a database query

string json = Newtonsoft.Json.JsonConvert.SerializeObject(dataTable, Newtonsoft.Json.Formatting.Indented);
return new HttpResponseMessage(HttpStatusCode.OK) 
{
    Content = new StringContent(json, Encoding.UTF8, "application/json")
}; 

我收到此错误消息:

Exception while executing function: Functions.GetRepeaterDetails. Microsoft.Azure.WebJobs.Script: One or more errors occurred. System.Data: DataReader.GetFieldType(42) returned null.

我已经进行了几个小时的搜索并尝试了其他人声称有效的方法,但 none 已经修复了我的情况。

我在 another forum 上阅读了这篇文章:

Be aware that your sample will work, but what won’t work is a web application that retrieves geometry from SQL Azure, because SQL Azure is currently running Denali (SQL Server 2012) at compatibility level 100; hence it will return instances of the 10.0 spatial types and the web role will throw an exception when trying to cast them to 11.0.

我尝试过的一些事情:

  1. 将 Microsoft.SqlServer.Types v14.0.314.76 添加到 project.json(也尝试了其他几个版本。
  2. 已将 HashFoo.SqlServer.SpatialTypes v10.0.0 添加到 project.json
  3. 将 Azure SQL 服务器兼容性级别更改为 110。

尽管我可以看到服务器正在通过 Nuget 安装适当的引用,但错误消息仍然保持不变。

我不知道要加载哪个程序集才能使其正常工作,但您可以通过在查询中使用 .ToString() 函数将该列转换为 WKT 来回避该问题。

由于您要将结果格式化为 JSON,这可能正是您想要的。

例如

select ...., geo_location.ToString() geo_location, . . .
from some_table

你也可以 SQL Server Return Query Results as JSON