C#Winform登录问题 [英] C# Winform Login Issue

查看:125
本文介绍了C#Winform登录问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次发帖,找到了很多好的答案,希望其他人可以从这个问题的解决方案中受益。



这可能是微不足道的,但我无法让我的登录表单显示无效的用户名或密码。我已经在远程MySQL服务器上设置了两个帐户,两者都可以正常登录但是如果我尝试拼写错误密码/用户名或者将它们完全留空,你可以根据需要按下按钮,它不会显示任何错误信息。



Hi, this is my first time posting, found a lot of good answers and hope others may benefit from the solution to this question.

It may be something trivial, but I cannot get my login form to display "Invalid username or password". I've set two accounts up on the remote MySQL server, both can log in fine however if I try misspelling the password/username or leave them blank completely, you can press the button as much as you want, it will not show any error message.

private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                dbConnect.login_initialise(); //creates connection string
                dbConnect.login_Open_Connection(); //opens the connection
                string login_query = "SELECT * FROM auth WHERE username=@username AND pass=@pass";
                MySqlCommand cmd = new MySqlCommand(login_query, dbConnect.connection);
                pass = txtPassword.Text;
                hashed_pass = dbConnect.hash_value(pass); //creates a hashed value for the password
                cmd.Parameters.AddWithValue("@username", txtUsername.Text);
                cmd.Parameters.AddWithValue("@pass", hashed_pass);
                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        if (reader.HasRows) 
                        {
                            MessageBox.Show("Welcome!");
                            this.Hide(); 
                            mainMenu mm = new mainMenu(); 
                            mm.Show(); 
                        }
                        else // if (reader.HasRows == false) so if no rows it shoud display this however is doesn't
                        {
                            MessageBox.Show("Wrong Username or Password");
                        }

                    }
                }
            }





很奇怪的部分关于这个,我发现的大部分研究,用户无论他们尝试什么都无法登录,但是我可以登录正常无法让它显示错误信息。



为了添加更多细节,我手动在数据库上创建了两个用户帐户并以两者身份登录,因此它显然是从数据库中读取的,但如果用户名错误输入obb而不是bob,则不会显示任何错误消息。



希望有人可能会在几秒钟内发现我出错了,这是一个非常令人困惑的问题。



非常感谢你的帮助!



(稍微偏离主题但可能是相关的,我有一个添加用户的表单,它做同样的事情,保持单击按钮,没有错误消息,它不是每次尝试连接并插入值)



Weird part about this, most of the research I found on this, users cannot log in no matter what they try however I can log in fine just can't get it to display the error message.

To add a bit more detail, I have created two user accounts on the DB manually and logged in as both so it is clearly reading from the DB but if mistyped "obb" instead of "bob" for the username, it will not show any error message.

Hopefully someone may spot in seconds where I am going wrong, this has been a really confusing issue.

Many thanks for all your help!

(Slightly off-topic but may be related, I have a form that adds users and it does the same thing, keep clicking the button and no error message it simply does not every try connection and inserting the values)

推荐答案

我有一些更改您的代码。试试这个
i have some changing your code. try this
  if( cmd.ExecuteNonQuery()>0) 
{
                         MessageBox.Show("Welcome!");
                            this.Hide(); 
                            mainMenu mm = new mainMenu(); 
                            mm.Show(); 
                        }
                        else // if (reader.HasRows == false) so if no rows it shoud display this however is doesn't
                        {
                            MessageBox.Show("Wrong Username or Password");
                        }



以及DataBase查询中的一些更改


And also some changing in DataBase Query

string login_query = "SELECT Count(*) FROM auth WHERE username=@username AND pass=@pass";





我希望这段代码能为您服务。任何查询点击回复免费的感觉。



I hope this code will be worked on your side. for any query hit to reply free feel.


这篇关于C#Winform登录问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆