public static void UpdateWeatherToDb(Classes.Weather weather) { string connectionString = Constants.connectionString; using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlCommand command = conn.CreateCommand()) { command.CommandText = "UPDATE Weather SET " + "Q1Temp = @Q1Temp, Q1Hum = @Q1Hum, Q1Rain = @Q1Rain, " + "Q2Temp = @Q2Temp, Q2Hum = @Q2Hum, Q2Rain = @Q2Rain, " + "R1Temp = @R1Temp, R1Hum = @R1Hum, R1Rain = @R1Rain, " + "R2Temp = @R2Temp, R2Hum = @R2Hum, R2Rain = @R2Rain, " + "R3Temp = @R3Temp, R3Hum = @R3Hum, R3Rain = @R3Rain, " + "R4Temp = @R4Temp, R4Hum = @R4Hum, R4Rain = @R4Rain " + "WHERE Id = @Id"; command.Parameters.Clear(); command.Parameters.AddWithValue("@Q1Temp", weather.Q1[(int)Classes.WeatherEnum2.Temp]); command.Parameters.AddWithValue("@Q1Hum", weather.Q1[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@Q1Rain", weather.Q1[(int)Classes.WeatherEnum2.Rain]); command.Parameters.AddWithValue("@Q2Temp", weather.Q2[(int)Classes.WeatherEnum2.Temp]); command.Parameters.AddWithValue("@Q2Hum", weather.Q2[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@Q2Rain", weather.Q2[(int)Classes.WeatherEnum2.Rain]); command.Parameters.AddWithValue("@R1Temp", weather.R1[(int)Classes.WeatherEnum2.Temp]); command.Parameters.AddWithValue("@R1Hum", weather.R1[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@R1Rain", weather.R1[(int)Classes.WeatherEnum2.Rain]); command.Parameters.AddWithValue("@R2Temp", weather.R2[(int)Classes.WeatherEnum2.Temp]); command.Parameters.AddWithValue("@R2Hum", weather.R2[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@R2Rain", weather.R2[(int)Classes.WeatherEnum2.Rain]); command.Parameters.AddWithValue("@R3Temp", weather.R3[(int)Classes.WeatherEnum2.Temp]); command.Parameters.AddWithValue("@R3Hum", weather.R3[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@R3Rain", weather.R3[(int)Classes.WeatherEnum2.Rain]); command.Parameters.AddWithValue("@R4Temp", weather.R4[(int)Classes.WeatherEnum2.Temp]); command.Parameters.AddWithValue("@R4Hum", weather.R4[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@R4Rain", weather.R4[(int)Classes.WeatherEnum2.Rain]); command.Parameters.AddWithValue("@Id", weather.Id); conn.Open(); command.ExecuteNonQuery(); conn.Close(); } } }
public static void SaveWeatherToDb(Classes.Weather weather) { string connectionString = Constants.connectionString; using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlCommand command = conn.CreateCommand()) { command.CommandText = "INSERT INTO Weather VALUES (" + "@Q1Temp, @Q1Hum, @Q1Rain, " + "@Q2Temp, @Q2Hum, @Q2Rain, " + "@R1Temp, @R1Hum, @R1Rain, " + "@R2Temp, @R2Hum, @R2Rain, " + "@R3Temp, @R3Hum, @R3Rain, " + "@R4Temp, @R4Hum, @R4Rain " + ")"; command.Parameters.Clear(); command.Parameters.AddWithValue("@Q1Temp", weather.Q1[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@Q1Hum", weather.Q1[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@Q1Rain", weather.Q1[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@Q2Temp", weather.Q2[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@Q2Hum", weather.Q2[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@Q2Rain", weather.Q2[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@R1Temp", weather.R1[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@R1Hum", weather.R1[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@R1Rain", weather.R1[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@R2Temp", weather.R2[(int)Classes.WeatherEnum2.Temp]); command.Parameters.AddWithValue("@R2Hum", weather.R2[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@R2Rain", weather.R2[(int)Classes.WeatherEnum2.Rain]); command.Parameters.AddWithValue("@R3Temp", weather.R3[(int)Classes.WeatherEnum2.Temp]); command.Parameters.AddWithValue("@R3Hum", weather.R3[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@R3Rain", weather.R3[(int)Classes.WeatherEnum2.Rain]); command.Parameters.AddWithValue("@R4Temp", weather.R4[(int)Classes.WeatherEnum2.Temp]); command.Parameters.AddWithValue("@R4Hum", weather.R4[(int)Classes.WeatherEnum2.Hum]); command.Parameters.AddWithValue("@R4Rain", weather.R4[(int)Classes.WeatherEnum2.Rain]); conn.Open(); command.ExecuteNonQuery(); conn.Close(); } } }
public static Classes.Weather ReadWeatherFromDB() { string connectionString = Constants.connectionString; Classes.Weather weather = new Classes.Weather(); string query = "SELECT * FROM Weather WHERE Id=1"; using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand(query, conn)) { conn.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { weather.Id = int.Parse(reader[0].ToString()); weather.Q1[(int)Classes.WeatherEnum2.Temp] = int.Parse(reader[1].ToString()); weather.Q1[(int)Classes.WeatherEnum2.Hum] = int.Parse(reader[2].ToString()); weather.Q1[(int)Classes.WeatherEnum2.Rain] = int.Parse(reader[3].ToString()); weather.Q2[(int)Classes.WeatherEnum2.Temp] = int.Parse(reader[4].ToString()); weather.Q2[(int)Classes.WeatherEnum2.Hum] = int.Parse(reader[5].ToString()); weather.Q2[(int)Classes.WeatherEnum2.Rain] = int.Parse(reader[6].ToString()); weather.R1[(int)Classes.WeatherEnum2.Temp] = int.Parse(reader[7].ToString()); weather.R1[(int)Classes.WeatherEnum2.Hum] = int.Parse(reader[8].ToString()); weather.R1[(int)Classes.WeatherEnum2.Rain] = int.Parse(reader[9].ToString()); weather.R2[(int)Classes.WeatherEnum2.Temp] = int.Parse(reader[10].ToString()); weather.R2[(int)Classes.WeatherEnum2.Hum] = int.Parse(reader[11].ToString()); weather.R2[(int)Classes.WeatherEnum2.Rain] = int.Parse(reader[12].ToString()); weather.R3[(int)Classes.WeatherEnum2.Temp] = int.Parse(reader[13].ToString()); weather.R3[(int)Classes.WeatherEnum2.Hum] = int.Parse(reader[14].ToString()); weather.R3[(int)Classes.WeatherEnum2.Rain] = int.Parse(reader[15].ToString()); weather.R4[(int)Classes.WeatherEnum2.Temp] = int.Parse(reader[16].ToString()); weather.R4[(int)Classes.WeatherEnum2.Hum] = int.Parse(reader[17].ToString()); weather.R4[(int)Classes.WeatherEnum2.Rain] = int.Parse(reader[18].ToString()); } conn.Close(); } } return(weather); }
private async void Forecast() { string homeCityID; CityToID.Instance.australianCities.TryGetValue(Preferences.Instance.HomeCity, out homeCityID); WeatherAPI api = new WeatherAPI(); IList <IList <WeatherRawData> > WeatherData = api.GetforecastWeather(homeCityID).Result; if (WeatherData == null) { ForeCastProgressRing.IsActive = false; if (api.errType == "Internet") { ErrorText.Text = "No response: Please check your internet connection"; return; } else if (api.errType == "WebsiteError") { ErrorText.Text = "Reponse error: Please contact support for further information"; return; } else if (api.errType == "APIParameterChange") { ErrorText.Text = "Response Error: please contact support for further information"; return; } } LocationText.Text = Preferences.Instance.HomeCity; Classes.Weather DayOne = new Classes.Weather(); Classes.Weather DayTwo = new Classes.Weather(); Classes.Weather DayThree = new Classes.Weather(); Classes.Weather DayFour = new Classes.Weather(); Classes.Weather DayFive = new Classes.Weather(); CalculateWeatherDay CalculateWeather = new CalculateWeatherDay(); //****Convert each day to useful data //Day One DateTime DayOneDate = WeatherData[0][0].DateOfforecast; DayOne = CalculateWeather.CalculateUsefulWeather(WeatherData[0]); DateDayOne.Text = WeatherData[0][0].DisplayDate; DayOneMaxTemp.Text = string.Format("Max: {0} °C", DayOne.MaxTemp.ToString()); DayOneMinTemp.Text = string.Format("Min: {0} °C", DayOne.MinTemp.ToString()); DayOneAverageHumidity.Text = string.Format("Humidity: {0} %", DayOne.AverageHumidity.ToString()); BitmapImage bitmapImageDayOne = new BitmapImage(); ///File licenses/credit here /// 1/ <a href="https://pngtree.com/free-backgrounds">free background photos from pngtree.com</a> /// 2/ sourced from: https://www.flashcardsforkindergarten.com/weather-flashcards/ /// 3/ https://africanaprincess.wordpress.com/2018/08/22/fog/ /// Uri imageFileDayOne = new Uri(string.Format("ms-appx:///Assets//{0}", DayOne.WeatherType)); bitmapImageDayOne.UriSource = imageFileDayOne; WeatherImageDayOne.Source = bitmapImageDayOne; DayOneAverageWindSpeed.Text = string.Format("Pressure: {0} hPa", DayOne.AveragePressure); DayOneTotalRainfall.Text = string.Format("Expected Rainfall: {0:0.00} mm", DayOne.TotalRainfall); //Day Two DateTime DayTwoDate = WeatherData[1][0].DateOfforecast; DayTwo = CalculateWeather.CalculateUsefulWeather(WeatherData[1]); DateDayTwo.Text = WeatherData[1][1].DisplayDate; DayTwoMaxTemp.Text = string.Format("Max: {0} °C", DayTwo.MaxTemp.ToString()); DayTwoMinTemp.Text = string.Format("Min: {0} °C", DayTwo.MinTemp.ToString()); DayTwoAverageHumidity.Text = string.Format("Humidity: {0} %", DayTwo.AverageHumidity.ToString()); BitmapImage bitmapImageDayTwo = new BitmapImage(); Uri imageFileDayTwo = new Uri(string.Format("ms-appx:///Assets//{0}", DayTwo.WeatherType)); bitmapImageDayTwo.UriSource = imageFileDayTwo; WeatherImageDayTwo.Source = bitmapImageDayTwo; DayTwoAverageWindSpeed.Text = string.Format("Pressure: {0} hPa", DayTwo.AveragePressure); DayTwoTotalRainfall.Text = string.Format("Expected Rainfall: {0:0.00} mm", DayTwo.TotalRainfall); //Day Three DateTime DayThreeDate = WeatherData[2][0].DateOfforecast; DayThree = CalculateWeather.CalculateUsefulWeather(WeatherData[2]); DateDayThree.Text = WeatherData[2][0].DisplayDate; DayThreeMaxTemp.Text = string.Format("Max: {0} °C", DayThree.MaxTemp.ToString()); DayThreeMinTemp.Text = string.Format("Min: {0} °C", DayThree.MinTemp.ToString()); DayThreeAverageHumidity.Text = string.Format("Humidity: {0} %", DayThree.AverageHumidity.ToString()); BitmapImage bitmapImageDayThree = new BitmapImage(); Uri imageFileDayThree = new Uri(string.Format("ms-appx:///Assets//{0}", DayThree.WeatherType)); bitmapImageDayThree.UriSource = imageFileDayThree; WeatherImageDayThree.Source = bitmapImageDayThree; DayThreeAverageWindSpeed.Text = string.Format("Pressure: {0} hPa", DayThree.AveragePressure); DayThreeTotalRainfall.Text = string.Format("Expected Rainfall: {0:0.00} mm", DayThree.TotalRainfall); //Day Four DateTime DayFourDate = WeatherData[3][0].DateOfforecast; DayFour = CalculateWeather.CalculateUsefulWeather(WeatherData[3]); DateDayFour.Text = WeatherData[3][0].DisplayDate; DayFourMaxTemp.Text = string.Format("Max: {0} °C", DayFour.MaxTemp.ToString()); DayFourMinTemp.Text = string.Format("Min: {0} °C", DayFour.MinTemp.ToString()); DayFourAverageHumidity.Text = string.Format("Humidity: {0} %", DayFour.AverageHumidity.ToString()); BitmapImage bitmapImageDayFour = new BitmapImage(); Uri imageFileDayFour = new Uri(string.Format("ms-appx:///Assets//{0}", DayFour.WeatherType)); bitmapImageDayFour.UriSource = imageFileDayFour; WeatherImageDayFour.Source = bitmapImageDayFour; DayFourAverageWindSpeed.Text = string.Format("Pressure: {0} hPa", DayFour.AveragePressure); DayFourTotalRainfall.Text = string.Format("Expected Rainfall: {0:0.00} mm", DayFour.TotalRainfall); //Day Five DateTime DayFiveDate = WeatherData[4][0].DateOfforecast; DayFive = CalculateWeather.CalculateUsefulWeather(WeatherData[4]); DateDayFive.Text = WeatherData[4][0].DisplayDate; DayFiveMaxTemp.Text = string.Format("Max: {0} °C", DayFive.MaxTemp.ToString()); DayFiveMinTemp.Text = string.Format("Min: {0} °C", DayFive.MinTemp.ToString()); DayFiveAverageHumidity.Text = string.Format("Humidity: {0} %", DayFive.AverageHumidity.ToString()); BitmapImage bitmapImageDayFive = new BitmapImage(); Uri imageFileDayFive = new Uri(string.Format("ms-appx:///Assets//{0}", DayFive.WeatherType)); bitmapImageDayFive.UriSource = imageFileDayFive; WeatherImageDayFive.Source = bitmapImageDayFive; DayFiveAverageWindSpeed.Text = string.Format("Pressure: {0} hPa", DayFive.AveragePressure); DayFiveTotalRainfall.Text = string.Format("Expected Rainfall: {0:.00} mm", DayFive.TotalRainfall); ForeCastProgressRing.IsActive = false; Logger.Instance.LogSuccessFullLoad(); }