我想将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)
然后我尝试指定地理参数是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的存储过程?