Conversion of .NET DateTime to SQL datetime via VarBinary

  • Thread starter Thread starter Karch
  • Start date Start date
K

Karch

I am doing some experimenting with serialization (for use with Service
Broker) and I am having a problem converting from a .NET DateTime (in the
client application) to a SqlDbType.VarBinary (as passed to the stored
procedure VARBINARY(MAX)) and then finally back to a valid DATETIME in Sql
Server. It seems I have tried everything, but I always get an error when
trying to convert the hex value representation of the byte array to a valid
DATETIME in SQL. Any help appreciated.

So, it would be something like DateTime.Now -> SqlDbType.VarBinary ->
DATETIME
 
Why not send the date across as a SqlDateTime? It would alleviate your issue
and it is still serializable. If you are not storing as varbinary, why
transport via this representation.

As for how to pull from varbinary, I would have to look at how datetime is
represented in binary format. It is possible you will have to go bit by bit
(or byte by byte) to make sense of the datetime. I am sure you can
reassemble, but you will have to create the calculation. Most likely, the
binary data is a character by character representation, in bytes, which
means you will have to pull the date back out, as a string, and then
translate. Once you conquer this, you can make your own translation method
to convert. As soon as .NET 3.5 Framework is out, you could make this an
extension method.

But, we come back to the why? Why transfer as varbinary when SqlDateTime is
serializable?

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*********************************************
Think outside the box!
*********************************************
 
Back
Top