0

私はこのようなデータテーブルを持っています。

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");
        }
4

1 に答える 1

2

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");
}
于 2012-12-11T08:44:43.923 に答える