代码之家  ›  专栏  ›  技术社区  ›  Branson Street

我如何到达我的登录将接受电子邮件或用户名字段中的用户名的位置

  •  0
  • Branson Street  · 技术社区  · 1 年前
    namespace Login
    {
        public partial class logForm : Form
        {
            public logForm()
            {
                InitializeComponent();
            }
    
            SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=testData;Integrated Security=True;Encrypt=True;TrustServerCertificate=True");
            
            private void logUser_Click(object sender, EventArgs e)
            {
                String username, user_password;
                username = txt_user.Text;
                user_password = txt_pass.Text;
    
                try
                {
                    String query = "SELECT * FROM loginTable WHERE email or username = '"+txt_user.Text+"' AND password = '"+txt_pass.Text+"'";
                    SqlDataAdapter sda = new SqlDataAdapter(query, con);
    
                    DataTable dtable = new DataTable();
                    sda.Fill(dtable);
    
                    if (dtable.Rows.Count > 0) //reads dataTable
                    {
                        username = txt_user.Text;
                        user_password = txt_pass.Text;
    
                        Form1 f1 = new Form1(); //opens window after successful login
                        f1.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("Incorrect login Information, please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txt_user.Clear(); //clears inputs after failure
                        txt_pass.Clear();
                        txt_user.Focus();
                    }
                }
                catch
                {
                    MessageBox.Show("Error");
                }
                finally
                {
                    con.Close();
                }
            }
        }
    }
    

    在字符串查询行中,当我只有 username = txt_user email = text_user ,但当我试图将两者进行集成以使两者都有例外时,这是不起作用的。我的主要关注点是能够在登录字段中同时使用电子邮件或用户名。我也试着重新排序写OR语句的方式,所以我不知道答案是什么。

    1 回复  |  直到 1 年前
        1
  •  1
  •   Dale K    1 年前
    con.Open();
    String query = "SELECT * FROM loginTable WHERE (email = @usernameOrEmail OR username = @usernameOrEmail) AND password = @password";
    SqlCommand cmd = new SqlCommand(query, con);
    cmd.Parameters.Add("@usernameOrEmail", SqlDbType.NVarChar).Value = usernameOrEmail; 
    cmd.Parameters.Add("@password", SqlDbType.NVarChar).Value = user_password;
    
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable dtable = new DataTable();
    sda.Fill(dtable);
    

    参数化查询以防止SQL注入,因此您可以在查询中看到更新。我将这些参数添加到指定的变量中(您声明了)。OR的优先级低于AND,后者使您试图构建的预期查询失败->

    (email = 'inputValue') OR (username = 'inputValue' AND password = 'inputPassword')
    

    要正确检查电子邮件或用户名是否匹配,以及密码是否匹配,您需要将or条件与括号组合在一起:

    SELECT *
    FROM loginTable
    WHERE (email = 'inputValue' OR username = 'inputValue')
    AND password = 'inputPassword'