Re: DateTime
- From: "henry.lee.jr@xxxxxxxxx" <henry.lee.jr@xxxxxxxxx>
- Date: Thu, 3 Sep 2009 11:21:27 -0700 (PDT)
On Sep 3, 2:05 pm, Joe Cool <joecool1...@xxxxxxxx> wrote:
On Sep 3, 12:00 pm, shapper <mdmo...@xxxxxxxxx> wrote:
Hello,
On a SQL table I have a Date field.
When I don't set a date field what value is created and that is its
interpretation?
Can I create the date from my C# code using:
DateTime LastLogin = new DateTime();
Basically LastLogin holds the last moment the user has login but if
the user didn't login yet I need to set some date that informs be of
that.
This is what I am looking to determine.
If the SQL Column is set to "NOT NULL", then SQL will not let you
store an empty value. You MUST specify a valid value for the DateTime
datatype, or it simply will not store the row. The value is totally up
to you. If you must content with NOT NULL column, then you will need
to decide what value you may want to use that signal's "no value has
been set yet". Probably the minimum allowed value for the DateTime
datatype.
If the column is defined as "NULL" and you do not specify a value, the
column will contain the null value.
In C#, you can use MinValue() (i.e. DateTime LoginTime =
DateTime.MinValue()). Then later in code you can check for
DateTime.MinValue() as you would check for null, as such:
if(LoginTime == DateTime.MinValue())
{
// Code for no login
}
else
{
// User has logged in at some point
}
As for the database side, typically SQL Server will store the value as
0 (all dates are actually stored behind the scenes as numbers), and
the 0 gets converted to whatever the machine-specific settings for the
minimum date time value. At the last place I worked at, for example, a
DATETIME field that was null was the equivalent of 1/1/1900 I believe.
Mapping between the two may be harder, as you might have to do
something where you store the database min value in a config file, and
then compare it
LoginValue = [code to pull DateTime value from database];
if (LoginValue = Properties.Settings.Default.SQLServerMinDateValue)
{
// code to handle a null/zero
}
.
- References:
- DateTime
- From: shapper
- Re: DateTime
- From: Joe Cool
- DateTime
- Prev by Date: Re: DateTime
- Next by Date: Re: Distorted image when scrolling in DoubleBufferPanel
- Previous by thread: Re: DateTime
- Index(es):
Relevant Pages
|