Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MS Access存储了不正确的DateTime.MinValue?_C#_Ms Access_Datetime - Fatal编程技术网

C# MS Access存储了不正确的DateTime.MinValue?

C# MS Access存储了不正确的DateTime.MinValue?,c#,ms-access,datetime,C#,Ms Access,Datetime,在MS Access中将DateTime.MinValue存储到字段(作为日期/时间类型)时出现问题。在存储到数据库之前将其打印出来,我会得到“1/1/0001 12:00:00 AM”,这是预期的结果。然而,在从数据库中检索到相同的值之后,我得到了“1/1/2001 12:00:00 AM”(注意2001年而不是1年) 这显然是错误的!有人有什么想法吗 请注意,我正在使用DateTime.MinValue,正如有人建议的那样,它是无效的日期/时间,因为DateTime不能为空值 要写入数据库的

在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;
    }
publicstaticlist TodaysShifts()
{
列表移位=新列表();
//信息:不打算检索大量记录,因为没有缓存。。。。
OleDbCommand cmd=新OleDbCommand(“选择profiles.profile_id,profiles.full_name,shifts.start,shifts.stop,shifts.start_log,shifts.start_notes,shifts.stop_notes FROM shifts,profiles WHERE(shifts.start>=@today)和(shifts.profile_id=profiles.profile_id);”;
cmd.Parameters.AddWithValue(“@today”,DateTime.today);
OleDbDataReader=Database.Read(cmd);
while(reader.Read())
{
班次。添加(新班次视图)(
reader.GetString(reader.GetOrdinal(“profile_id”),
reader.GetString(reader.GetOrdinal(“全名”),
reader.GetDateTime(reader.GetOrdinal(“开始”)),
reader.GetDateTime(reader.GetOrdinal(“停止”)),
reader.GetDateTime(reader.GetOrdinal(“开始日志”),
reader.GetDateTime(reader.GetOrdinal(“停止日志”),
reader.GetString(reader.GetOrdinal(“开始注释”),
reader.GetString(reader.GetOrdinal(“停止注释”))
));
}
返回班次;
}

正如Jon Skeet所建议的:-

基本上,不要使用DateTime.MinValue来表示缺少的值。 不能在SQL Server日期时间字段中使用DateTime.MinValue作为SQL 服务器的最小值为1753开头

相反,将您的属性设置为可空(aka) DateTime?),并在没有值时将其设置为null。也使 确保数据库字段可为空。那你只需要确定一下 该null在数据库中最终成为null值。到底怎么做 这取决于您的数据访问权限


您可以尝试使用SqlDateTime.MinValue代替Jon Skeet建议的DateTime.MinVaue

:-

基本上,不要使用DateTime.MinValue来表示缺少的值。 不能在SQL Server日期时间字段中使用DateTime.MinValue作为SQL 服务器的最小值为1753开头

相反,将您的属性设置为可空(aka) DateTime?),并在没有值时将其设置为null。也使 确保数据库字段可为空。那你只需要确定一下 该null在数据库中最终成为null值。到底怎么做 这取决于您的数据访问权限


您还可以尝试在Microsoft Access中使用SqlDateTime.MinValue而不是DateTime.MinVaue“有效的日期值范围从-657434(公元100年1月1日)到2958465(公元9999年12月31日)。有效的时间值范围从.0到.9999,或23:59:59。”(参考:)因此,无法存储Access
日期/时间字段中的“1/1/0001 12:00:00 AM”。

Microsoft Access中的有效日期值范围为-657434(公元100年1月1日)到2958465(公元9999年12月31日)。有效时间值的范围为.0到.9999或23:59:59。“(参考:)因此,您不能存储“1/1/0001 12:00:00 AM”“在Access
日期/时间
字段中。

感谢您的帮助!最好的解决方法是使用单独的年、月和日字段,有效值为月1-12和日1-31。不能阻止无效日期,但比字符串更好。谢谢您的帮助!最好的解决方法是使用单独的年、月和日字段,有效值为月1-12和日1-31。不停止无效日期,但优于字符串。