From c34d288ca22628f40dd76da50b52bfea4190e8f9 Mon Sep 17 00:00:00 2001 From: Waleed Bensumaidea Date: Thu, 31 Aug 2023 20:58:11 +0300 Subject: [PATCH] Learn Execute Row Sql Select Statement --- 02_ExecuteRowSql/Program.cs | 32 ++++++++++++++++++++++++++++++-- 02_ExecuteRowSql/Wallet.cs | 15 +++++++++++++++ EF-Core.sln | 8 +++++++- 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 02_ExecuteRowSql/Wallet.cs diff --git a/02_ExecuteRowSql/Program.cs b/02_ExecuteRowSql/Program.cs index ae3a501..1a829d0 100644 --- a/02_ExecuteRowSql/Program.cs +++ b/02_ExecuteRowSql/Program.cs @@ -1,7 +1,11 @@ -using Microsoft.Extensions.Configuration; +using Microsoft.Data.SqlClient; +using Microsoft.Extensions.Configuration; +using System.Data; +using System.Globalization; namespace _02ConnectionString { + internal class Program { static void Main(string[] args) @@ -10,7 +14,31 @@ static void Main(string[] args) .AddJsonFile("appsettings.json") .Build(); - Console.WriteLine(configuration.GetSection("constr").Value); + SqlConnection conn = new SqlConnection(configuration.GetSection("constr").Value); //1 + + var sqlQuery = "Select * From Wallets "; //2 + + SqlCommand command = new SqlCommand(sqlQuery , conn); // 3 + command.CommandType = CommandType.Text;// 4 + + conn.Open(); // 5 + + SqlDataReader reader = command.ExecuteReader(); //6 + + Wallet wallet; + while (reader.Read()) // 7 how to get data into object + { + wallet = new Wallet() + { + Id = reader.GetInt32("Id"), + Holder = reader.GetString("Holder"), + Balance = reader.GetDecimal("Balance") + }; + Console.WriteLine(wallet); + } + + conn.Close(); + Console.ReadKey(); } } diff --git a/02_ExecuteRowSql/Wallet.cs b/02_ExecuteRowSql/Wallet.cs new file mode 100644 index 0000000..7a72b40 --- /dev/null +++ b/02_ExecuteRowSql/Wallet.cs @@ -0,0 +1,15 @@ +namespace _02ConnectionString +{ + public class Wallet + { + public int Id { get; set; } + public string? Holder { get; set; } + public decimal? Balance { get; set; } + + public override string ToString() + { + return $"[{Id}] {Holder} ({Balance:C0})"; + } + + } +} \ No newline at end of file diff --git a/EF-Core.sln b/EF-Core.sln index e560169..7b2e754 100644 --- a/EF-Core.sln +++ b/EF-Core.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.6.33829.357 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "02ConnectionString", "02ConnectionString\02ConnectionString.csproj", "{7317C8EF-EC9A-4F26-AE38-7D4E952083A9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "02ConnectionString", "02ConnectionString\02ConnectionString.csproj", "{7317C8EF-EC9A-4F26-AE38-7D4E952083A9}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "02_ExecuteRowSql", "02_ExecuteRowSql\02_ExecuteRowSql.csproj", "{421368B7-95A5-493C-94D2-E770FA33700B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {7317C8EF-EC9A-4F26-AE38-7D4E952083A9}.Debug|Any CPU.Build.0 = Debug|Any CPU {7317C8EF-EC9A-4F26-AE38-7D4E952083A9}.Release|Any CPU.ActiveCfg = Release|Any CPU {7317C8EF-EC9A-4F26-AE38-7D4E952083A9}.Release|Any CPU.Build.0 = Release|Any CPU + {421368B7-95A5-493C-94D2-E770FA33700B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {421368B7-95A5-493C-94D2-E770FA33700B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {421368B7-95A5-493C-94D2-E770FA33700B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {421368B7-95A5-493C-94D2-E770FA33700B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE