使用 Npgsql 为时间戳列传递空值的正确语法是什么?
What is the correct syntax to pass a null value for a timestamp column using Npgsql?
我有一个带有时间戳类型列的 AWS Redshift table。
create table if not exists testtimestampfields
(
testid varchar(50) not null,
creationdate timestamp null
);
由于creationdate
字段允许null,我想我可以在插入记录时将一个null DateTime作为参数值传递。
[Fact]
public async Task Can_insert_null_timestamp_field()
{
// Custom class that calls GetClusterCredentials and returns a connection with the user and password...
await using var conn = await _redshiftConnectionManager.GetConnectionAsync();
await conn.OpenAsync();
await using var cmd = new NpgsqlCommand(
@"insert into testtimestampfields(testid, creationdate) values (@testid, @creationdate);", conn);
var testidParam = cmd.Parameters.Add("@testid", NpgsqlDbType.Varchar);
var creationDateParam = cmd.Parameters.Add("@creationdate", NpgsqlDbType.Timestamp);
testidParam.Value = Guid.NewGuid().ToString();
creationDateParam.Value = null;
await cmd.ExecuteNonQueryAsync();
}
但是当我尝试这个时 await cmd.ExecuteNonQueryAsync();
失败了:
System.InvalidCastException: Parameter @creationdate must be set
System.InvalidCastException
Parameter @creationdate must be set
at Npgsql.NpgsqlParameter.ValidateAndGetLength()
at Npgsql.NpgsqlParameterCollection.ValidateAndBind(ConnectorTypeMapper typeMapper)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
...
使用 Npgsql 为时间戳列传递空值的正确语法是什么?
将 null 更改为 DBNull.Value 有效。
creationDateParam.Value = DBNull.Value;
使用 .NET 日期时间?有条件地设置参数值的变量:
creationDateParam.Value = dateTime.HasValue ? dateTime.Value : DBNull.Value;
我有一个带有时间戳类型列的 AWS Redshift table。
create table if not exists testtimestampfields
(
testid varchar(50) not null,
creationdate timestamp null
);
由于creationdate
字段允许null,我想我可以在插入记录时将一个null DateTime作为参数值传递。
[Fact]
public async Task Can_insert_null_timestamp_field()
{
// Custom class that calls GetClusterCredentials and returns a connection with the user and password...
await using var conn = await _redshiftConnectionManager.GetConnectionAsync();
await conn.OpenAsync();
await using var cmd = new NpgsqlCommand(
@"insert into testtimestampfields(testid, creationdate) values (@testid, @creationdate);", conn);
var testidParam = cmd.Parameters.Add("@testid", NpgsqlDbType.Varchar);
var creationDateParam = cmd.Parameters.Add("@creationdate", NpgsqlDbType.Timestamp);
testidParam.Value = Guid.NewGuid().ToString();
creationDateParam.Value = null;
await cmd.ExecuteNonQueryAsync();
}
但是当我尝试这个时 await cmd.ExecuteNonQueryAsync();
失败了:
System.InvalidCastException: Parameter @creationdate must be set
System.InvalidCastException
Parameter @creationdate must be set
at Npgsql.NpgsqlParameter.ValidateAndGetLength()
at Npgsql.NpgsqlParameterCollection.ValidateAndBind(ConnectorTypeMapper typeMapper)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
...
使用 Npgsql 为时间戳列传递空值的正确语法是什么?
将 null 更改为 DBNull.Value 有效。
creationDateParam.Value = DBNull.Value;
使用 .NET 日期时间?有条件地设置参数值的变量:
creationDateParam.Value = dateTime.HasValue ? dateTime.Value : DBNull.Value;