473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C#, SQL and the DateTime object...

Hi,

I create a DateTime object in C# like this...

DateTime _DateStart = new DateTime(2000, 1, 1, 1, 1, 1, 1);

Now I save it to the Database (into a DateTime type of column) using the
SqlCommand and later I reload the value. Comparing those using the == operator
of the DateTime object yields incorrect results. Say I have two DateTime
objects, one was loaded from the DB:

DateTime _DateLoaded = LoadTheDateFromTheDB();
DateTime _DateToCompareWith = new DateTime(2000, 1, 1, 1, 1, 1, 1);

if (_DateLoaded == _DateToCompareWith)
// unreachable code detected :))

Obviously, the Tick-Count is not equal. How do I get on about saving DateTime
values to a Database. Should I store the ticks instead of the dates? Am I
something missing about the DateTime object and its == operator?

In fact, I'm actually not even interested in the Milliseconds part (and
everything beyond that level of precision). I'd just like to compare two dates
based on their Date (DDMMYYYY) and their Time (HHMMSS) values.

Thanks for any help in advance!

--

kind regards,

matthias

--

I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]
Nov 16 '05 #1
3 11997
Hi Matthias,

Most of the compatibility issues get resolved when you use the SqlDateTime
object. Try with this object and see whether it works fine.

HTH,
Rakesh Rajan

"Matthias S." wrote:
Hi,

I create a DateTime object in C# like this...

DateTime _DateStart = new DateTime(2000, 1, 1, 1, 1, 1, 1);

Now I save it to the Database (into a DateTime type of column) using the
SqlCommand and later I reload the value. Comparing those using the == operator
of the DateTime object yields incorrect results. Say I have two DateTime
objects, one was loaded from the DB:

DateTime _DateLoaded = LoadTheDateFromTheDB();
DateTime _DateToCompareWith = new DateTime(2000, 1, 1, 1, 1, 1, 1);

if (_DateLoaded == _DateToCompareWith)
// unreachable code detected :))

Obviously, the Tick-Count is not equal. How do I get on about saving DateTime
values to a Database. Should I store the ticks instead of the dates? Am I
something missing about the DateTime object and its == operator?

In fact, I'm actually not even interested in the Milliseconds part (and
everything beyond that level of precision). I'd just like to compare two dates
based on their Date (DDMMYYYY) and their Time (HHMMSS) values.

Thanks for any help in advance!

--

kind regards,

matthias

--

I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]

Nov 16 '05 #2
Matthias,

If all you are worried about is the seconds, then you should make sure
that the milliseconds are not passed in when you create your DateTime
instance. I believe that the DateTime is stored as it is in COM (a double
where the fractional part represents the hours, minutes, seconds,
milliseconds, etc, etc), and because it is a float, is suspect to errors for
numbers with a large number of places.

I would recommend making sure that there are no milliseconds on the
DateTime value when you save it, and that should prove to be just fine.
Your tick counts should come out equal.

Hopet his helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Matthias S." <po*****@emvoidSPAMTRAP.de> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl...
Hi,

I create a DateTime object in C# like this...

DateTime _DateStart = new DateTime(2000, 1, 1, 1, 1, 1, 1);

Now I save it to the Database (into a DateTime type of column) using the
SqlCommand and later I reload the value. Comparing those using the ==
operator of the DateTime object yields incorrect results. Say I have two
DateTime objects, one was loaded from the DB:

DateTime _DateLoaded = LoadTheDateFromTheDB();
DateTime _DateToCompareWith = new DateTime(2000, 1, 1, 1, 1, 1, 1);

if (_DateLoaded == _DateToCompareWith)
// unreachable code detected :))

Obviously, the Tick-Count is not equal. How do I get on about saving
DateTime values to a Database. Should I store the ticks instead of the
dates? Am I something missing about the DateTime object and its ==
operator?

In fact, I'm actually not even interested in the Milliseconds part (and
everything beyond that level of precision). I'd just like to compare two
dates based on their Date (DDMMYYYY) and their Time (HHMMSS) values.

Thanks for any help in advance!

--

kind regards,

matthias

--

I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]

Nov 16 '05 #3
To expand on Nicholas's response, Sql Server datetime values are only
accurate to the nearest 3.33 milliseconds and are rounded accordingly. This
is documented in the Sql Server documentation here:
http://msdn.microsoft.com/library/de...da-db_9xut.asp

"Matthias S." wrote:
Hi,

I create a DateTime object in C# like this...

DateTime _DateStart = new DateTime(2000, 1, 1, 1, 1, 1, 1);

Now I save it to the Database (into a DateTime type of column) using the
SqlCommand and later I reload the value. Comparing those using the == operator
of the DateTime object yields incorrect results. Say I have two DateTime
objects, one was loaded from the DB:

DateTime _DateLoaded = LoadTheDateFromTheDB();
DateTime _DateToCompareWith = new DateTime(2000, 1, 1, 1, 1, 1, 1);

if (_DateLoaded == _DateToCompareWith)
// unreachable code detected :))

Obviously, the Tick-Count is not equal. How do I get on about saving DateTime
values to a Database. Should I store the ticks instead of the dates? Am I
something missing about the DateTime object and its == operator?

In fact, I'm actually not even interested in the Milliseconds part (and
everything beyond that level of precision). I'd just like to compare two dates
based on their Date (DDMMYYYY) and their Time (HHMMSS) values.

Thanks for any help in advance!

--

kind regards,

matthias

--

I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert...
1
by: Frank Millman | last post by:
Hi all I am using odbc from win32 extensions to connect to MS SQL Server. I use mx.DateTime to handle dates. When I select a datetime column from the database, odbc returns something called a...
1
by: Adam Monsen | last post by:
Say I have the following datetime object: >>> from datetime import datetime >>> d = datetime(2005, 8, 10, 15, 43) I happen to know this is a local time from the Netherlands, so I assume the...
15
by: Fritz Switzer | last post by:
I'd like to have a string assigned the value of a DateTime.AddMinutes(amount) so that the string is formatted in "HH:MM" format. For example: DateTime.Now.AddMinutes(30) returns "00:30" ...
3
by: Andrew S. Giles | last post by:
Hello, I am importing a flat text file, and putting it into a datagrid for display on a form. Currently the users have their dates and times seperated. I have two fields, therefore in the...
5
by: I am Sam | last post by:
I have created this DateTime object and instanced it I think correctly DateTime myClubNow1=new...
5
by: A.M | last post by:
Hi, I know we can assigne year, month and day to a DateTime value by the constructor like Dim dt As DateTime = New DateTime(2003,8,1) How can i assign value by year, month and day? I am...
11
by: Cor Ligthert | last post by:
Hello everybody, Jay and Herfried are telling me every time when I use CDate that using the datetime.parseexact is always the best way to do String to datetime conversions. They don't tell why...
6
by: JFieseler | last post by:
Hi all, in a huge project i have the following problem. I create an object which contains many private members (i know that this is not correct, but it is a single use migration program). The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.