开发者

how to use datetime minvalue in get/set method when null is the database value

I have two functions, in my constructor I read from the database and call all of my set functions:

while (reader.Read())
{
    Status = reader["status"].ToString();
    Event_Start_Date1 = reader.GetDateTime(reader.GetOrdinal("event_start_date1"));
}

my get and set method for status works fine. for th开发者_如何学编程e date field however, i get an error because sometimes the field coming from the database is a NULL value. how would i modify my method to assign the minvalue incase the database returns a NULL?

public DateTime Event_Start_Date1
{
    get
    { return event_start_date1; }
    set
    { event_start_date1 = value; }
}


Use DataReader.IsDBNull to check if the value is null:

Event_Start_Date1 = reader.IsDBNull("event_start_date1") ? DateTime.MinValue : 
reader.GetDateTime(reader.GetOrdinal("event_start_date1"));

The above sentence is in the following format:

Condition ? accepted action : rejected action


Check for DBNull and based on that you can assign MinValue.

int ordinal = reader.GetOrdinal("event_start_date1");
Event_Start_Date1 = reader.IsDBNull(ordinal)? DateTime.MinValue: reader.GetDateTime(ordinal);


Wrap the logic in a method:

private static DateTime GetDateTimeValue(SqlDataReader reader, string fieldName)
{
    int ordinal = reader.GetOrdinal(fieldName);
    return reader.IsDBNull(ordinal) ? reader.GetDateTime(ordinal) : DateTime.MinValue;
}

...and call it:

Event_Start_Date1 = GetDateTimeValue(reader, "event_start_date1");

A more general and reusable approach could be to make it into an extension method that can (optionally) take a default value as input:

public static class SqlDataReaderExtensions
{
    public static DateTime GetDateTimeValue(this SqlDataReader reader, 
        string fieldName)
    {
        return GetDateTimeValue(reader, fieldName, DateTime.MinValue);
    }

    public static DateTime GetDateTimeValue(this SqlDataReader reader, 
        string fieldName, DateTime defaultValue)
    {
        int ordinal = reader.GetOrdinal(fieldName);
        return reader.IsDBNull(ordinal) 
            ? reader.GetDateTime(ordinal) 
            : defaultValue;
    }
}

This assumes that you cannot change the model. If you have that option, go with a Nullable<DateTime> instead, and have the GetDateTimeValue method default to returning null.


Use a nullable DateTime:

public DateTime? EventStartDate { get; set; }

Which is similar to:

public Nullable<DateTime> EventStartDate { get; set; }

Read more about Nullable Types

To get the default value of DateTime, you can use either DateTime.MinValue or default(DateTime):

while (reader.Read())
{
    Status = reader["status"].ToString();
    var startDate = reader.GetOrdinal("event_start_date1");
    EventStartDate = if reader.IsDBNull(startDate) ? 
                        reader.GetDateTime(ordinal) :
                        default(DateTime);
}

Essentially, DateTime is a value-type, not a reference type, and there can't be a null DateTime (or whatever other Value Type) compile-time variable, in order for it to be capable to hold a null value it has to be wrapped in the Nullable<T>).
In .NET all the non-nullable types have a default value which can be obtained via the default keyword (e.g. default(DateTime)).


you can use a try-catch

while (reader.Read())
{
    Status = reader["status"].ToString();
    try{
    Event_Start_Date1 = reader.GetDateTime(reader.GetOrdinal("event_start_date1"));
    }
catch{
    Event_Start_Date1 = //some other thing
}

or you can use GetSqlDateTime()

while (reader.Read())
{
    Status = reader["status"].ToString();
    Event_Start_Date1 = reader.GetSqlDateTime(reader.GetOrdinal("event_start_date1"));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜