MS Access存储了错误的DateTime.MinValue

本文关键字:DateTime MinValue 错误 Access 存储 MS | 更新日期: 2023-09-27 18:16:34

我在MS Access中将DateTime.MinValue存储到字段(作为日期/时间类型)时遇到问题。在将其存储在数据库之前打印出来,我得到"1/1/0001 12:00:00 AM",这是预期的。然而,在从数据库中检索相同的值后,我得到"1/1/2001 12:00:00 AM"(注意2001年而不是1年)。

这显然是不正确的!有人有什么想法吗?

注意,我使用的是DateTime。MinValue作为无效的日期/时间,因为DateTime不能为空值。

写入数据库的代码是:

    public static bool StartNow(string profileID, string startNotes)
    {
        DateTime now = DateTime.Now;
        OleDbCommand cmd = new OleDbCommand("SELECT * FROM shifts WHERE profile_id=@profile_id AND closed=false;");
        cmd.Parameters.AddWithValue("@profile_id", profileID);
        // A new shift should NEVER be started with another open shift.
        OleDbDataReader reader = Database.Read(cmd);
        if (reader.HasRows)
            return false;
        cmd = new OleDbCommand("INSERT INTO shifts(profile_id, start, stop, start_log, stop_log, start_notes, stop_notes, closed) VALUES(@profile_id, @start, @stop, @start_log, @stop_log, @start_notes, @stop_notes, @closed);");
        cmd.Parameters.AddWithValue("@profile_id", profileID);
        cmd.Parameters.AddWithValue("@start", RoundUp(now, 30).ToString());
        cmd.Parameters.AddWithValue("@stop", DateTime.MinValue);
        cmd.Parameters.AddWithValue("@start_log", now.ToString());
        cmd.Parameters.AddWithValue("@stop_log", DateTime.MinValue);
        cmd.Parameters.AddWithValue("@start_notes", startNotes);
        cmd.Parameters.AddWithValue("@stop_notes", "");
        cmd.Parameters.AddWithValue("@closed", false);
        // TODO: need to set default values for stop, stop_log and stop_notes
        return Database.Write(cmd) == 1 ? true : false;
    }

这是回读日期和时间的代码:

    public static List<ShiftView> TodaysShifts()
    {
        List<ShiftView> shifts = new List<ShiftView>();
        // INFO: Not intended to retrieve a lot of records as there is no caching....
        OleDbCommand cmd = new OleDbCommand("SELECT profiles.profile_id, profiles.full_name, shifts.start, shifts.stop, shifts.start_log, shifts.stop_log, shifts.start_notes, shifts.stop_notes FROM shifts, profiles WHERE (shifts.start>=@today) AND (shifts.profile_id=profiles.profile_id);");
        cmd.Parameters.AddWithValue("@today", DateTime.Today);
        OleDbDataReader reader = Database.Read(cmd);
        while(reader.Read())
        {
            shifts.Add(new ShiftView(
                reader.GetString(reader.GetOrdinal("profile_id")), 
                reader.GetString(reader.GetOrdinal("full_name")),
                reader.GetDateTime(reader.GetOrdinal("start")),
                reader.GetDateTime(reader.GetOrdinal("stop")),
                reader.GetDateTime(reader.GetOrdinal("start_log")),
                reader.GetDateTime(reader.GetOrdinal("stop_log")),
                reader.GetString(reader.GetOrdinal("start_notes")),
                reader.GetString(reader.GetOrdinal("stop_notes"))
                ));
        }
        return shifts;
    }

MS Access存储了错误的DateTime.MinValue

在Microsoft Access "有效的日期值范围从-657,434(公元100年1月1日)到2,958,465(公元9999年12月31日)。有效的时间取值范围是0.0 ~。9999或23:59:59。 (ref: here.)因此,您不能在Access Date/Time字段中存储"1/1/0001 12:00:00 AM"。

根据Jon Skeet的建议:-

基本上,不要使用DateTime。MinValue表示缺失值。你不能使用DateTime。SQL Server DateTime字段中的MinValue,如SQL 服务器的最小起始值为1753。

相反,使您的属性为Nullable(即),并在没有值时将其设置为null。也使确保数据库字段是可空的。那你只需要确保那个null在数据库中以null值结束。究竟是如何这取决于你的数据访问权限。

,你可以尝试使用SqlDateTime。MinValue代替DateTime。MinVaue