Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 附加信息:在预期条件的上下文中指定的非布尔型表达式,靠近:_C#_Wpf - Fatal编程技术网

C# 附加信息:在预期条件的上下文中指定的非布尔型表达式,靠近:

C# 附加信息:在预期条件的上下文中指定的非布尔型表达式,靠近:,c#,wpf,C#,Wpf,伙计们,我正在尝试执行这个多登录页面,在c wpf中,我是一个编程初学者,我从youtube频道遵循了每一步,但没有任何帮助 private void button_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(@"Data Source=root**strong text**\SQLEXPRESS;Initial Catalog=Log-In;Integrated Se

伙计们,我正在尝试执行这个多登录页面,在c wpf中,我是一个编程初学者,我从youtube频道遵循了每一步,但没有任何帮助

private void button_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=root**strong text**\SQLEXPRESS;Initial Catalog=Log-In;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("select * from Login where username"+txt_usr.Text+"password"+txt_pass.Text+"", con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        string cmbitemvalue = comboBox1.SelectedItem.ToString();
        if (dt.Rows.Count > 0)
        {
            for(int i = 0; i<dt.Rows.Count; i++)
            {
              if(dt.Rows[i]["usertype"].ToString()==cmbitemvalue)
                {
                    MessageBox.Show("you are login as " + dt.Rows[i][2]);
                    if (comboBox1.SelectedIndex == 0)
                    {
                        Admin aa = new Admin();
                            aa.Show();
                        this.Hide();
                    }else
                    {
                        Student ss = new Student();
                        ss.Show();
                        this.Hide();
                    }
                }
            }
        }

    }
}
}

您正在创建的SQL查询看起来无效 让我们关注用户名部分

我可以想象txt_usr.Text包含用户名,比如leo。按照你写的方式,这将给出:

"select * from Login where username"+txt_usr.Text
即:

select * from Login where usernameleo
因此,错误的原因是,这里没有条件,只有无效列的名称

正确的查询如下所示:

select * from Login where username='leo'
还是这个

select * from Login where username="leo"
根据使用SQL Server、SQLite、MYSQL、Postgresql等的数据库类型

如果需要2个条件,正确的语法通常是使用和

因此,您缺少一个等号、布尔AND和正确的引号

还不止这些

!!! 您的SQL查询易受SQL注入攻击,请使用.NET SQL参数!!! 说明:

在C中,正确的方法是使用,而不是使用用户在文本框中输入的任何内容,包括潜在的恶意代码

这些将为您清理输入,代码中的内容也将更加清晰,您无需担心引用:

SqlCommand cmd = new SqlCommand("select * from Login where username=@username AND password=@password", con);
cmd.AddParameter('@username', txt_usr.Text);
cmd.AddParameter('@password', txt_pass.Text);
即使这是一个家庭作业,我们都看到过太多的网站/公司被黑客攻击,数据被破坏或窃取如此容易,因为这种漏洞已经成为可能,我认为这里的大多数数据库和安全专家都在不断强调,你需要快速学习,永远不要使用未经认可的输入进行sql查询

其他安全考虑 事实上,数据库中的密码似乎是明文的,这也是一个值得关注的问题,请搜索有关明文密码的信息,并正确地哈希和salt密码,以了解更多信息


但这比SQL参数和程序中的几行额外代码要理解得多。

这是错误的->用户名+txt_usr.Text+密码+txt_pass.Text+检查+1以查看Sandris的注释。您的Select语句不应该像您这样构建字符串,因为有一种叫做Sql注入的东西。根据@SandrisB的注释重构您的代码,并给我们一个update.SQL注入。除此之外,这非常重要,学会修复它,现在修复它,即使这只是家庭作业,不要再这样做,WHERE条件的SQL语法是SELECT。。。。其中用户名='foo'和密码='secretbar'。根据变量中的内容,可能会导致此错误。按照上面链接的建议使用SQL参数,并确保结果使用参数,且参数为'equal'=sign@Pac0你应该把它作为回答发出去。@KeithStein你说得对,完成了
SqlCommand cmd = new SqlCommand("select * from Login where username=@username AND password=@password", con);
cmd.AddParameter('@username', txt_usr.Text);
cmd.AddParameter('@password', txt_pass.Text);