c# - .Net Core:无法将“geography”参数传递给 SQL Server 存储过程
问题描述
我想将 SQL Server 类型的参数geography
从 .Net Core 传递给存储过程。
到目前为止,我已经安装了System.Data.SqlClient
和Microsoft.Spatial
包。这是我的代码:
using (SqlCommand cmd = new SqlCommand("myproc", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
// Here, GeographyPoint is of type Microsoft.Spatial.GeographyPoint, and
// I am pretty sure this is the wrong type to use...
GeographyPoint gp = GeographyPoint.Create(lat, lon);
cmd.Parameters.AddWithValue("@Location", gp);
connection.Open();
cmd.ExecuteNonQuery();
}
当我尝试执行此操作时,出现以下异常:
System.ArgumentException:'不存在从对象类型 Microsoft.Spatial.GeographyPointImplementation 到已知托管提供程序本机类型的映射
然后我尝试安装Microsoft.SqlServer.Types
包,但它与.Net Core 1.1 不兼容:
包 Microsoft.SqlServer.Types 14.0.314.76 与 netcoreapp1.1 (.NETCoreApp,Version=v1.1) 不兼容。包 Microsoft.SqlServer.Types 14.0.314.76 支持:net40 (.NETFramework,Version=v4.0)
然后我尝试指定 geography 参数为 Udt:
cmd.Parameters["@Location"].SqlDbType = SqlDbType.Udt;
//cmd.Parameters["@Location"].UdtTypeName = "Geography";
但是:由于这个问题,我无法将 设置为UdtTypeName
,所以这不起作用,因为Udt 参数需要 。Geography
UdtTypeName
然后,我升级到 .Net Core 2.0。我尝试再次添加 Microsoft.SqlServer.Types 包:这次它“成功”安装了,但是当我构建我的项目时,我收到了这个错误:
警告 NU1701:使用“.NETFramework,Version=v4.6.1”而不是项目目标框架“.NETCoreApp,Version=v2.0”恢复包“Microsoft.SqlServer.Types 14.0.314.76”。此软件包可能与您的项目不完全兼容。
我无法访问代码中的任何类型,所以这不起作用。
问题仍然存在:如何将 SQL Server 类型的参数geography
从 .Net Core 传递给存储过程?
解决方案
推荐阅读
- html - ngModel 使用 Angular8 时未显示加载的数据
- python - 来自def的通话清单
- encoding - Base64 编码的秘密
- position - 为什么如果我改变(.hamburger > div > position: fixed)汉堡变得这么长(大于父母的宽度)?
- rust - 为什么不能在运行时安全地生成 fmt::Arguments?
- java - 简单的碰撞检测项目
- oracle - SSMA for Oracle - 连接到 Azure SQL 时出现“未知此类主机”
- python - 词法分析器将所有外部调用的 txt 文件代码行识别为标识符而不是标记
- google-sheets-formula - 使用修剪删除 Google 表格查询中的空格
- angular - Angular 9:调用 API 来检查会话是否未过期的 Guard