如果在 C# 中使用 SqlParameter 和 SQL Server Types SqlGeography,如果允许 NULL,我如何在 Geography SQL Server 字段中发送空值?

How I can send null values in Geography SQL Server field if allow NULL, with SqlParameter and SQL Server Types SqlGeography in C#?

按照下面的解决方法 ():

cmd.Parameters.Add(new SqlParameter("@position", position) { UdtTypeName = "Geography" });

我的代码:

SqlGeography geographyfield = null;
sqlParameter.ParameterName = "@geographyfield";
sqlParameter.UdtTypeName = "Geography";

if (geographyfield != null)
    sqlParameter.Value = geographyfield;
else
    sqlParameter.Value = DBNull.Value; // or null, I get the same error

如果允许 NULL,我如何在 Geography 字段中发送空值?

我收到这个错误:

UdtTypeName property must be set only for UDT parameters.

一个选项可以设置数据具有最小值或另一个您可以采用的意外值,例如空值或未设置值。在 sql 值设置中此字段是否可为空?

根据 Jeroen Mostert 的评论,这是解决方案:SqlGeography.Null

SqlGeography geographyfield = null;
sqlParameter.ParameterName = "@geographyfield";
sqlParameter.UdtTypeName = "Geography";

if (geographyfield != null)
    sqlParameter.Value = geographyfield;
else
    sqlParameter.Value = SqlGeography.Null;

谢谢 Jeroen Mostert。