簡體   English   中英

如果在 C# 中使用 SqlParameter 和 SQL Server 類型 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#?

根據以下解決方案( https://stackoverflow.com/a/42976399/2825284 ):

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 字段中發送空值?

我收到此錯誤:

必須僅為 UDT 參數設置 UdtTypeName 屬性。

一個選項可以設置數據具有最小值或另一個意外值,您可以將其視為空值或未設置值。 在 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;

謝謝傑倫·莫斯特。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM