DateTime

S

shapper

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.

Thanks,
Miguel
 
B

Bjørn Brox

shapper skrev:
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.
I usually use nullable types for that and you should also accept a null
value in the database.

DateTime? LastLogin;
....
string lText = LastLogin.HasValue ? LastLogin.ToString() : "<never>";
 
S

shapper

shapper skrev:







I usually use nullable types for that and you should also accept a null
value in the database.

DateTime? LastLogin;
...
string lText = LastLogin.HasValue ? LastLogin.ToString() : "<never>";

Yes sometime ago I used it but I am mapping the database using
EntityFramework and it does not use Nullable Types.

But even when the type is nullable I suppose a value is added to the
SQL table or not?

Thanks,
Miguel
 
J

Joe Cool

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.
 
H

henry.lee.jr

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
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top