2013-05-10 1 views
1

C# 및 SQL의 사용자 수준에 따라 양식을 표시하려고하는데, User_ID, User_Pass 및 User_Level이있는 데이터 테이블이 있습니다. (직원에 대한 관리자 2 1) 사용자 수준 매우 감사합니다 :) 모든C# 및 SQL의 사용자 수준에 따라 폼을 표시하십시오.

private void button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     string connection = @"Data Source=Local-PC\HOME;Initial Catalog=Project;Integrated Security=True"; 
     SqlConnection cn = new SqlConnection(connection); 
     cn.Open(); 
     string userText = textBox1.Text; 
     string passText = textBox2.Text; 

     SqlCommand cmd = new SqlCommand("SELECT ISNULL(User_ID, '') AS User_ID, ISNULL(User_Pass,'') AS User_Pass, User_Level FROM User_Login WHERE User_ID = @User_ID and User_Pass = @User_Pass and User_Level = @User_Level", cn); 
     cmd.Parameters.Add(new SqlParameter("User_ID", userText)); 
     cmd.Parameters.Add(new SqlParameter("User_pass", passText)); 


     SqlDataReader dr = cmd.ExecuteReader(); 


     try 
     { 
      dr.Read(); 

      if (dr["User_ID"].ToString().Trim() == userText && dr["User_pass"].ToString().Trim() == passText && dr["User_Level"].ToString().Trim() == "1") 
      { 
       textBox3.Text = dr["User_ID"].ToString(); 
       this.Hide(); 
       Form2 form2 = new Form2(); 
       form2.Show(); 
       //this.Close(); 
      } 

      if (dr["User_ID"].ToString().Trim() == userText && dr["User_pass"].ToString().Trim() == passText && dr["User_Level"].ToString().Trim() == "2") 
      { 
       textBox3.Text = dr["User_ID"].ToString(); 
       this.Hide(); 
       Form3 form3 = new Form3(); 
       form2.Show(); 
       //this.Close(); 
      } 
     } 
     catch 
     { 
      MessageBox.Show("Invalid Username or Password"); 
     } 
     dr.Close(); 
     cn.Close(); 

    } 
    catch 
    { 

    } 
} 
+0

작동하지 않는 기능은 무엇입니까? – Max

+0

오류가 보이지 않고 무엇을 잘못하고 있는지 알 수 없습니다. – user2370201

+0

두 프로그램간에 중단 점을 설정할 수 있습니까? 프로그램을 실행하고 중단 점에서 중단되거나 중단 점에 도달하지 않는다고 알려 주시겠습니까? – Max

답변

1

우선에 따라 단어와 사용자 이름이 올바른지 전달하고 양식을 보여

SqlCommand cmd = new SqlCommand("SELECT ISNULL(User_ID, '') AS User_ID, 
             ISNULL(User_Pass,'') AS User_Pass, 
             User_Level 
           FROM User_Login 
           WHERE User_ID = @User_ID 
            AND User_Pass = @User_Pass 
            AND User_Level = @User_Level", cn); 

쿼리에 3 개의 매개 변수가 있지만 사용자는 2. 제공하는 당신은 선언해야합니다 세 번째로 :

cmd.Parameters.Add(new SqlParameter("User_Level", Int32.Parse(levelText))); 

또는

//AND User_Level = @User_Level" 

둘째로 당신의 WHERE 절에서 마지막 필터를 제거 : 당신은 확인합니까 왜

dr["User_ID"].ToString().Trim() == userTextdr["User_pass"].ToString().Trim() == passText 그 사실을 확실히 알고있는 동안. SQL 쿼리에서 필터링했습니다. 마지막으로

if (dr["User_Level"].ToString().Trim() == "1") //eventually = "2" 

:

private void button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     string connection = @"Data Source=Local-PC\HOME;Initial Catalog=Project;Integrated Security=True"; 
     SqlConnection cn = new SqlConnection(connection); 
     cn.Open(); 
     string userText = textBox1.Text; 
     string passText = textBox2.Text; 

     SqlCommand cmd = new SqlCommand("SELECT ISNULL(User_ID, '') AS User_ID, ISNULL(User_Pass,'') AS User_Pass, User_Level FROM User_Login WHERE User_ID = @User_ID and User_Pass = @User_Pass, cn); 
     cmd.Parameters.Add(new SqlParameter("User_ID", userText)); 
     cmd.Parameters.Add(new SqlParameter("User_pass", passText)); 

     SqlDataReader dr = cmd.ExecuteReader(); 

     try 
     { 
      dr.Read(); 

      if (dr["User_Level"].ToString().Trim() == "1") 
      { 
       textBox3.Text = dr["User_ID"].ToString(); 
       this.Hide(); 
       Form2 form2 = new Form2(); 
       form2.Show(); 
       //this.Close(); 
      } 

      if (dr["User_Level"].ToString().Trim() == "2") 
      { 
       textBox3.Text = dr["User_ID"].ToString(); 
       this.Hide(); 
       Form3 form3 = new Form3(); 
       form3.Show(); 
       //this.Close(); 
      } 
     } 
     catch 
     { 
      MessageBox.Show("Invalid Username or Password"); 
     } 
     dr.Close(); 
     cn.Close(); 

    } 
    catch 
    { 

    } 
} 

편집 :

나는 다음과 같은 고정 된 코드를 사용하는 것이 좋습니다

만 따를 온 필터는 경우이어야한다 오류 The name 'form2' does not exist in the current context에 대한 귀하의 의견에 따르면. 다음을 사용하여 마지막 if를 수정해야합니다.

Form3 form3 = new Form3(); 
form3.Show(); // instead of form2.Show(); 
+1

마지막 오류 : 'form2'이름이 존재하지 않습니다. 해결하기 어렵지 않습니다. 항목의 첫 번째 항목은 첫 번째 if 문에서 Form2의 새 인스턴스를 만들었지 만 두 번째 if 문은 만들지 않았습니다. – Max

+0

감사합니다 !! 그것은 완벽하게 작동했습니다! 나는 또한 폼 2의 객체와 같은 이름으로 양식 3의 객체 이름을 지정하고 form3을 시작하는 올바른 코드 인 경우 오류를 추가하고 싶습니다. if (dr [ "User_Level"]. ToString(). Trim() == "2") { textBox3.Text = dr [ "User_ID"]. ToString(); this.Hide(); Form3 form3 = 새 Form3(); form2.Show(); – user2370201

+0

Moslem and Mobstaa, 정말 도움이 되었어요! – user2370201

관련 문제