.Net核心:无法通过"地理位置" SQL Server存储过程的参数

时间:2018-06-13 19:08:11

标签: c# sql-server asp.net-core .net-core

我想将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)

然后我尝试指定地理参数是Udt:

cmd.Parameters["@Location"].SqlDbType = SqlDbType.Udt; 
//cmd.Parameters["@Location"].UdtTypeName = "Geography";

:由于this issue,我无法将UdtTypeName设置为Geography,所以这不会起作用,因为{ Udt参数需要{1}}。

然后,我升级到.Net Core 2.0。我尝试再次添加Microsoft.SqlServer.Types包:这次安装"成功",但是当我构建项目时,我收到此错误:

  

警告NU1701:软件包' Microsoft.SqlServer.Types 14.0.314.76'使用' .NETFramework,Version = v4.6.1'进行了恢复。而不是项目目标框架' .NETCoreApp,Version = v2.0'。此软件包可能与您的项目不完全兼容。

我无法访问代码中的任何类型,因此无法正常工作。

问题仍然存在:如何将SQL Server类型UdtTypeName的参数传递给.Net Core的存储过程?

0 个答案:

没有答案