diff --git a/ASP.Net Core/SqlConnector/SqlConnector.sln b/ASP.Net Core/SqlConnector/SqlConnector.sln new file mode 100644 index 0000000..fe033ac --- /dev/null +++ b/ASP.Net Core/SqlConnector/SqlConnector.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27130.2010 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlConnector", "SqlConnector\SqlConnector.csproj", "{A282DD85-07A7-45A8-A23C-A73B538A6624}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A282DD85-07A7-45A8-A23C-A73B538A6624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A282DD85-07A7-45A8-A23C-A73B538A6624}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A282DD85-07A7-45A8-A23C-A73B538A6624}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A282DD85-07A7-45A8-A23C-A73B538A6624}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {18A8B77D-030B-49F4-A8CA-5BA03B2B538B} + EndGlobalSection +EndGlobal diff --git a/ASP.Net Core/SqlConnector/SqlConnector/Program.cs b/ASP.Net Core/SqlConnector/SqlConnector/Program.cs new file mode 100644 index 0000000..3741ebe --- /dev/null +++ b/ASP.Net Core/SqlConnector/SqlConnector/Program.cs @@ -0,0 +1,62 @@ +using System; +using System.Data.SqlClient; + +namespace SqlConnector +{ + class Program + { + static void Main(string[] args) + { + // Annouce search + Console.WriteLine("Searching for all users in SQL database..."); + + // Open connection to SQL Server + using (var sqlConnection = new SqlConnection("Server=.;Database=test;User Id=testuser; Password = testuser;")) + { + // Log it + Console.WriteLine("Opening connection..."); + + // Attemp to open connection + sqlConnection.Open(); + + // Inject a user into the database + // NOTE: Command this out once it has run once if you don't want + // to re-adding users every time you run + using (var command = new SqlCommand($"INSERT INTO dbo.Users (Id, Username, FirstName, LastName, IsEnabled, CreatedDateUtc) VALUES ('{Guid.NewGuid().ToString("N")}', 'Username1', 'My first name', 'My last name', 1, '10/12/2025 12:32:10 +01:00')", sqlConnection)) + { + // Log it + Console.WriteLine("Adding new user..."); + + // Execute INSERT command + var result = command.ExecuteNonQuery(); + + // Log what should be "1 user added" + Console.WriteLine($"{result} user added"); + } + + // Select all users from database + using (var command = new SqlCommand("SELECT * FROM dbo.Users", sqlConnection)) + { + // Log it + Console.WriteLine("Selecting all users..."); + + // Execute selection as a reader to read each row... + using (var reader = command.ExecuteReader()) + { + // While we have a result + while (reader.Read()) + { + Console.WriteLine($"Username: {reader["Username"]}, First Name: {reader["FirstName"]}, LastName: {reader["LastName"]}, IsEnabled: {reader["IsEnabled"]}"); + } + } + } + } + + // Log it + Console.WriteLine("Done. Press any key to exit..."); + + // Keep window open + Console.Read(); + } + } +} diff --git a/ASP.Net Core/SqlConnector/SqlConnector/SqlConnector.csproj b/ASP.Net Core/SqlConnector/SqlConnector/SqlConnector.csproj new file mode 100644 index 0000000..e40366c --- /dev/null +++ b/ASP.Net Core/SqlConnector/SqlConnector/SqlConnector.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp2.0 + + + + + + +