Database connection with ADO
This commit is contained in:
63
C969Project/Data/DatabaseHelper.cs
Normal file
63
C969Project/Data/DatabaseHelper.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
22
C969Project/Data/Models/User.cs
Normal file
22
C969Project/Data/Models/User.cs
Normal 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; }
|
||||
}
|
||||
Reference in New Issue
Block a user