views:

1674

answers:

4

i want to do above.

i have an object of table in c#.

A: 

For a DateTime object, just use

var d = new DateTime();
ripper234
actually not datetime but Timestamp in sql.
Vikas
A: 

DateTime d = new DateTime(2009,3,23);

msdn

PoweRoy
Vikas
+1  A: 

A SQL TimeStamp is based on insert / update order on your sql server. There is no way to assign it from within C#. I doubt SQL will even allow you to update a column of type TimeStamp.

TimeStamp is a binary sequence. (8 bytes I believe.) There is no correlation to a DateTime. Nothing to display for the user on a page.

If you want to show the user when a record was last modified or first created, you will have to maintain separate fields of type DateTime and update the values in either your application or within sql using sprocs or triggers.

Andrew Robinson
Timestamp requires a value of type System.Data.Linq.Binary.so how do we convert System.DateTime.Now.i don't want update as it'll be done automatically by sql( I Think).I think there must be some way because it is a simple thing.
Vikas
Timestamp does not contain date time information. There is no conversion.
Andrew Robinson
Thank you very much for providing me correct information.and sorry for late reply!
Vikas
+1  A: 

According to the documentation, the Transact-SQL timestamp data type is just an incrementing number and does not preserve a date or a time. You can't convert that to a meaningful DateTime value.

The documentation says that it's an 8-byte value. So, if it's stored in a System.Data.Linq.Binary, you could convert it to a long with this code:

System.Data.Linq.Binary timestampValue;
// Somehow get a reference to the SQL timestamp you're interested in

// Use BitConverter to convert the 8 bytes to a long
long timestampLong = BitConverter.ToInt64(timestampValue.ToArray(), 0);

You could try to convert that value to a DateTime by calling the constructor that takes a ticks value, but the results would be meaningless. You might also encounter an exception if the ticks value is out of range.

Jim Mischel