From 6324c8508098f41c0e3f4bdf36324ca833070b9f Mon Sep 17 00:00:00 2001 From: Spudnut2000 Date: Wed, 11 Jun 2025 22:54:55 -0500 Subject: [PATCH] Database connection with ADO --- .idea/.idea.C969Project/.idea/dataSources.xml | 17 +++++ .idea/.idea.C969Project/.idea/sqldialects.xml | 6 ++ C969Project/C969Project.csproj | 1 + C969Project/Data/DatabaseHelper.cs | 63 +++++++++++++++++++ C969Project/Data/Models/User.cs | 22 +++++++ C969Project/LoginForm.cs | 6 +- C969Project/Program.cs | 3 + C969Project/appsettings.json | 2 +- 8 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 .idea/.idea.C969Project/.idea/dataSources.xml create mode 100644 .idea/.idea.C969Project/.idea/sqldialects.xml create mode 100644 C969Project/Data/DatabaseHelper.cs create mode 100644 C969Project/Data/Models/User.cs diff --git a/.idea/.idea.C969Project/.idea/dataSources.xml b/.idea/.idea.C969Project/.idea/dataSources.xml new file mode 100644 index 0000000..b77d257 --- /dev/null +++ b/.idea/.idea.C969Project/.idea/dataSources.xml @@ -0,0 +1,17 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306/client_schedule + + + + + + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/.idea.C969Project/.idea/sqldialects.xml b/.idea/.idea.C969Project/.idea/sqldialects.xml new file mode 100644 index 0000000..b7ed9fe --- /dev/null +++ b/.idea/.idea.C969Project/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/C969Project/C969Project.csproj b/C969Project/C969Project.csproj index d7dbd80..fd91a57 100644 --- a/C969Project/C969Project.csproj +++ b/C969Project/C969Project.csproj @@ -10,6 +10,7 @@ + diff --git a/C969Project/Data/DatabaseHelper.cs b/C969Project/Data/DatabaseHelper.cs new file mode 100644 index 0000000..658b03d --- /dev/null +++ b/C969Project/Data/DatabaseHelper.cs @@ -0,0 +1,63 @@ +using C969Project.Data.Models; +using MySql.Data.MySqlClient; + +namespace C969Project.Data; + +public static class DatabaseHelper +{ + public static List Users { get; private set; } = new(); + + public static void Initialize() + { + PopulateUsers(); + } + + public static User? GetUserById(int userId) + { + User user = Users.Find(u => u.UserId == userId); + return user; + } + + private static void PopulateUsers() + { + using MySqlConnection connection = new(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); + } + catch (MySqlException ex) + { + Console.WriteLine(ex.Message); + throw; + } + + string query = "SELECT * FROM client_schedule.user"; + + using MySqlCommand command = new(query, connection); + using MySqlDataReader reader = command.ExecuteReader(); + while (reader.Read()) + { + int id = reader.GetInt32("userId"); + string name = reader.GetString("userName"); + string password = reader.GetString("password"); + int active = reader.GetInt32("active"); + DateTime createDate = reader.GetDateTime("createDate"); + string createdBy = reader.GetString("createdBy"); + var lastUpdated = reader.GetDateTime("lastUpdate"); // Timestap + string lastUpdateBy = reader.GetString("lastUpdateBy"); + Console.WriteLine($"Id: {id}; Name: {name}; Password: {password}; Active: {active}; Created by: {createdBy}; Last Updated by: {lastUpdateBy}"); + User user = new() + { + UserId = id, + Username = name, + Password = password, + Active = active, + CreateDate = createDate, + CreatedBy = createdBy, + LastUpdate = lastUpdated, + LastUpdateBy = lastUpdateBy + }; + Users.Add(user); + } + } +} \ No newline at end of file diff --git a/C969Project/Data/Models/User.cs b/C969Project/Data/Models/User.cs new file mode 100644 index 0000000..e2df03c --- /dev/null +++ b/C969Project/Data/Models/User.cs @@ -0,0 +1,22 @@ +namespace C969Project.Data.Models; + +public class User +{ + // int id = reader.GetInt32("userId"); + // string name = reader.GetString("userName"); + // string password = reader.GetString("password"); + // int active = reader.GetInt32("active"); + // DateTime scheduled = reader.GetDateTime("createDate"); + // string createdBy = reader.GetString("createdBy"); + // var lastUpdated = reader.GetDateTime("lastUpdate"); // Timestap + // string lastUpdateBy = reader.GetString("lastUpdateBy"); + + public int UserId { get; set; } + public string Username { get; set; } + public string Password { get; set; } + public int Active { get; set; } + public DateTime CreateDate { get; set; } + public string CreatedBy { get; set; } + public DateTime LastUpdate { get; set; } + public string LastUpdateBy { get; set; } +} \ No newline at end of file diff --git a/C969Project/LoginForm.cs b/C969Project/LoginForm.cs index b2a3b1e..bee658f 100644 --- a/C969Project/LoginForm.cs +++ b/C969Project/LoginForm.cs @@ -6,12 +6,14 @@ namespace C969Project { InitializeComponent(); - // Determine user location and set the Localization CureentLanguage based on either US or IS location - loginLabel.Text = Localization.GetLocalization("login_text"); usernameTextBox.PlaceholderText = Localization.GetLocalization("username_text"); passwordTextBox.PlaceholderText = Localization.GetLocalization("password_text"); loginButton.Text = Localization.GetLocalization("login_text"); + + } + + } } diff --git a/C969Project/Program.cs b/C969Project/Program.cs index d028618..79b06c3 100644 --- a/C969Project/Program.cs +++ b/C969Project/Program.cs @@ -1,3 +1,5 @@ +using C969Project.Data; + namespace C969Project { internal static class Program @@ -10,6 +12,7 @@ namespace C969Project { AppSettings.Initialize(); Localization.Initialize(); + DatabaseHelper.Initialize(); // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. diff --git a/C969Project/appsettings.json b/C969Project/appsettings.json index da49e2f..f69ce03 100644 --- a/C969Project/appsettings.json +++ b/C969Project/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "" + "DefaultConnection": "server=localhost;port=3306;database=client_schedule;uid=sqlUser;password=Passw0rd!" }, "AppSettings": { "DefaultLocale": "en-US"