Beispiel #1
0
        public static void loadbndata(bndata bndata)
        {
            // receive bndata, load to database
            using (LexileTitlesEntities lr = new LexileTitlesEntities())
            {
                List<BarnesAndNoble> existing = new List<BarnesAndNoble>(from b in lr.BarnesAndNobles
                                                                         where b.Isbn13 == bndata.isbn13
                                                                         select b);

                if (existing.Count == 0)
                {
                    // create new BarnesAndNoble
                    BarnesAndNoble bn = new BarnesAndNoble();
                    bn.Isbn13 = bndata.isbn13;
                    bn.AgeHigh = bndata.agehigh;
                    bn.AgeLow = bndata.agelow;
                    bn.Rating = bndata.rating;

                    lr.BarnesAndNobles.Add(bn);
                    lr.SaveChanges();
                }
                else
                {
                    // update fields on existing BarnesAndNoble
                    existing[0].AgeHigh = bndata.agehigh;
                    existing[0].AgeLow = bndata.agelow;
                    existing[0].Rating = bndata.rating;

                    lr.SaveChanges();
                }
            }
        }
        public static string loadbndata(BarnesDto bndata)
        {
            string msg = "(Unable to load to local db)";
            // receive bndata, load to database
            try
            {
                using (LexileTitlesEntities lr = new LexileTitlesEntities())
                {
                    List<BarnesAndNoble> existing = new List<BarnesAndNoble>(from b in lr.BarnesAndNobles
                                                                             where b.Isbn13 == bndata.Isbn13
                                                                             select b);

                    if (existing.Count == 0)
                    {
                        // create new BarnesAndNoble
                        BarnesAndNoble bn = new BarnesAndNoble();
                        bn.Isbn13 = bndata.Isbn13;
                        bn.AgeHigh = bndata.BarnesAgeOld;
                        bn.AgeLow = bndata.BarnesAgeYoung;
                        bn.Rating = (decimal)bndata.BarnesAvg;

                        lr.BarnesAndNobles.Add(bn);
                        lr.SaveChanges();

                        msg = "(Loaded new BN to DB)";
                    }
                    else
                    {
                        // update fields on existing BarnesAndNoble
                        existing[0].AgeHigh = bndata.BarnesAgeOld;
                        existing[0].AgeLow = bndata.BarnesAgeYoung;
                        existing[0].Rating = (decimal)bndata.BarnesAvg;

                        lr.SaveChanges();

                        msg = "(Updated existing BN in DB)";
                    }
                }
            }

            catch { }

            return msg;
        }