Скриншот архива с проектом
Задание
Напишите приложение позволяющее вести учет путешествий. Рекомендации: язык C#, технология ASP.NET Core Razor Pages, база данных - SQL Server.
Функционал программы
- добавление путешествий
- редактирование путешествий
- удаление путешествий
Фрагмент программного кода
using Microsoft.AspNetCore.Mvc.RazorPages; using System.Data.SqlClient; using Voyage.BL; namespace Voyage.WebUI.Pages.Voyages { public class EditModel : PageModel { public String errorMessage = ""; public String successMessage = ""; public VoyageInfo voyageInfo = new VoyageInfo(); public void OnGet() { #pragma warning disable CS8600 String id = Request.Query["id"]; #pragma warning restore CS8600 try { String connectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;Initial Catalog=VoyagesDB;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); String sql = "SELECT * FROM Voyages WHERE id=@id"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@id", id); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { voyageInfo.id = "" + reader.GetInt32(0); voyageInfo.departure_point = reader.GetString(1); voyageInfo.departure_date = reader.GetDateTime(2).ToString(); voyageInfo.destination_country = reader.GetString(3); voyageInfo.destination_city = reader.GetString(4); voyageInfo.arrival_date = reader.GetDateTime(5).ToString(); voyageInfo.trip_cost = "" + reader.GetInt32(6); voyageInfo.trip_duration = "" + reader.GetInt32(7); } } } } } catch (Exception ex) { errorMessage = ex.Message; } } public void OnPost() { #pragma warning disable CS8601 voyageInfo.id = Request.Form["id"]; voyageInfo.departure_point = Request.Form["departure_point"]; voyageInfo.departure_date = Request.Form["departure_date"]; voyageInfo.destination_country = Request.Form["destination_country"]; voyageInfo.destination_city = Request.Form["destination_city"]; voyageInfo.arrival_date = Request.Form["arrival_date"]; voyageInfo.trip_cost = Request.Form["trip_cost"]; voyageInfo.trip_duration = Request.Form["trip_duration"]; #pragma warning restore CS8601 #pragma warning disable CS8602 if (voyageInfo.id.Length == 0 || voyageInfo.departure_point.Length == 0 || voyageInfo.departure_date.Length == 0 || voyageInfo.destination_country.Length == 0 || voyageInfo.destination_city.Length == 0 || voyageInfo.arrival_date.Length == 0 || voyageInfo.trip_cost.Length == 0 || voyageInfo.trip_duration.Length == 0) { errorMessage = "All the fields are required"; return; } #pragma warning restore CS8602 try { String connectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;Initial Catalog=VoyagesDB;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); String sql = "UPDATE Voyages SET departure_point = @departure_point, departure_date = @departure_date, " + "destination_country = @destination_country, destination_city = @destination_city, " + "arrival_date = @arrival_date, trip_cost = @trip_cost , trip_duration = @trip_duration WHERE id=@id"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@id", voyageInfo.id); command.Parameters.AddWithValue("@departure_point", voyageInfo.departure_point); command.Parameters.AddWithValue("@departure_date", voyageInfo.departure_date); command.Parameters.AddWithValue("@destination_country", voyageInfo.destination_country); command.Parameters.AddWithValue("@destination_city", voyageInfo.destination_city); command.Parameters.AddWithValue("@arrival_date", voyageInfo.arrival_date); command.Parameters.AddWithValue("@trip_cost", voyageInfo.trip_cost); command.Parameters.AddWithValue("@trip_duration", voyageInfo.trip_duration); command.ExecuteNonQuery(); } } } catch (Exception ex) { errorMessage = ex.Message; return; } Response.Redirect("/Voyages/Index"); } } }
Пояснения по запуску программы
Для эффективной работы вам потребуется ПК, Visual Studio 2022 Community. Скачать IDE можно по ссылке - https://visualstudio.microsoft.com/ru/vs/community/ Скачиваете. запускаете установку, в рабочих нагрузках выбираем: Разработка ASP.NET и разработка десктопных приложений на C#, Visual Basic. Запускаете проетк, запускаете код на выполнение. при необходимости выкладываете на хостинг.
admin