c#winform中的"if else"问题 [英] problem with 'if else' in c# winform

查看:137
本文介绍了c#winform中的"if else"问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友;
我在使用winform设计的日志记录表单时遇到问题,这是我的代码

hi friends;
i am having a problem working with my loging form which i design in winform here is my code

private void btnLogin_Click(object sender, EventArgs e)
      {
          user = txtUsername.Text;
          pass = txtPassword.Text;
          role = cbRole.Text;
          SqlConnection con = new SqlConnection();
          con.ConnectionString = "Data Source=ESE-HP;Initial Catalog=PIMS;User ID=sa;PWD=ese";
          con.Open();
          SqlCommand cmd = new SqlCommand("select * from employee where  occupation = '" + role + "' and password = '" + pass + "' and username = '" + user + "' ", con);
          SqlDataReader dr = cmd.ExecuteReader();


          while (dr.Read())
          {
              if ((dr["Password"].ToString() == pass && dr["Username"].ToString() == user && dr["Occupation"].ToString() == role))
              {
                  messageBox.Show("Login Successful");
              }

             else
              {
                  MessageBox.Show("LOGIN NOT SUCCESSFUL );
              }
            }
      }



运行代码并输入正确的凭据时,我有一个包含用户名,密码和角色文本框的登录表单,它显示消息"LOGIN SUCCESSFUL",但是如果我在框中输入的信息不正确,则不会显示未成功登录"的其他"消息,我不知道问题出在哪里,有人可以帮我吗?



i have a login form that has username, password and role textbox when i run the code and enter my correct credentials, it displays the message "LOGIN SUCCESSFUL", but if i did not enter the correct information in the boxes, it wont display the ''else'' message which is "LOGIN NOT SUCCESSFUL", i do not know what the problem is, can someone help me out? thanks.

推荐答案

您正在多余地检查传递的凭据:第一次使用SQL查询,第二次使用循环内的代码.
你可能会改变
You are redundantly checking the passed credentials: the first time with the SQL query, the second time with code inside the loop.
You might change
报价:

while(dr.Read())
{
if((dr ["Password"].ToString()==通过&& dr ["Username"].ToString()==用户&& dr ["Occupation"].ToString()==角色))
{
messageBox.Show(登录成功");
}

其他
{
MessageBox.Show("LOGIN NOT SUCCESSFUL);
}
}

while (dr.Read())
{
if ((dr["Password"].ToString() == pass && dr["Username"].ToString() == user && dr["Occupation"].ToString() == role))
{
messageBox.Show("Login Successful");
}

else
{
MessageBox.Show("LOGIN NOT SUCCESSFUL );
}
}




to

if ( dr.hasRows )
  messageBox.Show("Login Successful");
else
  messageBox.Show("LOGIN NOT SUCCESSFUL");


试试这个
使用try catch块,并获取变量中的数据库凭证信息,然后再将其与用户输入进行比较,并尝试使用&而不是&&
Try this
use try catch block, and get the database credentials informations in variable before compare them to the user input and try to use & rather than &&


尝试一下:

Try this:

private void btnLogin_Click(object sender, EventArgs e)
{
            user = txtUsername.Text;
            pass = txtPassword.Text;
            role = cbRole.Text;
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=ESE-HP;Initial Catalog=PIMS;User ID=sa;PWD=ese";
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from employee where  occupation = ''" + role + "'' and password = ''" + pass + "'' and username = ''" + user + "'' ", con);
            SqlDataReader dr = cmd.ExecuteReader();
 

if (dr != null)messageBox.Show("Login Successful");
else MessageBox.Show("LOGIN NOT SUCCESSFUL );
 
}


这篇关于c#winform中的"if else"问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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