gpt4 book ai didi

asp.net-mvc - 使用 LINQ to SQL SubmitChanges() 时,什么会导致 SqlDateTime 溢出?

转载 作者:行者123 更新时间:2023-12-02 09:00:42 26 4
gpt4 key购买 nike

在我的代码中,我有多个对象被添加到存储库中,我尝试在所有循环结束时运行一次存储库 Save() 函数,并在添加每个对象后调用它。但无论哪种方式,当存储库中的 db.SubmitChanges() .Save() 时,我仍然会遇到 SqlDateTime 溢出...有什么想法吗?

 SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

Source Error:

Line 155: public void Save()
Line 156: {
Line 157: db.SubmitChanges();
Line 158: }
Line 159:


Source File: C:\inetpub\wwwroot\Models\OrderRepository.cs Line: 157


foreach (string vol in volumes)
{
if (vol != "-1" && vol != "")
{
Product prod = orderRepository.getProductByVolumeID(System.Convert.ToInt32(vol));
ProductVolume pvol = orderRepository.getProductVolumeByID(System.Convert.ToInt32(vol));

OrderProduct oProd = new OrderProduct();
oProd.OrderID = getOrder.OrderID;
oProd.VolumeID = pvol.VolumeID;
orderRepository.AddOrderProduct(oProd);


}
}

foreach (string feat in features)
{
if (feat != "")
{
Product prod = orderRepository.getProductByID(System.Convert.ToInt32(feat));

OrderFeature oFeat = new OrderFeature();
oFeat.OrderID = getOrder.OrderID;
oFeat.ProductID = prod.ProductID;
orderRepository.AddOrderFeature(oFeat);


featuresTotal += prod.UserIntervalPrice;
}

}

totalTotal = volumeTotal + featuresTotal;
orderRepository.Save();

最佳答案

基本上,问题是 Sql DATETIME 数据类型从 1/1/1753 开始,而 DateTime.MinValue 是 1/1/0000(或类似的值)。因此,如果不初始化日期属性,就会出现 Sql 溢出。

修复选项:

a) 在某处初始化 DateTime。

b) 将 sql 切换为使用 DATETIME2 数据类型,该数据类型确实涵盖了 .NET DateTime 值的整个范围。 [仅限 SQL 2008]

关于asp.net-mvc - 使用 LINQ to SQL SubmitChanges() 时,什么会导致 SqlDateTime 溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1494066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com