首页 > 解决方案 > 如何解决 Wpf System.Data.SqlClient.SqlException 中的以下错误:“'System.Windows.Controls.PasswordBox'附近的语法不正确。”

问题描述

我创建了一个 wpf(.net framework) 应用程序供用户登录,但我不断收到此错误:System.Data.SqlClient.SqlException: 'System.Windows.Controls.PasswordBox' 附近的语法不正确。

这是我的代码:

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    string mainconn = "Data Source=VCPTCR4PC19\\SQLEXPRESS;Initial Catalog=TASK2;Integrated Security=True";
    SqlConnection conn = new SqlConnection(mainconn);
    conn.Open();

    string cmds = $" Select * from USERS (@USERS_NAME, @USERS_PASSWORD ) ";
    SqlCommand command = new SqlCommand(cmds, conn);
    command.Parameters.Add("@USERS_NAME", SqlDbType.VarChar).Value= txtUsername.Text;
    command.Parameters.Add("@USERS_PASSWORD", SqlDbType.VarChar).Value = HashCode.PassHash(password.Password);
     

    SqlDataAdapter sqlData = new SqlDataAdapter("Select Count(*) From USERS Where USER_NAME = '" + txtUsername + "'and PASSWORD'" + password + "'", conn);
     
    DataTable dataTable = new DataTable();
    sqlData.Fill(dataTable);
    if (dataTable.Rows[0][0].ToString() == "1")                
    {
        this.Hide();
        Modules modules = new Modules();
        modules.ShowDialog();
    }
    else
    {
        MessageBox.Show("Invaild user name or password enterd");
    }
    conn.Close();
}

在此处输入图像描述

标签: c#

解决方案


推荐阅读