我有一个访问 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.
我尝试过的一些事情:
- 将 Microsoft.SqlServer.Types v14.0.314.76 添加到 project.json(还尝试了其他几个版本。
- 将 HashFoo.SqlServer.SpatialTypes v10.0.0 添加到 project.json
- 将 Azure SQL Server 兼容性级别更改为 110。
尽管我可以看到服务器正在通过 Nuget 安装适当的引用,但错误消息仍然相同。
最佳答案
我不确切知道要加载哪个程序集才能使其工作,但您可以通过使用 .ToString() 将该列转换为 WKT 来回避这个问题。您的查询中的函数。
由于您要将结果格式化为 JSON,因此这可能正是您想要的。
例如
select ...., geo_location.ToString() geo_location, . . .
from some_table
关于c# - 从 Azure Function 中的 Azure SQL 访问几何列时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51717838/