C# 登录身份验证取决于winforms的角色
我有这样的数据表C# 登录身份验证取决于winforms的角色,c#,winforms,C#,Winforms,我有这样的数据表 uid m_code pass roleID 1 F2 F2 2 2 S2 S2 0 我想让用户根据他们的角色登录 我试着使用这段代码,但根本不起作用。非常感谢您的帮助 string user = textBox1.Text; string pass = textBox2.Text; SqlConnection con = new SqlConnectio
uid m_code pass roleID
1 F2 F2 2
2 S2 S2 0
我想让用户根据他们的角色登录
我试着使用这段代码,但根本不起作用。非常感谢您的帮助
string user = textBox1.Text;
string pass = textBox2.Text;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
SqlCommand cmd = new SqlCommand("select * from login where m_code='" + user + "' and pass='" + pass + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Columns[3].ToString() == "0")
{
this.Hide();
StudentUI s = new StudentUI();
s.Show();
}
if (dt.Columns[3].ToString() == "1")
{
this.Hide();
TeacherUI t = new TeacherUI();
t.Show();
}
if (dt.Columns[3].ToString() == "2")
{
FacultyUI f = new FacultyUI();
f.Show();
}
else
{
MessageBox.Show("Login Failed");
}
我同意Blachshma的观点,您应该使用参数来降低Sql注入的风险。同时,让我们修正一下你的逻辑:
if(dt.Rows.Count == 0)
{
MessageBox.Show("Login Failed");
return;
}
string val = Convert.ToString(dt.Rows[0][3]);
if(val == "0")
{
this.Hide();
StudentUI s = new StudentUI();
s.Show();
}
else if (val == "1")
{
this.Hide();
TeacherUI t = new TeacherUI();
t.Show();
}
else if (val == "2")
{
FacultyUI f = new FacultyUI();
f.Show();
}
else
{
MessageBox.Show("Login Failed");
}
在解决问题之前-您应该知道您的代码存在安全风险,因为它对@Blachshma开放:祝您在Winforms应用程序上的SQL注入工作好运。@leppie-如果它是Winforms应用程序,又有什么关系呢?在TextBox1中键入
”或“=”
,将导致SQL注入。非常感谢。你已经解决了我花了大约2个小时的问题:)谢谢@Blachsma:)当一些鹅开始编辑我的帖子时,我有点分心了。如果你不介意的话,我该如何保护SQL注入?我对这些IT东西都是新手:P@TheCatalySt:这里有一个很好的例子-