public decimal GetRate(int valute, DateTime date) { decimal rate; using (RateContext db = new RateContext()) { System.Data.SqlClient.SqlParameter pDate = new System.Data.SqlClient.SqlParameter("@date", date); System.Data.SqlClient.SqlParameter pValute = new System.Data.SqlClient.SqlParameter("@valute", valute); var list = db.Rates.SqlQuery("SELECT * FROM Rates WHERE Rates.date=@date AND NumCode=@valute", pDate, pValute); rate = list.FirstOrDefault().Value; } return(rate); }
public void Download() { WebClient wc = new WebClient(); string adress = "http://www.cbr.ru/scripts/XML_daily.asp?date_req=" + DateTime.Now.Date.ToString(); wc.DownloadFile(adress, "temp.xml"); XDocument xdoc = XDocument.Load("temp.xml"); DateTime today = DateTime.Parse( xdoc.Element("ValCurs").Attribute("Date").Value); foreach (XElement v in xdoc.Element("ValCurs").Elements("Valute")) { using (RateContext db = new RateContext()) { System.Data.SqlClient.SqlParameter pDate = new System.Data.SqlClient.SqlParameter("@date", today); var list = db.Rates.SqlQuery("SELECT * FROM Rates WHERE Rates.date=@date", pDate); if (list.Count() > 0) { return; } Rate rate = new Rate { date = today, NumCode = int.Parse(v.Element("NumCode").Value), CharCode = v.Element("CharCode").Value, Nominal = int.Parse(v.Element("Nominal").Value), Name = v.Element("Name").Value, Value = decimal.Parse(v.Element("Value").Value) }; db.Rates.Add(rate); db.SaveChanges(); } } }