I'm storing in my database using a store procedure the date time stamp using a function called sysdatetime()
and a type of varchar(max)
which stores in the database in this format yyyy-mm-dd hh:mm:ss.sssssss
. When I'm creating a new post its fine but when I'm trying to update the record I have to send a parameter to the store procedure with the current datetime
. I have tried this one
DateTime.Now.ToString(yyyy-mm-dd hh:mm:ss.sssssss)
but I'm getting an error. what i have to do?
P.s: see the image below with the error message
I suspect if you really need the string representation, you actually want:
string text = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fffffff",
CultureInfo.InvariantCulture)
Note that:
UtcNow
instead of Now
. For timestamps, you almost always want UTC.CultureInfo.InvariantCulture
See MSDN for more details of custom date and time formatting.
However, I'd strongly recommend that you try to avoid string conversions wherever possible. Why can't your stored procedure just use the relevant DateTime
type instead of a string representation? Then you can pass the value to the stored procedure as a DateTime
(via a command parameter) and you can get rid of the error-prone string conversion clutter.
Use this code:
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff")
See this ref: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With