Reputation: 3028
How to retrieve Timestamp value(eg:0x000000048E18B9D8 ) from SQL Server using DataReader
? I'm getting an IndexOutOfRangeException
when doing like this
DateTime date = reader.GetDateTime(reader.GetOrdinal("Timestamp"));
Can anyone help on this?
Also I want to know how to pass timestamp parameter to stored procedure using .Net code
Upvotes: 0
Views: 3032
Reputation: 1470
IndexOutOfRangeException
thrown on reader.Getordinal()
suggests that a column called Timestamp
doesn't exist. Check your column names again, and replace with the actual value. By default, it will be called timestamp
, but only if you haven't specified a column name.
You could also try string ts = reader["timestamp"].toString()
, and make sure it returns something.
According to MSDN, a TIMESTAMP
data type is "8 bytes...[and]... just an incrementing number and does not preserve a date or a time. To record a date or time, use a datetime data type."
Therefore, reader.GetInt64()
is probably close, if you really need this value, but byte[] myTimestamp = reader["timestamp"]
is probably better.
As far as passing it back to your stored procedure, you can create a new parameter with SqlDbType.Timestamp
. The value will be a byte array, or, if you have it stored as a string, you could try something like:
cmd.Parameters.Add("@TimeStampParam", SqlDbType.Timestamp).Value = System.Text.Encoding.ASCII.GetBytes(myTimestampStr);`
(NB: I haven't tested this, but it should be close enough).
Note that TIMESTAMP
is deprecated, and ROWVERSION
is the preferred syntax.
Upvotes: 1