C# 否则…如果语句在我的表单中不起作用。。。?我可以用try-catch来代替吗。。?

C# 否则…如果语句在我的表单中不起作用。。。?我可以用try-catch来代替吗。。?,c#,visual-studio,visual-studio-2012,C#,Visual Studio,Visual Studio 2012,我正在用用户名和密码创建一个登录窗口。我有一个包含用户信息的数据表,当用户尝试登录时(在代码末尾),我正在使用else..if语句。假设它检查用户名和密码,并验证它是否与表中的内容匹配。如果没有,则会出现一个messagebox。另外,对于最后一个else…if语句,用户看门人根本不登录!是什么导致了这个问题?除了最后一个用户看门人和我的messagebox没有出现之外,他们都可以正常登录 using System; using System.Collections.Generic; using

我正在用用户名和密码创建一个登录窗口。我有一个包含用户信息的数据表,当用户尝试登录时(在代码末尾),我正在使用else..if语句。假设它检查用户名和密码,并验证它是否与表中的内容匹配。如果没有,则会出现一个messagebox。另外,对于最后一个else…if语句,用户看门人根本不登录!是什么导致了这个问题?除了最后一个用户看门人和我的messagebox没有出现之外,他们都可以正常登录

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace LOGINPAGE
{
    public partial class Room : Form
    {
        public Room()
        {
            InitializeComponent();
            PassText.PasswordChar = '*';
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            FloorSelection ss = new FloorSelection();
            ss.Show();

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }


        private void EXIT_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }

        private void xButton1_Click(object sender, EventArgs e)
        {



            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Mohamed\Documents\UserData.mdf;Integrated Security=True;Connect Timeout=30");
            SqlDataAdapter sda = new SqlDataAdapter("Select Count (*) From dbo.[LOGIN] where username='" + UserText.Text + "' and Password ='" + PassText.Text + "'", con);
            FloorSelection ss = new FloorSelection();
            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows[0][0].ToString() == "1")
            {
                SqlDataAdapter sda1 = new SqlDataAdapter("Select TYPE From dbo.[LOGIN] where username='" + UserText.Text + "' and Password ='" + PassText.Text + "'", con);
                FloorSelection ss1 = new FloorSelection();
                DataTable dt1 = new DataTable();


                sda1.Fill(dt1);
                if (dt1.Rows[0][0].ToString() == "FACULTY")
                {
                    this.Hide();
                    FACULTY ff = new FACULTY();
                    ff.Show();
                }

                else if (dt1.Rows[0][0].ToString() == "ADMINISTRATOR")
                {
                    this.Hide();
                    ADMINISTRATOR Ad = new ADMINISTRATOR();
                    Ad.Show();
                }

                else if (dt1.Rows[0][0].ToString() == "JANITOR")
                {
                    this.Hide();
                    JANITOR jt = new JANITOR();
                    jt.Show();

                }
                else
                {

                    MessageBox.Show("Please check your username and password");
                }
            }
        }
        private void label3_Click(object sender, EventArgs e)
        {
            label3.BackColor = Color.Transparent;
        }

        private void UserText_TextChanged(object sender, EventArgs e)
        {
            UserText.BackColor = Color.Empty;
        }

        private void PassText_TextChanged(object sender, EventArgs e)
        {
            PassText.BackColor = Color.Empty;
        }
    }
}

逻辑流没有问题。我猜您的输入中有空白。试试这个:

string userName = UserText.Text.Trim();
string passwrd = PassText.Text.Trim();
这将从输入中删除空格和制表符。现在,用userName替换所有使用UserText.Text的位置,并用passwrd替换PassText.Text

您可能还希望尝试为该类型字段创建一个变量,如下所示:

string userType = dt1.Rows[0][0].ToString().Trim();
例如:

private void xButton1_Click(object sender, EventArgs e)
{
    string userName = UserText.Text.Trim();
    string passwrd = PassText.Text.Trim();

    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Mohamed\Documents\UserData.mdf;Integrated Security=True;Connect Timeout=30");
    SqlDataAdapter sda = new SqlDataAdapter("Select Count (*) From dbo.[LOGIN] where username='" + userName + "' and Password ='" + passwrd  + "'", con);

    DataTable dt = new DataTable();
    sda.Fill(dt);
我希望这有帮助

我假设这是一个学习项目,不会在现实世界中使用。如果没有,请重新思考你在做什么。你真的不应该使用你自己的安全系统。您的代码中存在一个典型的SQL注入攻击漏洞,该漏洞会危害整个系统

如果这是一个学校项目,继续你正在做的事情,但是你可能想问你的教授什么是SQL注入攻击


祝你好运

替换名字会把整个项目搞砸,因为我必须修理所有的窗户。还有别的办法吗?也许使用不同的功能?你不需要“替换”任何东西。只需声明并使用xButton1_Click方法中的变量。这样,您就不会影响该方法之外的任何内容,而只会清理该方法内部使用的输入。我应该将
字符串
变量放置在您的答案中的什么位置?这似乎解决了我的登录问题,但我的messagebox问题呢?当输入错误的凭证时,它会弹出!将messagebox的else语句移到内部ifs之外。Else应该与第一个相关,如果不是其他的。