示例#1
0
        private void InsertToDatabase(int shopID, int productID, int quantity, DateTime date)
        {
            var dbConnection = new SQLServerContextFactory().Create();

            using (dbConnection)
            {
                var sale = new Sale();
                sale.Shop = dbConnection.Shops.Find(shopID);
                sale.Product = dbConnection.Products.Find(productID);
                sale.Quantity = quantity;
                sale.SaleDate = date;

                dbConnection.Sales.Add(sale);
                dbConnection.SaveChanges();
            }
        }
示例#2
0
        private static void XMLtoSQL(string filePath)
        {
            try
            {
                XDocument xmlDoc = XDocument.Load(filePath);

                var sales =
                from sale in xmlDoc.Descendants("sale")
                select new
                {
                    pid = int.Parse(sale.Element("productId").Value),
                    sid = int.Parse(sale.Element("shopId").Value),
                    qua = int.Parse(sale.Element("quantity").Value),
                    date = DateTime.Parse(sale.Element("saleDate").Value),
                };

                var sqlServerConnection = new SQLServerContextFactory().Create();

                foreach (var sale in sales)
                {
                    var newSale = new Sale();
                    newSale.Product = sqlServerConnection.Products.Find(sale.pid);
                    newSale.Shop = sqlServerConnection.Shops.Find(sale.sid);
                    newSale.Quantity = sale.qua;
                    newSale.SaleDate = sale.date;
                    sqlServerConnection.Sales.Add(newSale);
                }

                sqlServerConnection.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return;
            }
        }