Compare two datetime values ​​from SQL Server using C #

I want to know how to compare two datetime values ​​that were obtained from sql database and the other is current with C #

+5
source share
9 answers

Be careful when comparing DateTimes generated in C #. A DateTime struct in C # has higher precision than datetime 1 in SQL Server. Therefore, if you create a DateTime in C # (say from DateTime.Now), save it in the database and return it back, this will most likely be different.

For example, the following code:

using(SqlConnection conn = new SqlConnection("Data Source=.;Integrated Security=SSPI"))
using(SqlCommand cmd = new SqlCommand("SELECT @d", conn)){
    DateTime now = DateTime.Now;
    cmd.Parameters.Add(new SqlParameter("@d", now));
    conn.Open();
    DateTime then = (DateTime)cmd.ExecuteScalar();
    Console.WriteLine(now.ToString("yyyy/MM/dd HH:mm:ss.fffffff"));
    Console.WriteLine(then.ToString("yyyy/MM/dd HH:mm:ss.fffffff"));
    Console.WriteLine(then - now);

}

returns the following example.

2009.06.20 12: 28: 23.6115968
2009.06.20 12: 28: 23.6100000
-00: 00: 00.0015968

, , epsilon:

Math.Abs((now - then).TotalMilliseconds) < 3

, , , , , .

:

1 . , " .000,.003 .007 "

+15

(, , , ) DateTime. , :

var foo = DateTime.Parse("01/01/1900");
var bar = DateTime.Now;

var test1 = foo == bar; // false
var test2 = foo != bar; // true
var test3 = foo < bar; // true
var test4 = foo > bar; // false
+7

DateTime.CompareTo.

:

firstDateTime.CompareTo(secondDatetime);

int ,

- .

Zero - .

- .

+6

, , DateTimes :

TimeSpan span = dateTime2 - dateTime1;
if (span == TimeSpan.Zero)
{
    // The times are the same
}

System.Data.SqlTypes.SqlDateTime System.DateTime, echosca .

( ), , , , . - :

if (Math.Abs(span.TotalMilliseconds) < 10.0)
{
    // The times are within the allowed range
}

, , DateTime.CompareTo, .

+5

sql # DateTime, #. MSDN , .

+1

SqlDataReader .NET. ( DataTable/DataSet, ).

SqlDataReader dr = cmd.ExecuteReader();
DateTime dt = dr.GetDateTime(dr.GetOrdinal("someDateTimeColumn"));

:

DateTime otherDate = DateTime.Now;
int compResult = dt.CompareTo(otherDate);

if(compResult > 0) { Console.Write("dt is after otherDate"); }
else if(compResult < 0) { Console.Write("dt is before otherDate"); }
else { Console.Write("dt is equal to otherDate"); }
+1

System.Data.SqlTypes.SqlDateTime System.DateTime .

SqlDateTime 1 1753 31 9999 3,33

DateTime ( .NET Framework) 1 0001 31 9999 100

. System.DateTime, .

Value SqlDateTime ( System.DateTime), .

.

+1

DateTime GreterThen, GreaterThenOrEqual, LesserThen, LesserThenOrEqual operater, Equalty operater.

DateTime dateTime1, dateTime2;
dateTime1 = DateTime.Now;
dateTime2 = //set value from database;

// all this operations are legal
if(dateTime1 == dateTime2){}
if(dateTime1 > dateTime2){}
if(dateTime1 < dateTime2){}
0

I hope you find this article ( DATEDIFF Function Demystified ) useful, although it is datetime specific in SQL, useful for understanding how this is handled on the database side.

0
source

All Articles