Search code examples
c#sql-serverdatetimeado.netsqldbtype

Validating a date to be within the SqlDbType.DateTime range


I want to validate a System.DateTime value before I add it as a parameter to my SqlCommand instance.

The MSDN documentation for the SqlDbType enumeration says:

Date and time data ranging in value from January 1, 1753 to December 31, 9999 to an accuracy of 3.33 milliseconds.

To validate the value, I'm using

public readonly DateTime SqlDateTimeMin = new DateTime(1753, 1, 1);
public readonly DateTime SqlDateTimeMax = new DateTime(9999, 12, 31);

if (value < SqlDateTimeMin || value > SqlDateTimeMax)
    // Validation check fails
else
    // Validation check succeeds

Is this the best way? Is there an alternative to hard coding these min and max values?


Solution

  • What about SqlDateTime.MinValue and SqlDateTime.MaxValue?

    Note: these are SQL type min/max not the .net types like the previous 2 answers :-)