Using SQL Server 2008 TIME column in class declara

2020-04-21 04:49发布

I hope this makes sense, I am trying to store a SQL Server 2008 time(7) datatype in an object.

I am using the below to read the data into the object, but it is crashing out on the line

timeStart = reader.GetDateTime(reader.GetOrdinal("timeStart"))

Other than changing my database field to a datetime, can anyone advise on a solution

My class declaration is:

public DateTime timeStart { get; set; }
public DateTime timeEnd { get; set; }

SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
    Schedules sched = new Schedules()
    {
        Name = reader.GetString(reader.GetOrdinal("Name")),
        timeStart = reader.GetDateTime(reader.GetOrdinal("timeStart")),
        timeEnd = reader.GetDateTime(reader.GetOrdinal("timeEnd")),
        IntervalMinutes = reader.GetInt32(reader.GetOrdinal("IntervalMinutes"))
    };
    schedules.Add(sched);
}

1条回答
叛逆
2楼-- · 2020-04-21 05:06

Per the documentation, you should be using the TimeSpan type and SqlDataReader.GetTimeSpan.

查看更多
登录 后发表回答