You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OMS.NET/DbClass/AccountData.cs

112 lines
5.9 KiB
C#

using MySql.Data.MySqlClient;
namespace OMS.NET.DbClass
{
public class AccountData
{
#region base
public string UserEmail { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public int? MapLayer { get; set; }
public string? DefaultA1 { get; set; }
public string? SavePoint { get; set; }
public string? UserQQ { get; set; }
public string? HeadColor { get; set; }
public int Mode { get; set; }
public int Phase { get; set; }
public string? Custom { get; set; }
public AccountData()
{
this.UserEmail = "";
this.UserName = "";
this.Password = "";
this.Mode = 1;
this.Phase = 1;
//this.Custom = "";
}
#endregion
public static void Add(AccountData accountData)
{
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
connection.Open();
var query = @"INSERT INTO account_data (user_email, user_name, pass_word, map_layer, default_a1, save_point, user_qq, head_color, mode, phase, custom)
VALUES (@UserEmail, @UserName, @Password, @MapLayer, @DefaultA1, @SavePoint, @UserQQ, @HeadColor, @Mode, @Phase, @Custom)";
using var command = new MySqlCommand(query, connection);
command.Parameters.AddWithValue("@UserEmail", accountData.UserEmail);
command.Parameters.AddWithValue("@UserName", accountData.UserName);
command.Parameters.AddWithValue("@Password", accountData.Password);
command.Parameters.AddWithValue("@MapLayer", accountData.MapLayer ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@DefaultA1", accountData.DefaultA1 ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@SavePoint", accountData.SavePoint ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@UserQQ", accountData.UserQQ ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@HeadColor", accountData.HeadColor ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Mode", accountData.Mode);
command.Parameters.AddWithValue("@Phase", accountData.Phase);
command.Parameters.AddWithValue("@Custom", accountData.Custom ?? (object)DBNull.Value);
command.ExecuteNonQuery();
}
public static void Update(AccountData accountData)
{
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
connection.Open();
var query = @"UPDATE account_data SET user_name = @UserName, pass_word = @Password, map_layer = @MapLayer, default_a1 = @DefaultA1, save_point = @SavePoint, user_qq = @UserQQ, head_color = @HeadColor, mode = @Mode, phase = @Phase, custom = @Custom
WHERE user_email = @UserEmail";
using var command = new MySqlCommand(query, connection);
command.Parameters.AddWithValue("@UserEmail", accountData.UserEmail);
command.Parameters.AddWithValue("@UserName", accountData.UserName);
command.Parameters.AddWithValue("@Password", accountData.Password);
command.Parameters.AddWithValue("@MapLayer", accountData.MapLayer ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@DefaultA1", accountData.DefaultA1 ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@SavePoint", accountData.SavePoint ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@UserQQ", accountData.UserQQ ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@HeadColor", accountData.HeadColor ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Mode", accountData.Mode);
command.Parameters.AddWithValue("@Phase", accountData.Phase);
command.Parameters.AddWithValue("@Custom", accountData.Custom ?? (object)DBNull.Value);
command.ExecuteNonQuery();
}
public static void Delete(string userEmail)
{
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
connection.Open();
var query = @"DELETE FROM account_data WHERE user_email = @UserEmail";
using var command = new MySqlCommand(query, connection);
command.Parameters.AddWithValue("@UserEmail", userEmail);
command.ExecuteNonQuery();
}
public static AccountData? Get(string userEmail)
{
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
connection.Open();
var query = @"SELECT user_email, user_name, pass_word, map_layer, default_a1, save_point, user_qq, head_color, mode, phase, custom
FROM account_data WHERE user_email = @UserEmail";
using var command = new MySqlCommand(query, connection);
command.Parameters.AddWithValue("@UserEmail", userEmail);
using var reader = command.ExecuteReader();
if (reader.Read())
{
return new AccountData
{
UserEmail = reader.GetString("user_email"),
UserName = reader.GetString("user_name"),
Password = reader.GetString("pass_word"),
MapLayer = reader["map_layer"] as int?,
DefaultA1 = reader["default_a1"] as string,
SavePoint = reader["save_point"] as string,
UserQQ = reader["user_qq"] as string,
HeadColor = reader["head_color"] as string,
Mode = reader.GetInt32("mode"),
Phase = reader.GetInt32("phase"),
Custom = reader["custom"] as string,
};
}
return null;
}
}
}