- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在使用我编写的这段代码,它以最不清楚的方式工作。我希望在数据库中插入一行,其中包含两列日期时间:
myrow.ApprovalDate = DateTime.Now
myrow.ProposedDate = DateTime.Now
然而,当我更新数据库时,我收到此错误:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
我什至尝试从数据库复制插入的值并将其硬编码到正在更新的对象中:
// I copied this value from the DB
myrow.ApprovalDate = Convert.ToDateTime("2008-12-24 00:00:00.000");
仍然是同样的错误,奇怪的是上面的技巧对于第一次插入数据库有效,但从那时起就失败了。知道发生了什么吗?
最佳答案
一个DateTime
在 C# 中是值类型,而不是引用类型,因此不能为 null。但它可以是常数 DateTime.MinValue
超出了 Sql Server 的范围 DATETIME
数据类型。
保证值类型始终具有(默认)值(零),而无需始终需要显式设置(在本例中为 DateTime.MinValue)。
结论是您可能有一个未设置的 DateTime 值,您正在尝试将其传递到数据库。
DateTime.MinValue = 1/1/0001 12:00:00 AM
DateTime.MaxValue = 23:59:59.9999999, December 31, 9999,
exactly one 100-nanosecond tick
before 00:00:00, January 1, 10000
MSDN:DateTime.MinValue
<小时/>关于Sql Server
datetime
Date and time data from January 1, 1753 through December 31, 9999, to an accuracy of one three-hundredth of a second (equivalent to 3.33 milliseconds or 0.00333 seconds). Values are rounded to increments of .000, .003, or .007 secondssmalldatetime
Date and time data from January 1, 1900, through June 6, 2079, with accuracy to the minute. smalldatetime values with 29.998 seconds or lower are rounded down to the nearest minute; values with 29.999 seconds or higher are rounded up to the nearest minute.
MSDN:Sql Server DateTime and SmallDateTime
<小时/>最后,如果您发现自己通过了 C# DateTime
作为 sql 的字符串,您需要将其格式化如下以保留最大精度并防止 sql server 抛出类似的错误。
string sqlTimeAsString = myDateTime.ToString("yyyy-MM-ddTHH:mm:ss.fff");
<小时/>
更新(8年后)
考虑使用sql DateTime2
与 .net 更好地保持一致的数据类型 DateTime
日期范围 0001-01-01 through 9999-12-31
和时间范围 00:00:00 through 23:59:59.9999999
string dateTime2String = myDateTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffff");
关于asp.net - 错误 - SqlDateTime 溢出。必须介于 1/1/1753 12 :00:00 AM and 12/31/9999 11:59:59 PM 之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/468045/
我在 SQL Server Management Studio 中使用了 SQL 语句,我在其中输入了以下日期时间 {ts '9999-12-31 23:59:59'} 的无限有效性,但是当我尝试通过
考虑以下程序: DateTime dateTime = new DateTime(634546165144647370); SqlDateTime sqlDateTime = new SqlDateT
我使用存储过程来获取分页列表,这是我的方法: using(SqlConnection conn = new SqlConnection(_connectionString)) { using(S
我刚刚发现我的 SqlDateTime.MinValue 在不同的机器上是不同的。 有时是 = DateTime(1953, 1, 1, 0, 0, 0); 但在其他机器上它是 = DateTime(
table = {{ID1, SQLDateTime[{1978, 1, 10, 0, 0, 0.`}]}, {ID2, SQLDateTime[{1999, 1, 10, 0, 0, 0.`}]},
我正在映射一个非常简单的 Users 表,并且我有一个名为“LastLoginDate”的列,它在 sql server 中定义为可为空。 我的映射看起来像这样: public Users {
我似乎无法将 sqldatatime.minvalue 转换为 datetime 对象。 我试过: Datetime dt = Convert.ToDateTime(SqlDateTime.MinVa
我想知道,为什么 SqlDateTime.MinValue 与 DateTime.MinValue 不一样? 最佳答案 我认为 SQL 和 .NET 的 Date 数据类型之间的差异源于 SQL Se
我需要用 SqlDateTime.Parse(val) 其中 val 是一个字符串,例如“23.3.1992 00:00:00 ”。 字符串为欧洲格式,即日在月之前。但是 Parse 需要“美国”格式
我在数据库中使用 NHibernate 持久化我的对象 App 对象定义了一个属性: public virtual DateTime ReleaseDate { get; set; } 在映射类中:
我有一个从源(DataRow 或 SqlDataReader)获取日期属性的方法: protected SqlDateTime GetSqlDateTimePropertyValue(string p
到处找都试过了,还是不行。 目前的方法是 DateTime dt = (DateTime)sqlparam.Value;。我可以想象在不同的配置、不同的 SQL 区域设置、不同的 PC 区域设置等
我不想验证 txtBirthDate,所以我想在数据库中传递 DateTime.MinValue。 我的代码: if (txtBirthDate.Text == string.Empty)
在将 .NET DateTime(默认情况下(DateTime))转换为 SqlDateTime 时,我应该始终检查 .NET 日期是否在 SqlDateTime.MinValue 和 SqlDate
当我将 DateTime 变量与 SqlDateTime.MinValue 进行比较时: if (StartDate > SqlDateTime.MinValue) { // some code
System.DateTime可以采用比 SQL Server 的 DateTime 更广泛的值。因此有类 System.Data.SqlTypes.SqlDateTime模仿后者。 因此,我希望 E
我正在开发一个以 NHibernate 作为后端的 ASP.NET MVC 项目,但在将一些日期写回到我的 SQL Server 数据库表中时遇到一些问题。 这些日期字段不可为空,因此此处有关如何设置
我正在用 c#.net 编写一个与 sql DB 一起工作的应用程序。我有 5 个字段 - 日期时间。当我处于 Debug模式时,我看到 DateTime 字段是正确的,但是当我运行应用程序时,我得到
以下代码使用比较 运算符可以正常编译。 If(dateTimeVariable > SqlDateTime.MinValue) //compiles Ok. dateTimeVariable is o
我正在尝试将日期从 C# 保存到 SQL Server。首先要向用户显示日期格式为 dd/MM/yyyy。然后在 Winforms 屏幕上选择日期后。我想将它保存到数据库中。如果我删除代码中的 dat
我是一名优秀的程序员,十分优秀!