public void Post()
        {
            string _frmTblData = string.Empty;

            //retrive data from another app using httpClient
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:5000");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var response = client.GetAsync("api/values/").Result;
                if (response.IsSuccessStatusCode)
                {
                    _frmTblData = response.Content.ReadAsStringAsync().Result;
                }
            }

            //get data from Table toTbl and store as json
            string _toTblData = String.Empty;

            using (To_Context db = new To_Context())
            {
                var _tblToDataC = db.tb_data.ToList();
                _toTblData = JsonConvert.SerializeObject(_tblToDataC, Formatting.None);
            }

            // convert both json string as list items
            var _lstToTbl   = JsonConvert.DeserializeObject <List <Items> >(_toTblData);
            var _lstFromTbl = JsonConvert.DeserializeObject <List <Items> >(_frmTblData);

            //count the items in both lists
            int _totlItemToTbl   = _lstToTbl.Count();
            int _totlItemFromTbl = _lstFromTbl.Count();

            //check count of both table if not add new records
            if (_totlItemToTbl != _totlItemFromTbl)
            {
                for (int i = _totlItemToTbl; i < _totlItemFromTbl; i++)
                {
                    using (To_Context db = new To_Context())
                    {
                        var book = new Book {
                            Books = _lstFromTbl[i].Books, Authors = _lstFromTbl[i].Authors
                        };
                        db.Add(book);
                        db.SaveChanges();
                    }
                }
            }
        }
Пример #2
0
        public static void ex()
        {
            //gether data from tbl_from
            DataTable ds = new DataTable();

            using (var context = new From_Context())
            {
                ds.Columns.Add("Books");
                ds.Columns.Add("Authors");

                foreach (var au in context.tb_data)
                {
                    // ds.Rows.Add
                    ds.Rows.Add(au.Books, au.Authors);
                }
                frmTbl_last_record = ds.Rows[(ds.Rows.Count - 1)][0].ToString();
                // Console.WriteLine("frm:" + frmTbl_last_record);
            }
            //gather data from tbl_to
            DataTable ds2 = new DataTable();

            using (var context = new To_Context())
            {
                ds2.Columns.Add("Books");
                ds2.Columns.Add("Authors");

                foreach (var au in context.tb_data)
                {
                    // ds.Rows.Add
                    ds2.Rows.Add(au.Books, au.Authors);
                }
                toTbl_last_rows = ds2.Rows.Count;
                if (ds2.Rows.Count != 0)
                {
                    toTbl_last_record = ds2.Rows[(ds2.Rows.Count - 1)][0].ToString();
                }
                // Console.WriteLine("to:" + toTbl_last_record);
                //   Console.WriteLine("l row:" + toTbl_last_rows);
            }



            if (frmTbl_last_record != toTbl_last_record)
            {
                using (var context = new To_Context())
                {
                    var book = new Book {
                    };
                    int i;
                    for (i = toTbl_last_rows; i <= ds.Rows.Count - 1; i++)
                    {
                        book = new Book {
                            Books = ds.Rows[i][0].ToString(), Authors = ds.Rows[i][1].ToString()
                        };

                        context.Add(book);
                        context.SaveChanges();
                        toTbl_last_record = ds.Rows[i][0].ToString();
                        Console.WriteLine("Data Changes Made New Record added Succesfully");
                    }
                }
            }
        }