I am using C# and SQL Server 2005. I want to store date and time in my table. In SQL Server, I use smalldatetime datatype. I am using this code.
TimeZoneInfo ob1 = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime dt = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, ob1);
and insert data through this query
insert into tablename (name,sub_date) values (@name,'{0}')",dt.ToShortTimeString()
but, the problem is that in my table date and time is stored like this 1/1/1900 5:15:00 PM. Actual date is not stored. How to store actual date and time in database.
Note: I want to show all the records of table according to date and time. So, date and time both are important for me.
Try something like this:
That should do the trick! No messy conversion from
DateTimeto string – theDateTimevalue is preserved “as is” and stored in that same format in SQL Server. By using parameters, you’ve both eliminated the possibility of a SQL injection attack, and also, this query can be re-used if you need to call it multiple times with different values, thus increasing performance, too!