how print timestamp in ASP.net - c#

I have in SQl Server table with col timestamp.
Now i use Entity Framework 6, and get in model field byte[].
Now what I should do to save current date to this field, and what i should do to print in view this date like(dd/mm/yyyy hh/mm/ss)
I find this:
How to convert byte array (SQL-server Timestamp) to DateTime (C#)?
and I felt that I should not use tiemstamp. What is a correct way to solved it?

The timestamp data type in SQL Server is a misnomer. It is not related to date/time at all so it cannot be converted to a .NET DateTime. The SQL Server timestamp data type is actually a binary value that changes every time the row is updated, intended to be used for optimistic concurrency checking. The use of the timestamp data type name is deprecated in favor of the alias rowversion in later SQL Server versions.
You need to use SQL Server data type datetime, datetime2, datetimeoffset, or time to store temporal values in SQL Server.

Related

How to pass a "Datetime" datatype to SQL with Entity framework (not with strings)?

I've been looking for a solution to this for hours. The problem I have having is that I have an SQL table which is storing dates as datetime DATATYPES, but when I try to pass a DateTime field to this table with Entity framework It's of datetime2 DATATYPE. All the solutions I found is using strings to format the date but this isn't working for me since I am using Entity Framework, and Entity Framework is expecting a DateTime type variable to be passed to the model and NOT a string.
result.SurveyDate = Convert.ToDateTime(CurrentSurveryDateValue);
I can't do something like the above because this is sending a datatime2 DATATYPE to the my model result. Which throws this error...
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value
Now I can't pass a string formatted to be of datetime format either because it throws a compile time error stating I can only pass DateTime types and not string...
How can i resolve this without changing the date datatype within my SQL table?

.Net DateTime cannot be saved to SQL Server DATETIME because of precision

I have a property on an class that is of the .Net type DateTime. It is attempting to save into a table in SQL Server 2008 with a type of DATETIME. I am receiving a Database Error when I attempt to save a new record to the table from my .Net service.
When I look at SQL Server Profiler and see the call to the Stored Procedure that saves to the table, the property is a string: '2014-09-04 23:08:18.0500000'. When I truncate this string to just milliseconds the Stored Procedure call succeeds. The conversion of my .Net DateTime property to this string all happens under the hood and I have no control over that.
I do not need the full precision that I am seeing in the string, but it is important to keep milliseconds. I would rather not change my table column to a data type of DATETIME2. How can I remove the extra precision from the .Net DateTime property?
DateTime dateTime;
dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
Please have #Tanner reply and mark that as an answer if correct as I believe he is correct in his comment above. Be careful to check for NULL on DateTime as you cannot convert to formated string if no data exists.
Below explains the range of DATETIME field in SQL Server.
http://msdn.microsoft.com/en-us/library/ms187819.aspx

SQL Server Timestamp using with C#

In my database I have used Timestamp in each table to see when data was inserted.
It stores data in byte[] of 8 byte.
Now I want to read that time using C#.
How can I get DateTime object from Timestamp which is byte[]?
SQL Server's TIMESTAMP datatype has nothing to do with a date and time!
It's just a binary representation of a consecutive number - it's only good for making sure a row hasn't change since it's been read.
In never versions of SQL Server, it's being called RowVersion - since that's really what it is. See the MSDN docs on ROWVERSION:
Is a data type that exposes automatically generated, unique binary numbers within a database. rowversion is generally used as a mechanism
for version-stamping table rows. The
rowversion data type is just an incrementing number and does not
preserve a date or a time. To record a date or time, use a datetime2
data type.
So you cannot convert a SQL Server TIMESTAMP to a date/time - it's just not a date/time.
But if you're saying timestamp but really you mean a DATETIME column - then you can use any of those valid date formats described in the CAST and CONVERT topic in the MSDN help. Those are defined and supported "out of the box" by SQL Server. Anything else is not supported, e.g. you have to do a lot of manual casting and concatenating (not recommended).
The format you're looking for looks a bit like the ODBC canonical (style = 121):
DECLARE #today DATETIME = SYSDATETIME()
SELECT CONVERT(VARCHAR(50), #today, 121)
gives:
2011-11-14 10:29:00.470
SQL Server 2012 will finally have a FORMAT function to do custom formatting......

Is is possible to change the date format of the LastLoginDate in the aspnet_membership table?

In the current project I'm working the initial developer had used ASP.NET Membership to handle the user login validation. As such the LastLoginDate and the LastActivityDate in the aspnet_Membership table are save in UTC format.
Is there anyway to save it in the local time format? Or does anyone know which stored procedure could be modified to compensate for the time difference (by using the DATEADD() method)?
I would caution against changing the data from UTC to local time.
SqlMembershipProvider returns MembershipUser.LastLoginDate and other similar properties in local time: i.e. it converts the database value from UTC to local.
So if you're accessing this data via the Membership API, you don't need to do this.
If you're accessing the data in the database directly, and you really, really want SQL Server to return it in local time, why not just convert it when you're reading from the database.
For example, you could create a VIEW on the aspnet_Membership table something like:
SELECT
...
LastLoginDate + GETDATE() - GETUTCDATE() AS LastLoginDateLocal
...
FROM aspnet_Membership
Note that by doing the conversion in SQL Server, you'll be getting the local time of the SQL Server, which may not be the same as the local time on the machine hosting your application.
UPDATE
From comments:
I want to know why #Leo approach didn't work.
#Leo suggested modifying the aspnet_Membership_UpdateUserInfo SP; you need to modify all SPs that update the columns you're interested in. For example, LastLoginDate is also updated by aspnet_Membership_UpdateUser.
Also, if you follow this approach, the time returned in MembershipUser.LastLoginDate property will be incorrect, since the SqlMembershipProvider code assumes the database value is in UTC. To correct this problem, you would need to modify all the Membership SPs that SELECT this column, to convert back to UTC. E.g. aspnet_Membership_GetAllUsers, aspnet_Membership_FindUsersByEmail, aspnet_Membership_FindUsersByName, ...
Note also that converting between UTC and local in SQL Server as above will sometimes give incorrect values (+/- 1h) due to DST (e.g. the difference GETDATE() - GETUTCDATE() is calculated during a period when DST is in operation, but the user last logged in before DST started).
Another reason why it's better to leave the database value in UTC, and do any conversion in your application.
aspnet_Membership_UpdateUserInfo is the Sp which update the LastLoginDate. You can modify this SP and cast the data format according to your requirement.

is it required to pass timestamp value in entitymodel to insert a data into sql table

I am working on ado.net entity model and I want to insert data into sql table.
SQL table structure is shown below.
ID int,
Name varchar(20),
EventTime Timestamp
I am trying to insert data into SQL Table using entitymodel but data are not inserted in the table.
My passing arguments are (1,"Test") it's not working.
I figured out that when I pass (1, "test", new Datetime()) then it works.
Can anyone let me know reason for that?
I understand that Timestamps value are automatically inserted by SQL server.
The "timestamp" data type gives you a binary value that automatically gets updated every time your field changes, but it won't give you a nice date/time value.
Update:
as per MSDN: timestamp Is a data type that exposes automatically generated, unique binary numbers within a database. timestamp is generally used as a mechanism for version-stamping table rows. The storage size is 8 bytes. The timestamp data type is just an incrementing number and does not preserve a date or a time.
To record a datetime when the record has been inserted, use a datetime data type. You might add a column of type datetime to your table and set getDAte() to generate the datetime.

Categories

Resources