本文整理汇总了C#中MySql.Data.MySqlClient.MySqlConnection.QueryFirst方法的典型用法代码示例。如果您正苦于以下问题:C# MySqlConnection.QueryFirst方法的具体用法?C# MySqlConnection.QueryFirst怎么用?C# MySqlConnection.QueryFirst使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MySql.Data.MySqlClient.MySqlConnection
的用法示例。
在下文中一共展示了MySqlConnection.QueryFirst方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Activate
public static ResultCode Activate(string token)
{
try
{
Logger.Info("Entering AccountManager.Activate.");
Logger.Info("Activating new account.");
Logger.Info($"Token: {token}");
using (var connection = new MySqlConnection(Config.ConnectionString))
{
var sql = [email protected]"select USERNAME, CREATE_TIME from user_to_activate where TOKEN = '{token}'";
var activateTokenModel = connection.QueryFirst<ActivateToken>(sql);
if (activateTokenModel == null)
{
Logger.Info($"Query has no result. Query: {sql}");
Logger.Info("Leaving AccountManager.Activate.");
return ResultCode.InvalidToken;
}
if ((DateTime.Now - activateTokenModel.CreateTime).TotalHours >= ExpiredTime)
{
Logger.Info("Activate failed. Token expired.");
Logger.Info("Leaving AccountManager.Activate.");
sql = $"delete from user_to_activate where USERNAME = '{activateTokenModel.Username}'";
if (connection.Execute(sql) == 0)
{
Logger.Info($"Executing sql query failed. Query: {sql}");
Logger.Info("Leaving AccountManager.Activate.");
return ResultCode.Failed;
}
return ResultCode.ExpiredToken;
}
sql = $"update user set IS_ACTIVATED = 1 where USERNAME = '{activateTokenModel.Username}';";
if (connection.Execute(sql) == 0)
{
Logger.Info($"Executing sql query failed. Query: {sql}");
Logger.Info("Leaving AccountManager.Activate.");
return ResultCode.Failed;
}
sql = $"delete from user_to_activate where USERNAME = '{activateTokenModel.Username}'";
if (connection.Execute(sql) == 0)
{
Logger.Info($"Executing sql query failed. Query: {sql}");
Logger.Info("Leaving AccountManager.Activate.");
return ResultCode.Failed;
}
Logger.Info("Activating succeed.");
Logger.Info("Leaving AccountManager.Activate.");
return ResultCode.Succeed;
}
}
catch (Exception ex)
{
Logger.Error("Failed in activating. Server error.", ex);
Logger.Info("Leaving AccountManager.Activate.");
return ResultCode.ServerError;
}
}
示例2: Reset
public static ResultCode Reset(string token, string newPassword)
{
try
{
Logger.Info("Entering AccountManager.Reset.");
Logger.Info("Reseting account's password.");
Logger.Info($"Token: {token}");
using (var connection = new MySqlConnection(Config.ConnectionString))
{
var sql = [email protected]"select USERNAME, CREATE_TIME from user_to_reset where TOKEN = '{token}'";
var resetToken = connection.QueryFirst<ResetToken>(sql);
if (resetToken == null)
{
Logger.Info($"Query has no result. Query: {sql}");
Logger.Info("Leaving AccountManager.Reset.");
return ResultCode.InvalidToken;
}
if ((DateTime.Now - resetToken.CreateTime).TotalHours >= ExpiredTime)
{
Logger.Info("Reset failed. Token expired.");
Logger.Info("Leaving AccountManager.Activate.");
sql = $"delete from user_to_reset where USERNAME = '{resetToken.Username}'";
if (connection.Execute(sql) == 0)
{
Logger.Info($"Executing sql query failed. Query: {sql}");
Logger.Info("Leaving AccountManager.Reset.");
return ResultCode.Failed;
}
return ResultCode.ExpiredToken;
}
sql =
$"update user set PASSWORD = '{newPassword}' where USERNAME = '{resetToken.Username}';";
if (connection.Execute(sql) == 0)
{
Logger.Info($"Executing sql query failed. Query: {sql}");
Logger.Info("Leaving AccountManager.Reset.");
return ResultCode.Failed;
}
sql = $"delete from user_to_reset where USERNAME = '{resetToken.Username}'";
if (connection.Execute(sql) == 0)
{
Logger.Info($"Executing sql query failed. Query: {sql}");
Logger.Info("Leaving AccountManager.Reset.");
return ResultCode.Failed;
}
Logger.Info("Resetting succeed.");
Logger.Info("Leaving AccountManager.Reset.");
return ResultCode.Succeed;
}
}
catch (Exception ex)
{
Logger.Error("Failed in resetting. Server error.", ex);
Logger.Info("Leaving AccountManager.Reset.");
return ResultCode.ServerError;
}
}
示例3: TryReset
public static ResultCode TryReset(string username, string newPassword, string email)
{
try
{
Logger.Info("Entering AccountManager.TryReset.");
Logger.Info("Trying to reset account's password.");
Logger.Info($"Username: {username}");
Logger.Info($"Email: {email}");
var sql = $"select * from user where USERNAME = '{username}' and EMAIL = '{email}';";
using (var connection = new MySqlConnection(Config.ConnectionString))
{
var userModel = connection.QueryFirst<User>(sql);
if (userModel == null)
{
Logger.Info($"Query shouldn't has result. Query: {sql}");
Logger.Info("Leaving AccountManager.Register.");
return ResultCode.UnmatchedUsernameAndEmail;
}
var token = EncryptHelper.Encrypt(Guid.NewGuid() + username, 1);
sql =
[email protected]"insert into user_to_reset (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: ResetMailSubject,
content: string.Format(ResetMailContent, token));
Logger.Info("Trying to reset succeed.");
Logger.Info("Leaving AccountManager.TryReset.");
return ResultCode.Succeed;
}
}
catch (Exception ex)
{
Logger.Error("Failed in trying to reset. Server error.", ex);
Logger.Info("Leaving AccountManager.Register.");
return ResultCode.ServerError;
}
}