Database connection with ADO

This commit is contained in:
Spudnut2000
2025-06-11 22:54:55 -05:00
parent b20b026aff
commit 6324c85080
8 changed files with 117 additions and 3 deletions

View File

@@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
<PackageReference Include="MySql.Data" Version="9.3.0" />
<PackageReference Include="MySql.EntityFrameworkCore" Version="9.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

View File

@@ -0,0 +1,63 @@
using C969Project.Data.Models;
using MySql.Data.MySqlClient;
namespace C969Project.Data;
public static class DatabaseHelper
{
public static List<User> 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);
}
}
}

View File

@@ -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; }
}

View File

@@ -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");
}
}
}

View File

@@ -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.

View File

@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"DefaultConnection": ""
"DefaultConnection": "server=localhost;port=3306;database=client_schedule;uid=sqlUser;password=Passw0rd!"
},
"AppSettings": {
"DefaultLocale": "en-US"