【问题标题】:MS Access stores the incorrect DateTime.MinValue?MS Access 存储了不正确的 DateTime.MinValue?
【发布时间】:2014-08-06 21:49:48
【问题描述】:

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;
    }

【问题讨论】:

    标签: c# ms-access datetime


    【解决方案1】:

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

    【讨论】:

    • 感谢您的帮助!
    • 最好的方法是使用单独的年、月和日字段,月份的有效值是 1-12,日的有效值是 1-31。不会停止无效日期,但比字符串更好。
    【解决方案2】:

    按照 Jon Skeet 的建议:-

    基本上,不要使用 DateTime.MinValue 来表示缺失值。 您不能在 SQL Server DateTime 字段中使用 DateTime.MinValue,因为 SQL 服务器的最小值为 1753 开头。

    相反,将您的属性设为 Nullable(又名 DateTime?),并在没有值时将其设置为 null。也使 确保您的数据库字段可以为空。然后你只需要确保 该 null 最终在数据库中作为 NULL 值。具体如何 你这样做取决于你的数据访问权限。

    您可以尝试使用 SqlDateTime.MinValue 而不是 DateTime.MinVaue

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      相关资源
      最近更新 更多