C#|登录不起作用

时间:2017-06-05 11:09:53

标签: c# .net sql-server wpf

我无法弄清楚为什么我的登录系统无法正常工作。

总是导致"错误"或者"错误的密码"我输入了正确的密码。

是的,我也检查过列名和表名。没有错,但没有用。

我无法弄清楚。

namespace GitGut {
    public partial class Login : Window {
        public String thisName;
        private String thisConnectionString = @"Data Source = THISISMINE\SQLEXPRESS;Initial Catalog = GitGut; Integrated Security = True";
        public Login() {
            InitializeComponent();
        }

        private void Button_Login_Click(object sender, RoutedEventArgs e) {
            Console.WriteLine("Button_Login pressed.");
            if(theValidation(TextBox_Username.Text, PasswordBox_Password.Password) == true) {
                Dashboard Form_Dashboard = new Dashboard();
                Form_Dashboard.Show();
                this.Hide();
            } else {
                MessageBox.Show("ERROR", "ERROR");
            }
        }

        private Boolean theValidation(String thisUsername, String thisPassword) {
            SqlConnection thisSqlConnection = new SqlConnection(thisConnectionString);
            SqlCommand thisSqlCommand = new SqlCommand();
            thisSqlCommand.Parameters.Add("@Username", SqlDbType.VarChar).Value = thisUsername;
            thisSqlCommand.Parameters.Add("@Password", SqlDbType.VarChar).Value = thisPassword;
            String thisQuery = "SELECT * FROM [User] WHERE [Username] = '@Username' AND [Password] = '@Password'";
            thisSqlCommand.Connection = thisSqlConnection;
            thisSqlCommand.CommandText = thisQuery;
            thisSqlConnection.Open();
            SqlDataReader thisSqlDataReader = thisSqlCommand.ExecuteReader();
            if(thisSqlDataReader.Read()) {
                thisSqlConnection.Close();
                return true;
            } else {
                thisSqlConnection.Close();
                return false;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您应该从查询字符串中删除String thisQuery = "SELECT * FROM [User] WHERE [Username] = @Username AND [Password] = @Password";

thisSqlCommand.Parameters.Add("@Password", SqlDbType.VarChar)...

Sqlcommand参数不需要任何分隔符/指示符。您已经知道它们在var array = []; for (var i = 0; i < events.length; i++) { array[events[i]['pokoj']][i] = events[i]; } console.log(array); 中的类型,因此正确构造了查询字符串。