Exemplo n.º 1
0
        public void GatherData()
        {
            DatabaseConnection dc = new DatabaseConnection();

            dc.LoginToDatabase();
            foreach (var elem in Content)
            {
                if (dc.InsertIntoFilesAndDatesTable(elem.Key, elem.Value))
                {
                    if (elem.Key.StartsWith('c'))
                    {
                        Helper.TraceMessage(elem.Key);

                        XmlDocument doc     = new XmlDocument();
                        string      website = LoadXmlFromWebsite(elem.Key).Result;

                        LoadXmlFromWebsite(elem.Key).Wait();
                        try
                        {
                            doc.LoadXml(website);
                        }
                        catch (XmlException xmlException)
                        {
                            Helper.TraceMessage(xmlException.StackTrace);
                        }
                        XmlNodeList position = doc.SelectNodes("/tabela_kursow/pozycja");
                        foreach (XmlNode xmlNode in position)
                        {
                            string          currCode   = xmlNode.SelectSingleNode("kod_waluty").InnerText;
                            string          currName   = xmlNode.SelectSingleNode("nazwa_waluty").InnerText;
                            double          buyRate    = double.Parse(xmlNode.SelectSingleNode("kurs_kupna").InnerText);
                            double          sellRate   = double.Parse(xmlNode.SelectSingleNode("kurs_sprzedazy").InnerText);
                            int             conversion = int.Parse(xmlNode.SelectSingleNode("przelicznik").InnerText);
                            PopularCurrency pc         = new PopularCurrency(elem.Key, elem.Value, currCode, currName, buyRate, sellRate, conversion);

                            Console.WriteLine("list.InnerText: " + xmlNode.SelectSingleNode("nazwa_waluty").InnerText);
                            Console.WriteLine("list.InnerText: " + xmlNode.SelectSingleNode("przelicznik").InnerText);
                            Console.WriteLine("list.InnerText: " + xmlNode.SelectSingleNode("kod_waluty").InnerText);
                            Console.WriteLine("list.InnerText: " + xmlNode.SelectSingleNode("kurs_kupna").InnerText);
                            Console.WriteLine("list.InnerText: " + xmlNode.SelectSingleNode("kurs_sprzedazy").InnerText);

                            dc.InsertIntoCurrencyTable(pc);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Record was already added");
                }
            }
            dc.CloseConnectionToDatabase();
        }
Exemplo n.º 2
0
        public void InsertIntoCurrencyTable(PopularCurrency popularCurrency)
        {
            // Start a local transaction.
            MySqlCommand     command = connection.CreateCommand();
            MySqlTransaction transaction;

            transaction = connection.BeginTransaction(System.Data.IsolationLevel.Serializable);
            try
            {
                command.CommandText =
                    "insert into currency.popular_buy_and_sell(id, date, currency_code, currency_name, buy_rate, sell_rate, conversion) values " +
                    "(\"" + popularCurrency.Id + "\"" + ", " +
                    "\'" + popularCurrency.Date.Year + "-" + popularCurrency.Date.Month + "-" + popularCurrency.Date.Day + "-" + "\'" + ", " +
                    "\"" + popularCurrency.CurrencyCode + "\"" + ", " +
                    "\"" + popularCurrency.CurrencyName + "\"" + ", " +
                    +popularCurrency.BuyRate + ", " +
                    +popularCurrency.SellRate + ", " +
                    +popularCurrency.Conversion +
                    ")";
                command.ExecuteNonQuery();

                try
                {
                    transaction.Commit();
                }
                catch (Exception commitException)
                {
                    Helper.TraceMessage("Commit Exception Type: " + commitException.GetType());
                    Helper.TraceMessage("  Message: " + commitException.Message);
                }

                Helper.TraceMessage("Records was written to database.");
            }
            catch (Exception ex)
            {
                Helper.TraceMessage("Commit Exception Type: " + ex.GetType());
                Helper.TraceMessage("  Message: " + ex.Message);

                try
                {
                    transaction.Rollback();
                }
                catch (Exception transactionException)
                {
                    Helper.TraceMessage("Commit Exception Type: " + transactionException.GetType());
                    Helper.TraceMessage("  Message: " + transactionException.Message);
                }
            }
        }