试图使用WCF服务使用LINQ到SQL更新SQL数据库中的单行

本文关键字:SQL 更新 数据库 单行 LINQ WCF 服务 | 更新日期: 2023-09-27 18:07:28

我要做的是创建一个使用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的日销售额中添加大量记录,而我想要的只是一行更新的总数。

任何想法?

试图使用WCF服务使用LINQ到SQL更新SQL数据库中的单行

您应该将DailySale d = new DailySale();移动到使用它的作用域

在foreach语句中每轮添加相同的对象

1)可以将这些行移出foreach:

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

2)第一次插入对象。在接下来的回合中更新相同的对象

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

只能插入一次

 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 ();甚至可以完全移动到最后,这样您只有一个事务

我更改的是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(); 

谢谢你的帮助