private CityVO getCityVO(HtmlParser parser, string dataHtml, string indexCity) { IHtmlDocument doc = parser.Parse(dataHtml); WeatherVO weatherVO; CityVO cityVO = new CityVO(); cityVO.IndexCity = indexCity; cityVO.NameCity = doc.QuerySelector("#weather > div.fcontent > div.section.higher > h2").InnerHtml; cityVO.ImgSrcWeather = doc.QuerySelector("#tab_wdaily2 > img").GetAttribute("src"); cityVO.Day = doc.QuerySelector("#tab_wdaily2 > dl > dd").InnerHtml; cityVO.DayWeek = doc.QuerySelector("#tab_wdaily2 > dl > dt").InnerHtml; cityVO.TemperatureMin = doc.QuerySelector("#tab_wdaily2 > div > span.value.m_temp.c").InnerHtml; cityVO.TemperatureMax = doc.QuerySelector("#tab_wdaily2 > div > em > span.value.m_temp.c").InnerHtml; WeatherVO[] weather = new WeatherVO[4]; for (int i = 0; i < 4; i++) { weatherVO = new WeatherVO(); weatherVO.TimeOfDay = doc.QuerySelector("#tbwdaily2 > tr:nth-child(" + (i + 1) + ") > th:nth-child(1)").InnerHtml.ToString().Trim(); weatherVO.ImgSrcWeather = doc.QuerySelector("#tbwdaily2 > tr:nth-child(" + (i + 1) + ") > td:nth-child(2) > img:nth-child(1)").GetAttribute("src"); weatherVO.FeatureWeather = doc.QuerySelector("#tbwdaily2 > tr:nth-child(" + (i + 1) + ") > td:nth-child(3)").InnerHtml; weatherVO.TemperatureAir = doc.QuerySelector("#tbwdaily2 > tr:nth-child(" + (i + 1) + ") > td:nth-child(4) > span:nth-child(1)").InnerHtml; weatherVO.Pressure = doc.QuerySelector("#tbwdaily2 > tr:nth-child(" + (i + 1) + ") > td:nth-child(5) > span:nth-child(1)").InnerHtml; weatherVO.DirectionWind = doc.QuerySelector("#tbwdaily2 > tr:nth-child(" + (i + 1) + ") > td:nth-child(6) > dl:nth-child(1) > dt:nth-child(1)").InnerHtml; weatherVO.SpeedWind = doc.QuerySelector("#tbwdaily2 > tr:nth-child(" + (i + 1) + ") > td:nth-child(6) > dl:nth-child(1) > dd:nth-child(2) > span:nth-child(1)").InnerHtml; weatherVO.Humidity = doc.QuerySelector("#tbwdaily2 > tr:nth-child(" + (i + 1) + ") > td:nth-child(7)").InnerHtml; weatherVO.TemperatureFeels = doc.QuerySelector("#tbwdaily2 > tr:nth-child(" + (i + 1) + ") > td:nth-child(8) > span:nth-child(1)").InnerHtml; weather[i] = weatherVO; } cityVO.Weather = weather.ToArray(); for (int i = 0; i < 4; i++) { weather[i] = null; } weather = null; doc.Dispose(); doc = null; return(cityVO); }
// вставка данных public void Insert(CityVO cityVO) { //Console.WriteLine("INSERT INTO cities (IdCity, NameCity, ImgSrcWeather, Day, DayWeek, TemperatureMin, TemperatureMax ) VALUES( " + cityVO.IndexCity + "," + cityVO.NameCity + "," + cityVO.ImgSrcWeather + "," + cityVO.Day + "," + cityVO.DayWeek + "," + cityVO.TemperatureMin + "," + cityVO.TemperatureMax + ")"); string query = "INSERT INTO cities (IdCity, NameCity, ImgSrcWeather, Day, DayWeek, TemperatureMin, TemperatureMax, Weather ) VALUES( @IdCity,@NameCity,@ImgSrcWeather,@Day,@DayWeek,@TemperatureMin,@TemperatureMax,@Weather)"; if (this.OpenConnection() == true) { try { using (MySqlCommand cmd = new MySqlCommand(query, connection)) { cmd.Parameters.AddWithValue("@IdCity", cityVO.IndexCity); cmd.Parameters.AddWithValue("@NameCity", cityVO.NameCity); cmd.Parameters.AddWithValue("@ImgSrcWeather", cityVO.ImgSrcWeather); cmd.Parameters.AddWithValue("@Day", cityVO.Day); cmd.Parameters.AddWithValue("@DayWeek", cityVO.DayWeek); cmd.Parameters.AddWithValue("@TemperatureMin", cityVO.TemperatureMin); cmd.Parameters.AddWithValue("@TemperatureMax", cityVO.TemperatureMax); cmd.Parameters.AddWithValue("@Weather", JsonConvert.SerializeObject(cityVO.Weather)); // выполнить с возвратом данных //Object o = cmd.ExecuteScalar(); //Console.WriteLine(cmd.Parameters["@IdCity"].Value); // не возвращать данные cmd.ExecuteNonQuery(); } } catch (MySqlException ex) { Console.WriteLine("Error: {0}", ex.ToString()); } finally { this.CloseConnection(); } } }
//обновление данных public void Update(CityVO cityVO) { string query = "UPDATE cities SET IdCity = @IdCity, NameCity = @NameCity, ImgSrcWeather = @ImgSrcWeather, Day = @Day, DayWeek = @DayWeek, TemperatureMin = @TemperatureMin, TemperatureMax = @TemperatureMax, Weather = @Weather WHERE IdCity= @IdCity"; if (this.OpenConnection() == true) { try { using (MySqlCommand cmd = new MySqlCommand(query, connection)) { cmd.Parameters.AddWithValue("@IdCity", cityVO.IndexCity); cmd.Parameters.AddWithValue("@NameCity", cityVO.NameCity); cmd.Parameters.AddWithValue("@ImgSrcWeather", cityVO.ImgSrcWeather); cmd.Parameters.AddWithValue("@Day", cityVO.Day); cmd.Parameters.AddWithValue("@DayWeek", cityVO.DayWeek); cmd.Parameters.AddWithValue("@TemperatureMin", cityVO.TemperatureMin); cmd.Parameters.AddWithValue("@TemperatureMax", cityVO.TemperatureMax); cmd.Parameters.AddWithValue("@Weather", JsonConvert.SerializeObject(cityVO.Weather)); // выполнить с возвратом данных //Object o = cmd.ExecuteScalar(); //Console.WriteLine(cmd.Parameters["@IdCity"].Value); // не возвращать данные cmd.ExecuteNonQuery(); } } catch (MySqlException ex) { Console.WriteLine("Error: {0}", ex.ToString()); } finally { this.CloseConnection(); } } }