當前位置: 首頁>>代碼示例>>C#>>正文


C# MySqlConnection.QueryFirst方法代碼示例

本文整理匯總了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;
            }
        }
開發者ID:yhvicey,項目名稱:ConfigSync,代碼行數:74,代碼來源:AccountManager.cs

示例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;
            }
        }
開發者ID:yhvicey,項目名稱:ConfigSync,代碼行數:75,代碼來源:AccountManager.cs

示例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;
            }
        }
開發者ID:yhvicey,項目名稱:ConfigSync,代碼行數:56,代碼來源:AccountManager.cs


注:本文中的MySql.Data.MySqlClient.MySqlConnection.QueryFirst方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。