Задание
Написать / разработать веб-приложение на языке высокого уровня C# для учета товаров. В качестве графического интерфейса используйте ASP.NET core 7.0 и базу данных SQL Server.
Функционал программы
- вывод всех товаров
- добавление товара
- редактирование товараудаление товараи так далее, должно быть понятно предназначение программы.
Фрагмент программного кода
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data.SqlClient;
namespace AccountingGoodsWebApp.Pages.Products
{
public class EditModel : PageModel
{
public ProductInfo productInfo = new ProductInfo();
public String errorMessage = "";
public String successMessage = "";
public void OnGet()
{
String id = Request.Query["id"];
try
{
String connectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;" +
"Initial Catalog=GoodsDB;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
String sql = "SELECT * FROM Products WHERE id=@id";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@id", id);
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
productInfo.id = "" + reader.GetInt32(0);
productInfo.supplier = reader.GetString(1);
productInfo.productName = reader.GetString(2);
productInfo.storekeeper = reader.GetString(3);
}
}
}
}
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
}
public void OnPost()
{
productInfo.id = Request.Form["id"];
productInfo.supplier = Request.Form["supplier"];
productInfo.productName = Request.Form["productName"];
productInfo.storekeeper = Request.Form["storekeeper"];
if (productInfo.id.Length == 0 || productInfo.supplier.Length == 0 ||
productInfo.productName.Length == 0 || productInfo.storekeeper.Length == 0)
{
errorMessage = "All the fields are required";
return;
}
try
{
String connectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;" +
"Initial Catalog=GoodsDB;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
String sql = "UPDATE Products SET supplier=@supplier, productName=@productName, " +
"storekeeper=@storekeeper WHERE id=@id";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@supplier", productInfo.supplier);
command.Parameters.AddWithValue("@productName", productInfo.productName);
command.Parameters.AddWithValue("@storekeeper", productInfo.storekeeper);
command.Parameters.AddWithValue("@id", productInfo.id);
command.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
errorMessage = ex.Message;
return;
}
Response.Redirect("/Products/Index");
}
}
}
Структура проекта

Скриншот архива с проектом

Пояснения по запуску программы
Скачать и установить ПО - Visual Studio 2022 и MS SQL Server Management Studio 19. Присоединить БД (смотри видео), добавить подключение (смотри видео), запустить проект.
Телеграм
-