Skip to content

Commit

Permalink
Learn Execute Row Sql Select Statement
Browse files Browse the repository at this point in the history
  • Loading branch information
WALEED-NET committed Aug 31, 2023
1 parent 891de8d commit c34d288
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
32 changes: 30 additions & 2 deletions 02_ExecuteRowSql/Program.cs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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();
}
}
Expand Down
15 changes: 15 additions & 0 deletions 02_ExecuteRowSql/Wallet.cs
Original file line number Diff line number Diff line change
@@ -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})";
}

}
}
8 changes: 7 additions & 1 deletion EF-Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c34d288

Please sign in to comment.