Skip to content

Instantly share code, notes, and snippets.

@ImShizer
Created March 31, 2024 21:02
Show Gist options
  • Save ImShizer/bf0c69bd2c4b6095d8b44994e7c60464 to your computer and use it in GitHub Desktop.
Save ImShizer/bf0c69bd2c4b6095d8b44994e7c60464 to your computer and use it in GitHub Desktop.
using Dapper;
using Microsoft.Data.SqlClient;
using System;
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public DateTime BirthDate { get; set; }
}
public class NewSection
{
public string Name { get; set; }
public string Description { get; set; }
}
public class NewProduct
{
public string PName { get; set; }
public decimal Price { get; set; }
public int SectionId { get; set; }
}
public class NewCountry
{
public string CountryName { get; set; }
public string Code { get; set; }
}
public class NewCity
{
public string CityName { get; set; }
public int CountryId { get; set; }
}
public class Program
{
static void Main(string[] args)
{
string connectionString = "Data Source=localhost;Initial Catalog=Mail;Integrated Security=True";
// Task 1
var newCustomer = new Customer
{
FirstName = "Oleg",
LastName = "Olegov",
Email = "test@test.com",
BirthDate = new DateTime(2007, 20, 7)
};
using (var connection = new SqlConnection(connectionString))
{
const string sqlCustomer = @"INSERT INTO Customers (FirstName, LastName, Email, BirthDate)
VALUES (@FirstName, @LastName, @Email, @BirthDate)";
connection.Execute(sqlCustomer, newCustomer);
}
var newSection = new NewSection
{
Name = "Important",
Description = "Most important news"
};
using (var connection = new SqlConnection(connectionString))
{
const string sqlSection = @"INSERT INTO Sections (Name, Description)
VALUES (@Name, @Description)";
connection.Execute(sqlSection, newSection);
}
var newProduct = new NewProduct
{
PName = "Samsung Galaxy S9",
Price = 1600,
SectionId = 1
};
var newCountry = new NewCountry
{
CountryName = "Ukraine",
Code = "UA"
};
using (var connection = new SqlConnection(connectionString))
{
const string sqlCountry = @"INSERT INTO Countries (CountryName, Code)
VALUES (@CountryName, @Code)";
connection.Execute(sqlCountry, newCountry);
}
var newCity = new NewCity
{
CityName = "Odesa",
CountryId = 1
};
using (var connection = new SqlConnection(connectionString))
{
const string sqlCity = @"INSERT INTO Cities (CityName, CountryId)
VALUES (@CityName, @CountryId)";
connection.Execute(sqlCity, newCity);
}
using (var connection = new SqlConnection(connectionString))
{
const string sqlProduct = @"INSERT INTO Products (PName, Price, SectionId)
VALUES (@PName, @Price, @SectionId)";
connection.Execute(sqlProduct, newProduct);
}
Console.WriteLine("Data added successfully!");
//Task 2
using (var connection = new SqlConnection(connectionString))
{
var customerToUpdate = connection.QuerySingle<Customer>("SELECT * FROM Customers WHERE Id = @Id", new { Id = 1 });
customerToUpdate.FirstName = "Oleg";
connection.Execute("UPDATE Customers SET FirstName = @FirstName, LastName = @LastName, Email = @Email, BirthDate = @BirthDate WHERE Id = @Id", customerToUpdate);
}
using (var connection = new SqlConnection(connectionString))
{
var sectionToUpdate = connection.QuerySingle<NewSection>("SELECT * FROM Sections WHERE Id = @Id", new { Id = 1 });
sectionToUpdate.Name = "News";
connection.Execute("UPDATE Sections SET Name = @Name, Description = @Description WHERE Id = @Id", sectionToUpdate);
}
using (var connection = new SqlConnection(connectionString))
{
var productToUpdate = connection.QuerySingle<NewProduct>("SELECT * FROM Products WHERE Id = @Id", new { Id = 1 });
productToUpdate.PName = "Samsung Galaxy S10";
productToUpdate.Price = 1400;
connection.Execute("UPDATE Products SET Name = @PName, Price = @Price, SectionId = @SectionId WHERE Id = @Id", productToUpdate);
}
using (var connection = new SqlConnection(connectionString))
{
var countryToUpdate = connection.QuerySingle<NewCountry>("SELECT * FROM Countries WHERE Id = @Id", new { Id = 1 });
countryToUpdate.CountryName = "Ukraine";
connection.Execute("UPDATE Countries SET Name = @CountryName, Code = @Code WHERE Id = @Id", countryToUpdate);
}
using (var connection = new SqlConnection(connectionString))
{
var cityToUpdate = connection.QuerySingle<NewCity>("SELECT * FROM Cities WHERE Id = @Id", new { Id = 1 });
cityToUpdate.CityName = "Mykolaiv";
connection.Execute("UPDATE Cities SET Name = @CityName, CountryId = @CountryId WHERE Id = @Id", cityToUpdate);
}
//Task3
using (var connection = new SqlConnection(connectionString))
{
connection.Execute("DELETE FROM NewCustomers WHERE Id = @Id", new { Id = 1 });
}
using (var connection = new SqlConnection(connectionString))
{
connection.Execute("DELETE FROM NewSections WHERE Id = @Id", new { Id = 1 });
}
using (var connection = new SqlConnection(connectionString))
{
connection.Execute("DELETE FROM NewProducts WHERE Id = @Id", new { Id = 1 });
}
using (var connection = new SqlConnection(connectionString))
{
connection.Execute("DELETE FROM NewCountries WHERE Id = @Id", new { Id = 1 });
}
using (var connection = new SqlConnection(connectionString))
{
connection.Execute("DELETE FROM NewCities WHERE Id = @Id", new { Id = 1 });
}
//Task4
using (var connection = new SqlConnection(connectionString))
{
var sections = connection.Query<NewSection>("SELECT * FROM NewSections WHERE CustomerId = @Id", new { Id = 1 });
foreach (var section in sections)
{
Console.WriteLine($"{section.Name}");
}
}
using (var connection = new SqlConnection(connectionString))
{
var products = connection.Query<NewProduct>("SELECT * FROM NewProducts WHERE SectionId = @Id AND IsSale = 1", new { Id = 1 });
foreach (var product in products)
{
Console.WriteLine($"{product.PName} - {product.Price}");
}
}
using (var connection = new SqlConnection(connectionString))
{
var cities = connection.Query<NewCity>("SELECT * FROM NewCities WHERE CountryName = @CountryName", new { CountryName = "Україна" });
foreach (var city in cities)
{
Console.WriteLine($"{city.CityName}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment