本文整理汇总了C#中MySql.Data.MySqlClient.MySqlConnection.QueryFirstOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# MySqlConnection.QueryFirstOrDefault方法的具体用法?C# MySqlConnection.QueryFirstOrDefault怎么用?C# MySqlConnection.QueryFirstOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MySql.Data.MySqlClient.MySqlConnection
的用法示例。
在下文中一共展示了MySqlConnection.QueryFirstOrDefault方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Login
public static ResultCode Login(string username, string password, ref string key)
{
try
{
Logger.Info("Entering UserManager.Login.");
Logger.Info("User logging in.");
Logger.Info($"Username: {username}");
Logger.Info($"Key: {key}");
if (string.IsNullOrWhiteSpace(key))
{
Logger.Info("Logging in failed. No key.");
Logger.Info("Leaving UserManager.Login.");
return ResultCode.ServerError;
}
if (string.IsNullOrWhiteSpace(username) ||
string.IsNullOrWhiteSpace(password))
{
Logger.Info("Logging in failed. Wrong username or password.");
Logger.Info("Leaving UserManager.Login.");
return ResultCode.WrongUsernameOrPassword;
}
using (var connection = new MySqlConnection(Config.ConnectionString))
{
var sql = [email protected]"select * from user where USERNAME = '{username}';";
var userModel = connection.QueryFirstOrDefault<User>(sql);
if (userModel?.Password != password)
{
Logger.Info("Logging in failed. Wrong username or password.");
Logger.Info("Leaving UserManager.Login.");
return ResultCode.WrongUsernameOrPassword;
}
var currentTime = CurrentTime;
sql = [email protected]"update user set LAST_ACTIVE_TIME = '{currentTime}' where USERNAME = '{userModel.Username}';";
connection.Execute(sql);
userModel.LastActiveTime = DateTime.Parse(currentTime);
if (CurrentUsers.Any(x => x.Value.Username == username))
{
Logger.Info("Already logged in, update key.");
key = CurrentUsers.Where(x => x.Value.Username == username).Select(x => x.Key).FirstOrDefault();
}
else
{
CurrentUsers.Add(key, userModel);
}
Logger.Info("Logging in succeed.");
Logger.Info("Leaving UserManager.Login.");
return ResultCode.Succeed;
}
}
catch (Exception ex)
{
Logger.Error("Logging in failed. Server error.", ex);
Logger.Info("Leaving UserManager.Login.");
return ResultCode.ServerError;
}
}
示例2: Register
public static ResultCode Register(string username, string password, string email)
{
try
{
Logger.Info("Entering AccountManager.Register.");
Logger.Info("Registering new account.");
Logger.Info($"Username: {username}");
Logger.Info($"Email: {email}");
if (!ValidateUserName(username))
{
Logger.Info("Invalid username.");
Logger.Info("Leaving AccountManager.Register.");
return ResultCode.InvalidUsername;
}
using (var connection = new MySqlConnection(Config.ConnectionString))
{
var sql = [email protected]"select USERNAME, PASSWORD, EMAIL from user where USERNAME = '{username}';";
var user = connection.QueryFirstOrDefault<Register>(sql);
if (user != null)
{
Logger.Info($"Query shouldn't has result. Query: {sql}");
Logger.Info("Leaving AccountManager.Register.");
return ResultCode.ConflictingUsername;
}
sql =
[email protected]"insert into user (USERNAME, PASSWORD, LAST_ACTIVE_TIME, EMAIL) values('{username}','{password}','{CurrentTime}','{email}');";
if (connection.Execute(sql) == 0)
{
Logger.Info($"Executing sql query failed. Query: {sql}");
Logger.Info("Leaving AccountManager.Register.");
return ResultCode.Failed;
}
var token = EncryptHelper.Encrypt(Guid.NewGuid() + username, 1);
sql =
[email protected]"insert into user_to_activate (USERNAME, TOKEN, CREATE_TIME) values('{username}','{token}','{CurrentTime}');";
if (connection.Execute(sql) == 0)
{
Logger.Info($"Executing sql query failed. Query: {sql}");
Logger.Info("Leaving AccountManager.Register.");
return ResultCode.Failed;
}
MailHelper.SendMail(
"noreply",
new[] { email },
subject: ActivateMailSubject,
content: string.Format(ActivateMailContent, token));
Logger.Info("Registering succeed.");
Logger.Info("Leaving AccountManager.Register.");
return ResultCode.Succeed;
}
}
catch (Exception ex)
{
Logger.Error("Failed in registering. Server error.", ex);
Logger.Info("Leaving AccountManager.Register.");
return ResultCode.ServerError;
}
}