c# - MS Access 存储的 DateTime.MinValue 不正确?

标签 c# ms-access datetime

DateTime.MinValue 存储到 MS Access 中的字段(作为日期/时间类型)时遇到问题。在存储到数据库之前打印出来,我得到“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;
    }

最佳答案

在 Microsoft Access 中“有效日期值范围为 -657,434(公元 100 年 1 月 1 日)到 2,958,465(公元 9999 年 12 月 31 日)。有效时间值范围为 0.0 到 0.9999,即 23:59: 59."(引用:here 。)因此,您无法在 Access Date/Time 字段中存储“1/1/0001 12:00:00 AM”。

关于c# - MS Access 存储的 DateTime.MinValue 不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25159825/

相关文章:

java - 将字符串转换为 iso 日期格式

java - 为什么此代码会生成错误 : "unparseable date"

c# - 注销主窗体并显示登录窗体

c# - !Regex.IsMatch无法使用csharp以窗口形式工作

c# - 如何使用 USB 和/或 WPD 将文件从 Android 设备传输到 PC

java - C# 匿名接口(interface)实现

ms-access - 如何判断表单是否作为子窗口启动

ms-access - Microsoft Access 数据库不显示“数据库工具”选项卡

c# - 数据库/数据网格上的插入和更新语法错误

c# - 使用 System.Text.Json 反序列化 Json 时间戳