2012-07-06 3 views
1

上午,查詢不更新數據庫

我有一個問題,我的一些代碼... 基本上我試圖更新或插入到數據庫中。第一條if語句是否適用於何時添加新產品。其他人應該更新任何現有的產品。

但是,當我運行它,它不更新數據庫中的現有產品。但是,設置準備更新的項目。有任何想法嗎?

非常感謝......

using (aboDataDataContext dc = new aboDataDataContext()) 
      { 
       foreach (abcProduct p in abcProducts) 
       { 

        var match = (from t in dc.abcProducts 
           where t.sku == p.productcode 
           select t).FirstOrDefault(); 

        if (match == null) 
        { 

         // Watch out here; there is some type conversion required for certain fields! 
         abcProduct prod = new abcProduct(); 

         prod.sku = p.productcode; 
         prod.categoryId = dc.Categories.Single(c => c.Name == p.category).Id; 
         prod.title = p.name; 
         prod.brand = p.manufacturer; 
         prod.description = p.description; 
         prod.abcPrice = p.price; 
         prod.size = decimal.TryParse(p.size.Replace("cl", ""), out size) == true ? (int?)size : null; 
         prod.country = p.country; 
         prod.region = p.region; 
         prod.vintage = int.TryParse(p.vintage, out vintage) == true ? (int?)vintage : null; 
         prod.weight = Convert.ToDecimal("1.50"); 
         prod.strength = p.strength; 
         prod.bottler = p.bottler; 
         prod.age = int.TryParse(p.age, out age) == true ? (int?)age : null; 
         prod.caskType = p.casktype; 
         prod.series = p.series; 
         prod.flavour = p.flavour; 
         if (p.freestock <= 0) { prod.stock = 0; } //check to see if stock is 0 
          else { prod.stock = p.freestock; } 
         prod.abcUpdated = false; 
         prod.stockUpdated = false; 
         prod.priceUpdated = false; 
         prod.pricePublished = false; 
         prod.stockPublished = false; 
         prod.imgPublished = false; 
         prod.prodPublished = false; 
         prod.lastUpdated = DateTime.Now; 

         // Add the new object to the abcProducts table (only in memory here) 
         dc.abcProducts.InsertOnSubmit(prod); 
        } 
        else 
        { 
         // update row 
         match.abcUpdated = true; 
         //Set if an item has been updated or not. 
         if (match.stock == p.freestock) { match.stockUpdated = false; } 
         else { match.stockUpdated = true; } 
         if (match.abcPrice == p.price) { match.priceUpdated = false; } 
         else { match.priceUpdated = true;} 
         match.sku = p.productcode; 
         match.categoryId = dc.Categories.Single(c => c.Name == p.category).Id; 
         match.title = p.name; 
         match.brand = p.manufacturer; 
         match.description = p.description; 
         match.stock = p.freestock; 
         match.abcPrice = p.price; 
         match.size = decimal.TryParse(p.size.Replace("cl", ""), out size) == true ? (int?)size : null; 
         match.weight = Convert.ToDecimal("1.50"); 
         match.country = p.country; 
         match.region = p.region; 
         match.vintage = int.TryParse(p.vintage, out vintage) == true ? (int?)vintage : null; 
         match.strength = p.strength; 
         match.bottler = p.bottler; 
         match.age = int.TryParse(p.age, out age) == true ? (int?)age : null; 
         match.caskType = p.casktype; 
         match.series = p.series; 
         match.flavour = p.flavour; 
         if (p.freestock <= 0) { match.stock = 0; } //check to see if stock is 0 
          else { match.stock = p.freestock; } 
         match.abcUpdated = true; 
         match.pricePublished = false; 
         match.stockPublished = false; 
         match.imgPublished = false; 
         match.prodPublished = false; 
         match.lastUpdated = DateTime.Now; 
        } 
       } 

       // Finally, request Linq to perform the database updates. 
       dc.SubmitChanges(); 
      } 
      return null; 
     } 
+0

你說,這是插入新行,但不更新現有的? – Habib 2012-07-06 10:31:31

+0

@ Habib.OSU是的,這是正確的。基本上只有1件物品可能需要更新,如價格或庫存。然而,無論是更新,但值例如傳遞給「match.stock」。 我只是不能解決它爲什麼不更新,因爲它應該。 – thatuxguy 2012-07-06 10:33:24

+1

我也有過這個問題! – 2012-07-06 10:35:56

回答

3

有一個在你的代碼的問題,您是通過產品表迭代,並在match變量中獲得的值。在if語句中匹配不爲空的部分,您將該對象設置爲新值,但是您不調用dc.SubmitChanges();,該對象match未被存儲在代碼中的任何位置,並且在下一次迭代中該循環,它正在被分配新的值。

您需要調用dc.SubmitChanges();更新匹配值後。

foreach (abcProduct p in abcProducts) 
       { 

        var match = (from t in dc.abcProducts 
           where t.sku == p.productcode 
           select t).FirstOrDefault(); 

        if (match == null) 
        { 
         //insertion code commented out 
         dc.abcProducts.InsertOnSubmit(prod); 
        } 
        else 
        { 
         ///.... setting up all fields. 
         match.stockPublished = false; 
         match.imgPublished = false; 
         match.prodPublished = false; 
         match.lastUpdated = DateTime.Now; 
         dc.SubmitChanges(); //Call this otherwise you will 
              //loose the match values in the next iteration 
        } 
       } 
+0

現在測試:)我認爲這可能是這樣的事情,但我想我會仔細檢查心靈的安寧 – thatuxguy 2012-07-06 10:38:31

+0

你是一個明星!那就是它!非常感謝:D – thatuxguy 2012-07-06 10:41:07

+0

@thatuxguy,不客氣 – Habib 2012-07-06 10:41:20

4

設置匹配時,上下文丟失對象的跟蹤。

在else語句的底部插入

dc.abcProducts.Attach(match); 
+0

與此,我只需要一個dc.SubmitChanges();仍然? – thatuxguy 2012-07-06 10:42:00

+0

這是正確的。 – 2012-07-06 10:43:54

+0

mmm,或許一次提交一次會比一次提交更好。 – thatuxguy 2012-07-06 10:47:34