简体   繁体   English

如何使用 C# 客户端和通用参数 API 将 NULL 插入带有时区列的时间戳到 Postgres

[英]How to insert a NULL into a timestamp with timezone column into Postgres using C# client and generic parameter API

I have a column in a table whose type is set to Timestamp with time zone.我在表中有一个列,其类型设置为带时区的时间戳。 I am calling a function (whose argument is also of type Timestamp with time zone) to insert rows into the table.我正在调用 function (其参数也是带时区的时间戳类型)将行插入表中。 I am using the generic parameter API and I need to be able to set the value to NULL.我使用的是通用参数 API,我需要能够将值设置为 NULL。 I cannot use我不能使用

command.Parameters.Add(new NpgsqlParameter<DateTime>("p_responded", null));

I get a compilation error that there is no constructor.我得到一个编译错误,没有构造函数。 So I tried using a nullable DT:所以我尝试使用可以为空的 DT:

 command.Parameters.Add(new NpgsqlParameter<DateTime?>("p_responded", null));

I get the following exception我得到以下异常

System.InvalidCastException: Cannot write DateTime with Kind=UTC to PostgreSQL type 'timestamp without time zone', consider using 'timestamp with time zone'. System.InvalidCastException:无法将 Kind=UTC 的 DateTime 写入 PostgreSQL 类型“不带时区的时间戳”,请考虑使用“带时区的时间戳”。 Note that it's not possi ble to mix DateTimes with different Kinds in an array/range.请注意,将 DateTimes 与数组/范围中的不同种类混合是不可能的。 See the Npgsql.EnableLegacyTimestampBehavior AppContext switch to enable legacy behavior.请参阅 Npgsql.EnableLegacyTimestampBehavior AppContext 开关以启用旧行为。

Supporting null with the generic parameter API is tracked in https://github.com/npgsql/npgsql/issues/3679 .支持 null 与通用参数 API 在https://github.com/npgsql/npgsql/issues/3679中跟踪。

In the meantime, use the non-generic parameter API and pass DBNull.Value.同时,使用非泛型参数 API 并传递 DBNull.Value。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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