2009-09-29 47 views
2

在我的代碼中,我有多個對象被添加到存儲庫中,我曾嘗試在所有循環的末尾運行一次存儲庫Save()函數,並且在添加每個對象後調用它。但無論哪種方式,當存儲庫.Save()中的db.SubmitChanges()...任何想法時,我仍然會遇到SqlDateTime溢出問題?當使用LINQ to SQL SubmitChanges()時會導致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(); 

回答

4

基本上,這個問題是SQL datetime數據類型開始於1753年1月1日,wheras DateTime.MinValue是1/1/0000(或類似的東西)。所以,如果你不初始化日期屬性,你會得到Sql溢出。

要修復的選項:

a)初始化DateTime somewheres。

b)將sql切換到使用DATETIME2數據類型,該數據類型覆蓋了整個.NET DateTime值範圍。 [SQL 2008 only]

+0

b只適用於SQL Server 2008 – RichardOD 2009-09-29 18:05:06

+0

ah是的,我已經完全忘記了我的表中甚至有一個DateTime字段,這個答案刷新了我的記憶,並解釋了爲什麼,謝謝 – BigOmega 2009-09-29 18:32:57

+0

修復了對RichardOD評論的解答 – 2009-09-29 18:48:38