2015-09-19 16 views
1

當我輸入用戶名和密碼時,它會成功登錄並打開所需的字段,但是當我輸入錯誤的名稱或密碼(未保存在數據庫中)時,它實際上不會顯示任何內容「其他」這是代碼否則在使用c#的讀卡器中不起作用

private void signin_button_Click(object sender, EventArgs e) 
{ 
    string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= D:/Student_Managment_System/SMS1.mdb"; 

    OleDbConnection myConnection = new OleDbConnection(connectionString); 
    myConnection.Open(); 

    OleDbCommand cmd = new OleDbCommand(); 
    cmd.Connection = myConnection; 

    string usrname = name_textBox.Text; 
    string passwd = pass_textBox.Text; 

    OleDbCommand cmd1 = new OleDbCommand("select * from Manager where Name='" + usrname + "' and Passwd='" + passwd + "'"); 

    OleDbDataReader Reader = cmd1.ExecuteReader(); 

    while (Reader.Read()) 
    { 
      if (Reader[5].ToString() == "manager") 
      { 
       this.Hide(); 
       Student_Info stuInf = new Student_Info(); 
       stuInf.Show(); 
       break; 
      } 
      else if (Reader[5].ToString() == "employee") 
      { 
       MessageBox.Show("log in as a employee "); 
      } 
      else 
      { 
       MessageBox.Show("Inviled User name or password"); 
      } 
    } 

    myConnection.Close(); 
} 

回答

3

不執行你的else語句,因爲沒有值輸入無效的用戶名或密碼的時候閱讀。你需要檢查你的閱讀器是否有任何行。見here

代碼

//Are there any rows 
if(Reader.HasRows) 
{ 
    //If so read them 
    while (Reader.Read()) 
    { 
     if (Reader[5].ToString() == "manager") 
     { 
      this.Hide(); 
      Student_Info stuInf = new Student_Info(); 
      stuInf.Show(); 
      break; 
     } 
     else if (Reader[5].ToString() == "employee") 
     { 
      MessageBox.Show("log in as a employee "); 
     } 
    } 
} 
else 
{ 
    MessageBox.Show("Inviled User name or password"); 
} 
+0

感謝X-TECH ....這作品 –