2017-05-27 69 views
-1

我有這個登錄代碼,如果數據庫中只有一個用戶,但如果我添加另一個用戶,然後用該帳戶登錄,彈出消息「錯誤的用戶名或密碼」我點擊確定它將我重定向到Form1。登錄表單不起作用

private void buttonLogin_Click(object sender, EventArgs e) 
    { 

     SQLiteConnection conn = new SQLiteConnection("data source = zivali.sqlite"); 
     conn.Open(); 
     SQLiteCommand com = new SQLiteCommand(conn); 

     com.CommandText = "SELECT * FROM login;"; 

     SQLiteDataReader reader = com.ExecuteReader(); 

     while (reader.Read()) 
     { 
      string username = reader["username"].ToString(); 
      string password = reader["password"].ToString(); 


       if (username == textBoxUserName.Text && password == textBoxPassword.Text) 
       { 
        this.Hide(); 
        Form1 f1 = new Form1(); 
        f1.ShowDialog(); 
       } 
       else 
       { 
        MessageBox.Show("Wrong username or password"); 
       } 

     } 
     conn.Close(); 
    } 
+0

您的登錄成功/失敗代碼針對數據庫中的每個登錄都會運行。這就是爲什麼你得到N-1錯誤的用戶名messageboxes和1成功登錄。 – user6144226

回答

1

如果您有一個用戶,while循環將循環一次。因此,如果用戶不存在,它將檢查用戶是否存在,它將顯示一個消息框。

如果您有多個用戶。 while循環將運行X次。但是,當它循環第一次,憑證與給定不匹配時,它將顯示消息框。

你說你已經添加了第二個用戶。所以當你用第二個用戶登錄時,第一個用戶在循環時將是錯誤的。這就是它顯示消息框的原因。

爲了解決這個問題,你可以嘗試這樣的事:

 SQLiteConnection conn = new SQLiteConnection("data source = zivali.sqlite"); 
     conn.Open(); 
     SQLiteCommand com = new SQLiteCommand(conn); 

     com.CommandText = "SELECT * FROM login;"; 

     SQLiteDataReader reader = com.ExecuteReader(); 

     // place your username/password decleration here, no need to read them X times in a loop 
     string username = reader["username"].ToString(); 
     string password = reader["password"].ToString(); 

     // bool for if there is any user whith the given cridentials 
     bool loginValid = false; 
     while (reader.Read()) 
     { 
      // if cridentials mathch set loginValid to true and break out of the loop 
      if (username == textBoxUserName.Text && password == textBoxPassword.Text) 
      { 
       loginValid = true; 
       break; 

      } 
     } 

     // check if the login is true 
     if (loginValid) 
     { 
      this.Hide(); 
      Form1 f1 = new Form1(); 
      f1.ShowDialog(); 
     } 
     else 
     { 
      MessageBox.Show("Wrong username or password"); 
     } 
     conn.Close(); 
+0

嗨。我試過你的代碼,但現在我得到這個錯誤消息InvalidOperationException:沒有當前行錯誤這裏是一個錯誤圖片的鏈接:http://imgur.com/vUMLoS2 – krneki

+0

我想我犯了一個錯誤嘗試移動這些行:'字符串username = reader [「username」]。ToString(); string password = reader [「password」]。ToString();'進入while循環,就像以前一樣。 –

+0

現在它的工作謝謝。 – krneki

0

你必須不能讓你while語句中你的決定,你可能應該做的事情如下所示:

bool doesMatch = false; 
string username = reader["username"].ToString(); 
string password = reader["password"].ToString(); 


while (reader.Read()) 
{ 
     if (username == textBoxUserName.Text && password == textBoxPassword.Text) 
     { 
      doesMatch = true; 
     } 
} 
if (doesMatch) 
{ 
    this.Hide(); 
    Form1 f1 = new Form1(); 
    f1.ShowDialog(); 
} 
else 
{ 
    MessageBox.Show("Wrong username or password"); 
} 

或者您可以將WHERE語句添加到您的查詢中,以檢查表中是否有一行中的用戶具有與輸入的用戶名和密碼相同的數據。