当前位置: 首页>>代码示例>>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;未经允许,请勿转载。