Created
March 27, 2024 22:38
-
-
Save ImShizer/fbcb1c1f21914d2cda67e97549b99781 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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=Fruits and Vegetables;Integrated Security=True;" /> | |
</connectionStrings> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Data; | |
using System.Data.Common; | |
using System.Data.SqlClient; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ADO.NET | |
{ | |
public class Program | |
{ | |
private static string providerName = ConfigurationManager.AppSettings["provider"]; | |
private static string connString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; | |
static async Task Main(string[] args) | |
{ | |
DbProviderFactory factory = DbProviderFactories.GetFactory(providerName); | |
using (DbConnection connection = factory.CreateConnection()) | |
{ | |
if (connection == null) | |
{ | |
Console.WriteLine("Failed to create connection"); | |
return; | |
} | |
connection.ConnectionString = connString; | |
try | |
{ | |
await connection.OpenAsync(); | |
Console.WriteLine("Connection successful"); | |
string query = null; | |
while (true) | |
{ | |
Console.WriteLine("[1] - Показать информацию\n" + | |
"[2] - Показать названия\n" + | |
"[3] - Показать цвета\n" + | |
"[4] - Максимальная каллорийность\n" + | |
"[5] - Минимальная каллорийность\n" + | |
"[6] - Средняя каллорийность\n" + | |
"[7] - Количество овощей\n" + | |
"[8] - Количество фруктов\n" + | |
"[9] - Фильтр по цвету\n" + | |
"[10] - Количество по цвету\n" + | |
"[11] - Показать по калориям более заданного\n" + | |
"[12] - Показать по диапазону калорий\n" + | |
"[13] - Красного или жёлтого цвета\n"); | |
int choose; | |
string inputVar = null; | |
string range_start = null; | |
string range_end = null; | |
choose = Convert.ToInt32(Console.ReadLine()); | |
switch (choose) | |
{ | |
case 1: | |
query = "SELECT * FROM Product"; | |
break; | |
case 2: | |
query = "SELECT name FROM Product"; | |
break; | |
case 3: | |
query = "SELECT color FROM Product"; | |
break; | |
case 4: | |
query = "SELECT MAX(cal) FROM Product"; | |
break; | |
case 5: | |
query = "SELECT MIN(cal) FROM Product"; | |
break; | |
case 6: | |
query = "SELECT AVG(cal) FROM Product"; | |
break; | |
case 7: | |
query = "SELECT COUNT(*) FROM Product WHERE Product.type LIKE 'vegetable'"; | |
Console.WriteLine("Amount of vegetables: "); | |
break; | |
case 8: | |
query = "SELECT COUNT(*) FROM Product WHERE Product.type LIKE 'fruit'"; | |
Console.WriteLine("Amount of fruits: "); | |
break; | |
case 9: | |
Console.WriteLine("Enter ur color: "); | |
inputVar = Console.ReadLine(); | |
query = "SELECT COUNT(*) FROM Product WHERE color LIKE @color"; | |
Console.WriteLine("Amount of color " + inputVar + ": "); | |
break; | |
case 10: | |
query = "SELECT color, COUNT(*) FROM Product GROUP BY color"; | |
break; | |
case 11: | |
Console.WriteLine("Enter end of range: "); | |
inputVar = Console.ReadLine(); | |
query = "SELECT * FROM Product WHERE cal > @inputVar"; | |
break; | |
case 12: | |
Console.WriteLine("Enter begin of range: "); | |
range_start = Console.ReadLine(); | |
Console.WriteLine("Enter end of range: "); | |
range_end = Console.ReadLine(); | |
query = "SELECT * FROM Product WHERE cal > @range_start AND cal < @range_end"; | |
break; | |
case 13: | |
query = "SELECT * FROM Product WHERE color LIKE 'yellow' OR color LIKE 'red'"; | |
break; | |
} | |
using (DbCommand command = factory.CreateCommand()) | |
{ | |
if (command == null) | |
{ | |
Console.WriteLine("Failed to create command."); | |
return; | |
} | |
command.Connection = connection; | |
command.CommandText = query; | |
if (choose == 9 || choose == 11 || choose == 12) | |
{ | |
if (choose == 9) | |
{ | |
command.Parameters.Add(CreateParameter("color", DbType.String, inputVar)); | |
} | |
else if (choose == 11) | |
{ | |
command.Parameters.Add(CreateParameter("cal", DbType.Int32, inputVar)); | |
} | |
else if (choose == 12) | |
{ | |
command.Parameters.Add(CreateParameter("range_start", DbType.Int32, range_start)); | |
command.Parameters.Add(CreateParameter("range_end", DbType.Int32, range_end)); | |
} | |
} | |
Stopwatch stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
using (DbDataReader reader = await command.ExecuteReaderAsync()) | |
{ | |
stopwatch.Stop(); | |
TimeSpan ts = stopwatch.Elapsed; | |
if (reader.HasRows) | |
{ | |
while (await reader.ReadAsync()) | |
{ | |
for (int i = 0; i < reader.FieldCount; i++) | |
{ | |
Console.Write(reader[i] + " "); | |
} | |
Console.WriteLine(); | |
} | |
} | |
else | |
{ | |
Console.WriteLine("No data found"); | |
} | |
Console.WriteLine($"Execution Time: {ts.TotalMilliseconds} milliseconds"); | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Error: " + ex.Message); | |
} | |
} | |
} | |
private static DbParameter CreateParameter(string name, DbType type, object value) | |
{ | |
DbParameter parameter = DbProviderFactories.GetFactory(providerName).CreateParameter(); | |
parameter.ParameterName = name; | |
parameter.DbType = type; | |
parameter.Value = value; | |
return parameter; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment