c# - 从 Azure Function 中的 Azure SQL 访问几何列时出错

标签 c# azure azure-sql-database azure-functions sqlgeography

我有一个访问 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.

我已经进行了几个小时的搜索并尝试了其他人声称有效的方法,但没有一个能够解决我的情况。

我在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 Server 兼容性级别更改为 110。

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

最佳答案

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

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

例如

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

您也可以只拥有 SQL Server Return Query Results as JSON

关于c# - 从 Azure Function 中的 Azure SQL 访问几何列时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51717838/

相关文章:

c# - Entity Framework - 使用默认参数调用存储过程

C# * 和 & 运算符指向数组

c# - 验证 File.Delete 是否已使用 Moq 调用

azure - azurerm_role_assignment 的资源 block 中的多个角色

c# - 为什么这两个函数不返回相同的值?

.net - Visual Studio + Azure - 部署将引用复制到输出目录

azure - HDInsight - 由于 -8 个核心的订阅限制,Spark 群集验证失败

sql-server - SQL Azure 读取横向扩展功能 : Monitor queries on read-only replica

mysql - Azure:如何最好地将 SQL 数据库与 MySQL/Web 服务连接

Azure SQL 数据库自动调整 - 开发时无需担心索引?