Skip to content

Instantly share code, notes, and snippets.

@ImShizer
Last active March 27, 2024 22:34
Show Gist options
  • Save ImShizer/9467694f639fc190e6697ca5b482be6d to your computer and use it in GitHub Desktop.
Save ImShizer/9467694f639fc190e6697ca5b482be6d to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<connectionStrings>
<add name="conn"
providerName="System.Data.SqlClient"
connectionString="Data Source=localhost;Initial Catalog=Teas;Integrated Security=True;" />
</connectionStrings>
</configuration>
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAdapter
{
public class DbController
{
public void AddTea(string connectionString, string name, string type, float weight, float price, int country_id, string description)
{
SqlDataAdapter teaDataAdapter = new SqlDataAdapter("SELECT * FROM Teas", connectionString);
SqlCommand insertTea = new SqlCommand("INSERT INTO Teas (name, type, weight, price, country_id, description) VALUES (@name, @type, @weight, @price, @country_id, @description); SELECT SCOPE_IDENTITY();", new SqlConnection(connectionString));
insertTea.Parameters.Add("@name", SqlDbType.NVarChar, 50, "name");
insertTea.Parameters.Add("@type", SqlDbType.NVarChar, 50, "type");
insertTea.Parameters.Add("@weight", SqlDbType.Float, 50, "weight");
insertTea.Parameters.Add("@price", SqlDbType.Float, 50, "price");
insertTea.Parameters.Add("@country_id", SqlDbType.Int, 50, "country_id");
insertTea.Parameters.Add("@description", SqlDbType.NVarChar, 50, "description");
teaDataAdapter.InsertCommand = insertTea;
DataSet dataSet = new DataSet();
teaDataAdapter.Fill(dataSet, "Teas");
DataRow newTea = dataSet.Tables["Teas"].NewRow();
newTea["name"] = name;
newTea["type"] = type;
newTea["weight"] = weight;
newTea["price"] = price;
newTea["country_id"] = country_id;
newTea["description"] = description;
dataSet.Tables["Teas"].Rows.Add(newTea);
teaDataAdapter.Update(dataSet, "Teas");
teaDataAdapter.Dispose();
insertTea.Connection.Dispose();
Console.WriteLine("Db updated");
}
public void AddCountry(string connectionString, string name, float teaAmount)
{
SqlDataAdapter countryDataAdapter = new SqlDataAdapter("SELECT * FROM Countries", connectionString);
SqlCommand insertCountry = new SqlCommand("INSERT INTO Countries (name, tea_amount) VALUES (@name, @tea_amount); SELECT SCOPE_IDENTITY();", new SqlConnection(connectionString));
insertCountry.Parameters.Add("@name", SqlDbType.NVarChar, 50, "name");
insertCountry.Parameters.Add("@tea_amount", SqlDbType.Float, 50, "tea_amount");
countryDataAdapter.InsertCommand = insertCountry;
DataSet dataSet = new DataSet();
countryDataAdapter.Fill(dataSet, "Countries");
DataRow newCountry = dataSet.Tables["Countries"].NewRow();
newCountry["name"] = name;
newCountry["tea_amount"] = teaAmount;
dataSet.Tables["Countries"].Rows.Add(newCountry);
countryDataAdapter.Update(dataSet, "Countries");
countryDataAdapter.Dispose();
insertCountry.Connection.Dispose();
Console.WriteLine("Db updated");
}
public void UpdateCountry(string connectionString, int countryId, string name, float teaAmount)
{
SqlDataAdapter countryDataAdapter = new SqlDataAdapter("SELECT * FROM Countries WHERE id = @countryId", connectionString);
countryDataAdapter.SelectCommand.Parameters.AddWithValue("@countryId", countryId);
SqlCommand updateCountry = new SqlCommand("UPDATE Countries SET name = @name, tea_amount = @tea_amount WHERE id = @countryId", new SqlConnection(connectionString));
updateCountry.Parameters.AddWithValue("@name", name);
updateCountry.Parameters.AddWithValue("@tea_amount", teaAmount);
updateCountry.Parameters.AddWithValue("@countryId", countryId);
countryDataAdapter.UpdateCommand = updateCountry;
DataSet dataSet = new DataSet();
countryDataAdapter.Fill(dataSet, "Countries");
if (dataSet.Tables["Countries"].Rows.Count > 0)
{
dataSet.Tables["Countries"].Rows[0]["name"] = name;
dataSet.Tables["Countries"].Rows[0]["tea_Amount"] = teaAmount;
countryDataAdapter.Update(dataSet, "Countries");
Console.WriteLine("Country updated");
}
else
{
Console.WriteLine("Id not found");
}
countryDataAdapter.Dispose();
updateCountry.Connection.Dispose();
}
public void UpdateTea(string connectionString, int teaId, string name, string type, float weight, float price, int country_id, string description)
{
SqlDataAdapter teaDataAdapter = new SqlDataAdapter("SELECT * FROM Teas WHERE id = @teaId", connectionString);
teaDataAdapter.SelectCommand.Parameters.AddWithValue("@teaId", teaId);
SqlCommand updateTea = new SqlCommand("UPDATE Teas SET name = @name, type = @type, weight = @weight, price = @price, country_id = @country_id, description = @description WHERE id = @teaId", new SqlConnection(connectionString));
updateTea.Parameters.AddWithValue("@name", name);
updateTea.Parameters.AddWithValue("@type", type);
updateTea.Parameters.AddWithValue("@weight", weight);
updateTea.Parameters.AddWithValue("@price", price);
updateTea.Parameters.AddWithValue("@country_id", country_id);
updateTea.Parameters.AddWithValue("@description", description);
updateTea.Parameters.AddWithValue("@teaId", teaId);
teaDataAdapter.UpdateCommand = updateTea;
DataSet dataSet = new DataSet();
teaDataAdapter.Fill(dataSet, "Teas");
if (dataSet.Tables["Teas"].Rows.Count > 0)
{
dataSet.Tables["Teas"].Rows[0]["name"] = name;
dataSet.Tables["Teas"].Rows[0]["type"] = type;
dataSet.Tables["Teas"].Rows[0]["weight"] = weight;
dataSet.Tables["Teas"].Rows[0]["price"] = price;
dataSet.Tables["Teas"].Rows[0]["country_id"] = country_id;
dataSet.Tables["Teas"].Rows[0]["description"] = description;
teaDataAdapter.Update(dataSet, "Teas");
Console.WriteLine("Tea updated");
}
else
{
Console.WriteLine("Id not found");
}
teaDataAdapter.Dispose();
updateTea.Connection.Dispose();
}
public void DeleteTea(string connectionString, int teaId)
{
SqlDataAdapter teaDataAdapter = new SqlDataAdapter("SELECT * FROM Teas WHERE id = @teaId", connectionString);
teaDataAdapter.SelectCommand.Parameters.AddWithValue("@teaId", teaId);
SqlCommand deleteTea = new SqlCommand("DELETE FROM Teas WHERE id = @teaId", new SqlConnection(connectionString));
deleteTea.Parameters.AddWithValue("@teaId", teaId);
teaDataAdapter.DeleteCommand = deleteTea;
DataSet dataSet = new DataSet();
teaDataAdapter.Fill(dataSet, "Teas");
if (dataSet.Tables["Teas"].Rows.Count > 0)
{
dataSet.Tables["Teas"].Rows[0].Delete();
teaDataAdapter.Update(dataSet, "Teas");
Console.WriteLine("Tea deleted");
}
else
{
Console.WriteLine("Id not found");
}
teaDataAdapter.Dispose();
deleteTea.Connection.Dispose();
}
public void DeleteCountry(string connectionString, int countryId)
{
SqlDataAdapter countryDataAdapter = new SqlDataAdapter("SELECT * FROM Countries WHERE id = @countryId", connectionString);
countryDataAdapter.SelectCommand.Parameters.AddWithValue("@countryId", countryId);
SqlCommand deleteCountry = new SqlCommand("DELETE FROM Countries WHERE id = @countryId", new SqlConnection(connectionString));
deleteCountry.Parameters.AddWithValue("@countryId", countryId);
countryDataAdapter.DeleteCommand = deleteCountry;
DataSet dataSet = new DataSet();
countryDataAdapter.Fill(dataSet, "Countries");
if (dataSet.Tables["Countries"].Rows.Count > 0)
{
dataSet.Tables["Countries"].Rows[0].Delete();
countryDataAdapter.Update(dataSet, "Countries");
Console.WriteLine("Country deleted");
}
else
{
Console.WriteLine("Id not found");
}
countryDataAdapter.Dispose();
deleteCountry.Connection.Dispose();
}
public void DeleteAll(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand deleteTeasCommand = new SqlCommand("DELETE FROM Teas", connection))
{
deleteTeasCommand.ExecuteNonQuery();
}
using (SqlCommand deleteCountriesCommand = new SqlCommand("DELETE FROM Countries", connection))
{
deleteCountriesCommand.ExecuteNonQuery();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace DataAdapter
{
internal class Program
{
private static DbController dbController = new DbController();
private static Printer printer = new Printer();
private static string conn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
static void Main(string[] args)
{
dbController.AddTea(conn, "chanchun", "green", 1, 120, 1, "Tea 1");
dbController.AddTea(conn, "linlin", "black", 22, 200, 1, "Tea 2");
dbController.AddTea(conn, "saosen", "black", 2, 999, 1, "Tea 3");
dbController.AddCountry(conn, "America", 200);
dbController.AddCountry(conn, "Romania", 10);
dbController.AddCountry(conn, "France", 99);
dbController.AddCountry(conn, "China", 79);
dbController.AddCountry(conn, "Japan", 29);
dbController.UpdateCountry(conn, 1, "Romania", 18000);
dbController.UpdateTea(conn, 3, "SAUSEP", "yellow", 120, 30, 2, "UPD");
dbController.DeleteTea(conn, 3);
dbController.DeleteCountry(conn, 3);
printer.PrintByType(conn, "green");
printer.PrintByPrice(conn, 100, 300);
printer.PrintByWeight(conn, 1, 3);
printer.PrintByCountry(conn, 1050);
printer.Top3ByQuantity(conn);
printer.Top3GreenByWeight(conn);
printer.Top3BlackByWeight(conn);
printer.Top3TeasByWeight(conn);
dbController.DeleteAll(conn);
}
}
}
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAdapter
{
public class Printer
{
public void PrintByType(string connectionString, string type)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Teas WHERE type = @type", connection);
adapter.SelectCommand.Parameters.AddWithValue("@type", type);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0)
{
Console.WriteLine("Teas with type " + type + ":");
foreach (DataRow row in dataSet.Tables[0].Rows)
{
Console.WriteLine($"ID: {row["id"]}, Name: {row["name"]}, Type: {row["type"]}, Weight: {row["weight"]}, Price: {row["price"]}, Country_ID: {row["country_id"]}, Description: {row["description"]}");
}
}
else
{
Console.WriteLine("No teas found");
}
}
Console.WriteLine();
}
public void PrintByPrice(string connectionString, float minPrice, float maxPrice)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Teas WHERE price >= @minPrice AND price <= @maxPrice", connection);
adapter.SelectCommand.Parameters.AddWithValue("@minPrice", minPrice);
adapter.SelectCommand.Parameters.AddWithValue("@maxPrice", maxPrice);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0)
{
Console.WriteLine($"Teas with price between {minPrice} and {maxPrice}:");
foreach (DataRow row in dataSet.Tables[0].Rows)
{
Console.WriteLine($"ID: {row["id"]}, Name: {row["name"]}, Type: {row["type"]}, Weight: {row["weight"]}, Price: {row["price"]}, Country_ID: {row["country_id"]}, Description: {row["description"]}");
}
}
else
{
Console.WriteLine($"No teas found");
}
}
Console.WriteLine();
}
public void PrintByWeight(string connectionString, float minWeight, float maxWeight)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Teas WHERE weight >= @minWeight AND weight <= @maxWeight", connection);
adapter.SelectCommand.Parameters.AddWithValue("@minWeight", minWeight);
adapter.SelectCommand.Parameters.AddWithValue("@maxWeight", maxWeight);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0)
{
Console.WriteLine($"Teas with weight between {minWeight} and {maxWeight}:");
foreach (DataRow row in dataSet.Tables[0].Rows)
{
Console.WriteLine($"ID: {row["id"]}, Name: {row["name"]}, Type: {row["type"]}, Weight: {row["weight"]}, Price: {row["price"]}, Country_ID: {row["country_id"]}, Description: {row["description"]}");
}
}
else
{
Console.WriteLine($"No teas found");
}
}
Console.WriteLine();
}
public void PrintByCountry(string connectionString, int countryId)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT t.*, c.Name AS CountryName FROM Teas t INNER JOIN Countries c ON t.country_id = c.id WHERE c.id = @countryId";
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
adapter.SelectCommand.Parameters.AddWithValue("@countryId", countryId);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0)
{
Console.WriteLine("Teas in country " + dataSet.Tables[0].Rows[0]["CountryName"] + ":");
foreach (DataRow row in dataSet.Tables[0].Rows)
{
Console.WriteLine($"ID: {row["id"]}, Name: {row["name"]}, Type: {row["type"]}, Weight: {row["weight"]}, Price: {row["price"]}, Country: {row["CountryName"]}, Description: {row["description"]}");
}
}
else
{
Console.WriteLine("No teas found");
}
}
Console.WriteLine();
}
public void Top3ByQuantity(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT TOP 3 * FROM Countries ORDER BY tea_amount DESC", connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0)
{
Console.WriteLine("Top 3 countries by tea_amount:");
foreach (DataRow row in dataSet.Tables[0].Rows)
{
Console.WriteLine($"ID: {row["id"]}, Name: {row["name"]}, Tea Amount: {row["tea_amount"]}");
}
}
else
{
Console.WriteLine("No countries found");
}
}
Console.WriteLine();
}
public void Top3GreenByWeight(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT TOP 3 * FROM Teas WHERE type = 'green' ORDER BY weight DESC", connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0)
{
Console.WriteLine("Top 3 green teas by weight:");
foreach (DataRow row in dataSet.Tables[0].Rows)
{
Console.WriteLine($"ID: {row["id"]}, Name: {row["name"]}, Type: {row["type"]}, Weight: {row["weight"]}, Price: {row["price"]}, Country_ID: {row["country_id"]}, Description: {row["description"]}");
}
}
else
{
Console.WriteLine("No green teas found");
}
}
Console.WriteLine();
}
public void Top3BlackByWeight(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT TOP 3 * FROM Teas WHERE type = 'black' ORDER BY weight DESC", connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0)
{
Console.WriteLine("Top 3 black teas by weight:");
foreach (DataRow row in dataSet.Tables[0].Rows)
{
Console.WriteLine($"ID: {row["id"]}, Name: {row["name"]}, Type: {row["type"]}, Weight: {row["weight"]}, Price: {row["price"]}, Country_ID: {row["country_id"]}, Description: {row["description"]}");
}
}
else
{
Console.WriteLine("No black teas found");
}
}
Console.WriteLine();
}
public void Top3TeasByWeight(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT TOP 3 * FROM Teas ORDER BY weight DESC", connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0)
{
Console.WriteLine("Top 3 teas by weight:");
foreach (DataRow row in dataSet.Tables[0].Rows)
{
Console.WriteLine($"ID: {row["id"]}, Name: {row["name"]}, Type: {row["type"]}, Weight: {row["weight"]}, Price: {row["price"]}, Country_ID: {row["country_id"]}, Description: {row["description"]}");
}
}
else
{
Console.WriteLine("No teas found");
}
}
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment