I am having an issue with DateTime
inserting into SQL Server DateTime2(6)
data type. I have a variable in C# defined as Datetime
:
public DateTime CreatedOn { get; set; }
Below is the the date that I am passing to SQL Server:
When you notice the ticks on createdon
is ending with 786066. But I merged the above datetime
to SQL Server and after Update I am getting different nanoseconds:
The last four digits are getting changed. However when I do a Bulk copy on to the table the dates are inserted just fine. But when I merge row by row, the nano seconds are merged incorrectly.
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);
We are using SQL Server 2016.
foreach (var entity in entities)
{
try
{
await conn.ExecuteAsync(sql, commandType: CommandType.Text, param: entity, commandTimeout: SQLCommandTimeOut);
}
}
The SQL has the above merge query, I am passing entity as parameter and CreatedOn on the entity is DateTime. However instead of looping and inserting into table, if we use BulkCopy of C#, it is inserted properly with the actual DateTime C# has up to nano seconds, but row by row insert last 4 digits of nano seconds are different if you notice above screenshots.