Skip to content

Instantly share code, notes, and snippets.

@ImShizer
Created March 31, 2024 20:18
Show Gist options
  • Save ImShizer/e769a57848fd561c3b9678e692cddc4e to your computer and use it in GitHub Desktop.
Save ImShizer/e769a57848fd561c3b9678e692cddc4e to your computer and use it in GitHub Desktop.
public class Coffee
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Cost { get; set; }
public int Grams { get; set; }
public string Country { get; set; }
}
public class DataAccess
{
private string connectionString;
public DataAccess(string connectionString)
{
this.connectionString = connectionString;
}
public void AddCoffee(Coffee coffee)
{
using (var connection = new SqlConnection(connectionString))
{
var cmd = new SqlCommand("INSERT INTO Coffees (Name, Description, Cost, Grams, Country) VALUES (@name, @description, @cost, @grams, @country)", connection);
cmd.Parameters.AddWithValue("@name", coffee.Name);
cmd.Parameters.AddWithValue("@description", coffee.Description);
cmd.Parameters.AddWithValue("@cost", coffee.Cost);
cmd.Parameters.AddWithValue("@grams", coffee.Grams);
cmd.Parameters.AddWithValue("@country", coffee.Country);
cmd.ExecuteNonQuery();
}
}
public void UpdateCoffee(Coffee coffee)
{
using (var connection = new SqlConnection(connectionString))
{
var cmd = new SqlCommand("UPDATE Coffees SET Name = @name, Description = @description, Cost = @cost, Grams = @grams, Country = @country WHERE Id = @id", connection);
cmd.Parameters.AddWithValue("@id", coffee.Id);
cmd.Parameters.AddWithValue("@name", coffee.Name);
cmd.Parameters.AddWithValue("@description", coffee.Description);
cmd.Parameters.AddWithValue("@cost", coffee.Cost);
cmd.Parameters.AddWithValue("@grams", coffee.Grams);
cmd.Parameters.AddWithValue("@country", coffee.Country);
cmd.ExecuteNonQuery();
}
}
public void DeleteCoffee(int id)
{
using (var connection = new SqlConnection(connectionString))
{
var cmd = new SqlCommand("DELETE FROM Coffees WHERE Id = @id", connection);
cmd.Parameters.AddWithValue("@id", id);
cmd.ExecuteNonQuery();
}
}
public List<Coffee> FindCoffeeByCherryDescription()
{
using (var connection = new SqlConnection(connectionString))
{
var cmd = new SqlCommand("SELECT * FROM Coffees WHERE Description LIKE '%вишня%'", connection);
var reader = cmd.ExecuteReader();
var coffees = new List<Coffee>();
while (reader.Read())
{
coffees.Add(new Coffee
{
Id = (int)reader["Id"],
Name = (string)reader["Name"],
Description = (string)reader["Description"],
Cost = (decimal)reader["Cost"],
Grams = (int)reader["Grams"],
Country = (string)reader["Country"],
});
}
return coffees;
}
}
public List<Coffee> FindCoffeeByCostRange(decimal minCost, decimal maxCost)
{
using (var connection = new SqlConnection(connectionString))
{
var cmd = new SqlCommand("SELECT * FROM Coffees WHERE Cost BETWEEN @minCost AND @maxCost", connection);
cmd.Parameters.AddWithValue("@minCost", minCost);
cmd.Parameters.AddWithValue("@maxCost", maxCost);
var reader = cmd.ExecuteReader();
var coffees = new List<Coffee>();
while (reader.Read())
{
coffees.Add(new Coffee
{
Id = (int)reader["Id"],
Name = (string)reader["Name"],
Description = (string)reader["Description"],
Cost = (decimal)reader["Cost"],
Grams = (int)reader["Grams"],
Country = (string)reader["Country"],
});
}
return coffees;
}
}
public List<Coffee> FindCoffeeByGramsRange(int minGrams, int maxGrams)
{
using (var connection = new SqlConnection(connectionString))
{
var cmd = new SqlCommand("SELECT * FROM Coffees WHERE Grams BETWEEN @minGrams AND @maxGrams", connection);
cmd.Parameters.AddWithValue("@minGrams", minGrams);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment