469,631 Members | 1,284 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

Conversion of .NET DateTime to SQL datetime via VarBinary

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

Mar 23 '07 #1
1 2464
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!
*********************************************
"Karch" <no****@absotutely.comwrote in message
news:eE**************@TK2MSFTNGP05.phx.gbl...
>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
Mar 24 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Kiran | last post: by
3 posts views Thread by moni | last post: by
reply views Thread by =?Utf-8?B?RGVuaXMgU29oZXQ=?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.