【问题标题】:Trying to update single row in SQL database using LINQ to SQL using a WCF service尝试使用 WCF 服务使用 LINQ to SQL 更新 SQL 数据库中的单行
【发布时间】:2013-04-08 21:00:48
【问题描述】:

我要做的是创建一种方法,使用 WCF 服务通过一个数据库(事务)从具有今天日期的表中获取所有内容,然后将它们添加到我的每日销售表中,该表将有一行显示利润、每日收入、费用等的日期。

我试过这样做

        public void CalculateProfit(string Date)
        {

            decimal takings = 0;// not needed
            decimal Expenses = 0;// not needed
            using (transactionClassDataContext cont = new transactionClassDataContext())
            { 
                int counter = 0;
                DailySale d = new DailySale();
                var query = (from q in cont.DailySales where q.Date.Equals(Date) select q);
                var query2 = (from r in cont.Transactions where r.Date.Equals(Date) select r);
                foreach (var z in query)
                {
                    counter++;

                }
                if (counter>0)
                {

                        foreach (var y in query2)
                        {

                            takings = takings + y.Price;
                            Expenses = Expenses + 0;

                            d.Expenses += 0;
                            d.Takings += y.Price;
                            d.Profit = d.Takings - d.Expenses;
                            d.Date = Date;

                            cont.DailySales.InsertOnSubmit(d);// update the value
                            cont.SubmitChanges();

                        }              
                }
                else
                {

                    d.Date = Date;
                    cont.DailySales.InsertOnSubmit(d);// if there isnt an entry for todays date, add one
                    cont.SubmitChanges();
                }

            }

        }


    }
}

但它所做的只是抛出这个错误“无法添加已经存在的实体。”

大多数类似的问题都说我需要在 foreach 中创建一个 d 的新实例,但似乎所做的只是将大量记录添加到 m 每日销售额中,而我想要的只是一行更新总数。

有什么想法吗?

【问题讨论】:

    标签: c# linq-to-sql wcf-data-services


    【解决方案1】:

    您应该将DailySale d = new DailySale(); 移动到使用它的范围内。

    【讨论】:

    • 如果您阅读了这个问题,您可能会注意到您的回答并没有解决我的问题。
    【解决方案2】:

    您正在为 foreach 语句中的每个转弯添加相同的对象。

    1) 您可以将此行移到 foreach 之外:

                        cont.DailySales.InsertOnSubmit(d);// update the value
                        cont.SubmitChanges();
    

    2) 在第一轮插入对象。在以下轮次中更新相同的对象。

                        cont.UpdateObject(d);
                        cont.SaveChanges();
    

    【讨论】:

    • UpdateObject 不是一个选项,它不会弹出...有什么建议吗?
    【解决方案3】:

    你应该只插入一次

     using (transactionClassDataContext cont = new transactionClassDataContext())
                { 
                    int counter = 0;
                    DailySale d = new DailySale();
                    cont.DailySales.InsertOnSubmit(d);// **INSERT** the value
    

    并更新其余时间

                var query = (from q in cont.DailySales where q.Date.Equals(Date) select q);
                var query2 = (from r in cont.Transactions where r.Date.Equals(Date) select r);
                foreach (var z in query)
                {
                    counter++;
    
                }
                if (counter>0)
                {
    
                        foreach (var y in query2)
                        {
    
                            takings = takings + y.Price;
                            Expenses = Expenses + 0;
    
                            d.Expenses += 0;
                            d.Takings += y.Price;
                            d.Profit = d.Takings - d.Expenses;
                            d.Date = Date;
    
    
                            cont.SubmitChanges(); // ***  left it here but better move it outside the foreach, 
    
                        }              
    

    `cont.SubmitChanges();甚至可以完全移动到最后,因此您只有一笔交易。

    【讨论】:

      【解决方案4】:

      原来我是在更改 d(每日销售表)而不是更改“var r”

                  DailySale d = new DailySale();
                  var query = (from q in cont.DailySales where q.Date.Equals(Date) select q);
      
      
                  foreach (var z in query)
                  {
                      counter++;
                  }
      
      
                  if (counter>0)
                  {
                      foreach (var r in query)
                      {
      
                          r.Takings = r.Takings + (decimal)price; //  here i was using d instead of r
                          r.Profit = r.Takings - r.Expenses;
                      }
                      cont.SubmitChanges(); 
      

      感谢您的帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-11
        • 1970-01-01
        • 2011-09-17
        • 2012-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-23
        相关资源
        最近更新 更多