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


C# MySqlConnection.QueryFirstOrDefault方法代碼示例

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

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


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