首页 > 解决方案 > .Net Core:无法将“geography”参数传递给 SQL Server 存储过程

问题描述

我想将 SQL Server 类型的参数geography从 .Net Core 传递给存储过程。

到目前为止,我已经安装了System.Data.SqlClientMicrosoft.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 参数需要 。GeographyUdtTypeName

然后,我升级到 .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 传递给存储过程?

标签: c#sql-serverasp.net-core.net-core

解决方案


推荐阅读