我在将DateTime
插入SQL Server DateTime2(6)
数据类型时遇到问题。我在C#中有一个定义为Datetime
的变量:
public DateTime CreatedOn { get; set; }
以下是我传递给SQL Server的日期:
当您注意到createdon
上的刻度以786066结尾。但是我将上述datetime
合并到SQL Server,并且在Update之后,我得到了不同的纳秒:
最后四位数字已更改。但是,当我在表上进行批量复制时,日期插入就很好了。但是当我逐行合并时,纳秒被错误地合并了。
MERGE [dbo].[ItemAttribute] as tgt
USING
(VALUES(@FK_ItemAttributeGroupCode, @ItemAttributeCode, @Name, @Description,
@EffectiveDate, @TerminationDate, @DataType, @DefaultString,
@DefaultNumeric, @DefaultDate, @FK_ItemGroupTypeCode,
@IsPublishable, @AttributeType, @IsAutoDelete, @IsFacilitySpecific,
@CreatedBy, @CreatedOn, @LastUpdatedBy, @LastUpdatedOn)) AS src ([FK_ItemAttributeGroupCode], [ItemAttributeCode], [Name], [Description], [EffectiveDate], [TerminationDate], [DataType], [DefaultString], [DefaultNumeric], [DefaultDate], [FK_ItemGroupTypeCode], [IsPublishable], [AttributeType], [IsAutoDelete], [IsFacilitySpecific], [CreatedBy], [CreatedOn], [LastUpdatedBy], [LastUpdatedOn])
ON tgt.[FK_ItemAttributeGroupCode] = @FK_ItemAttributeGroupCode AND tgt.[ItemAttributeCode] = @ItemAttributeCode
WHEN MATCHED AND (EXISTS (SELECT TGT.[Name], TGT.[Description], TGT.[EffectiveDate], TGT.[TerminationDate], TGT.[DataType], TGT.[DefaultString], TGT.[DefaultNumeric], TGT.[DefaultDate], TGT.[FK_ItemGroupTypeCode], TGT.[IsPublishable], TGT.[AttributeType], TGT.[IsAutoDelete], TGT.[IsFacilitySpecific], TGT.[CreatedBy], TGT.[CreatedOn], TGT.[LastUpdatedBy], TGT.[LastUpdatedOn] EXCEPT SELECT @Name, @Description, @EffectiveDate, @TerminationDate, @DataType, @DefaultString, @DefaultNumeric, @DefaultDate, @FK_ItemGroupTypeCode, @IsPublishable, @AttributeType, @IsAutoDelete, @IsFacilitySpecific, @CreatedBy, @CreatedOn, @LastUpdatedBy, @LastUpdatedOn))
THEN
UPDATE
SET [FK_ItemAttributeGroupCode] = @FK_ItemAttributeGroupCode, [ItemAttributeCode] = @ItemAttributeCode, [Name] = @Name, [Description] = @Description, [EffectiveDate] = @EffectiveDate, [TerminationDate] = @TerminationDate, [DataType] = @DataType, [DefaultString] = @DefaultString, [DefaultNumeric] = @DefaultNumeric, [DefaultDate] = @DefaultDate, [FK_ItemGroupTypeCode] = @FK_ItemGroupTypeCode, [IsPublishable] = @IsPublishable, [AttributeType] = @AttributeType, [IsAutoDelete] = @IsAutoDelete, [IsFacilitySpecific] = @IsFacilitySpecific, [CreatedBy] = @CreatedBy, [CreatedOn] = @CreatedOn, [LastUpdatedBy] = @LastUpdatedBy, [LastUpdatedOn] = @LastUpdatedOn
WHEN NOT MATCHED
THEN
INSERT([FK_ItemAttributeGroupCode], [ItemAttributeCode], [Name], [Description], [EffectiveDate], [TerminationDate], [DataType], [DefaultString], [DefaultNumeric], [DefaultDate], [FK_ItemGroupTypeCode], [IsPublishable], [AttributeType], [IsAutoDelete], [IsFacilitySpecific], [CreatedBy], [CreatedOn], [LastUpdatedBy], [LastUpdatedOn])
VALUES(@FK_ItemAttributeGroupCode, @ItemAttributeCode, @Name, @Description, @EffectiveDate, @TerminationDate, @DataType, @DefaultString, @DefaultNumeric, @DefaultDate, @FK_ItemGroupTypeCode, @IsPublishable, @AttributeType, @IsAutoDelete, @IsFacilitySpecific, @CreatedBy, @CreatedOn, @LastUpdatedBy, @LastUpdatedOn);
我们正在使用SQL Server 2016。
foreach (var entity in entities)
{
try
{
await conn.ExecuteAsync(sql, commandType: CommandType.Text, param: entity, commandTimeout: SQLCommandTimeOut);
}
}
Sql具有以上合并查询,我将实体作为参数传递,并且实体上的CreatedOn为DateTime。但是,如果我们使用C#的BulkCopy,而不是循环并插入到表中,则可以使用实际的DateTime C#将其正确插入,直到n#秒为止,但是如果您注意到上面的屏幕截图,则逐行插入的最后4位nano秒是不同的。 / p>
对此有任何帮助吗?