I have some C# code that copies rows from one database and inserts them into another. It does this using a DataTable
and SqlBulkCopy
.
When imported into my C# application, the timestamp columns have the data type System.DateTime
inside the DataTable
that is inserted into SQL Server. Once SqlBulkCopy.WriteToServer()
has executed, timestamp values inside the destination tables have the type datetimeoffset(6)
and have a timezone offset added to them (... +01:00).
How do I stop this happening? It hasn't always happened, only started happening recently.
UPDATE:
The timezone expected is UTC, always, for my purposes. However, I am forced to store this in a datetimeoffset column for business reasons. So I'm expecting +00:00
DataTable data = importer.GetDataTable();
using (SqlBulkCopy copy = new SqlBulkCopy(conn)){
copy.WriteToServer(data);
}
Using DateTime.SpecifyKind() on timestamp columns before inserting them into Sql Server didn't work for me.
I solved this by converting (casting) the System.DateTime
columns to DateTimeOffset
with an explicit offset of new TimeSpan(0, 0, 0)
. This removed the need for C# to implicitly handle the conversion from DateTime
to DbType.DateTimeOffset
which was adding an undesired offset.
EDIT
Reading the comments, @JohnSkeet essentially recommended this but I hadn't read everyone's comments.