简体   繁体   中英

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

According to the following solution ( https://stackoverflow.com/a/42976399/2825284 ):

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

My code:

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

How I can send null values in Geography field if allow NULL?

I get this error:

UdtTypeName property must be set only for UDT parameters.

One option can be set data has min value or another unexpected value that you can take like a null or unset value. Is in sql value set this field has nullable?

According Jeroen Mostert's comment this is the solution: SqlGeography.Null .

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

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

Thanks Jeroen Mostert.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM