2017-07-18 76 views
0

我在下面的代碼中有用戶登錄表單(用戶名和密碼)我希望它檢查用戶和密碼是否存在於數據庫中但是當輸入用戶和傳遞它不給我訪問form2總是我得到一個錯誤的登錄消息我認爲沒有數據庫的連接或它不讀取if條件?從DB中選擇用戶和密碼的用戶登錄表問題

我的表名是帳戶幷包含兩列(用戶,通過) 數據庫名Database1。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace testdb 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

    string cs = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;"; 


    private void button1_Click(object sender, EventArgs e) 
    { 

     try 
     { 
      SqlConnection cn = new SqlConnection(cs); 
      SqlCommand cmd = new SqlCommand("Select * from account where [email protected] and [email protected]", cn); 
      cmd.Parameters.AddWithValue("@username", textBox2.Text); 
      cmd.Parameters.AddWithValue("@password", textBox1.Text); 

      cn.Open(); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 
      cn.Close(); 
      int count = ds.Tables[0].Rows.Count; 
      if (count == 1) 
      { 

       Form2 form2 = new Form2(); 
       this.Hide(); 
       form2.Show(); 
      } 
      else 
      { 
       MessageBox.Show("Invalid Login please check username and password"); 
      } 
      cn.Close(); 
     } 

     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

     } 

} 

}

此行

SqlConnection cn = new SqlConnection(global::testdb.Properties.Settings.Default.Database1ConnectionString); 

我改成了

string cs = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;"; 

注:DB它是在Visual Studio現有意味着我使用它自帶的DB與視覺工作室和我已經定義了用戶名的表帳戶,並通過


這是我的問題,我得到錯誤信息,即使正確的用戶名和密碼 enter image description here

這裏我的數據的基礎上爲VS enter image description here

其他快照可以在我的回覆中發現

+0

您是否嘗試過調試?找到調試器停止的地方,並記下它給你的信息/警告/錯誤。 – Sofver

+0

您總是收到「無效登錄請檢查用戶名和密碼」錯誤信息?如果是,那麼你的數據庫連接工作正常,但如果你收到一些異常消息,那麼在數據庫調用中會出現問題。 –

+0

不知道你的數據是什麼樣的或者你得到了什麼錯誤,它不可能幫助你。請更新您的問題以包含更多詳細信息。 – Ortund

回答

0

在登錄時,系統基本上要求用戶告訴它2件事:

  • 這是我是誰(用戶名,電子郵件地址等),並
  • 這是我所知道的(密碼)

所以,當你正在評估來自用戶的這個輸入,你應該做的它以同樣的方式;也就是說通過先檢查用戶的身份(我是誰),然後比較密碼(我知道的)。

請看下面的例子:

protected void btnLogin_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     SqlConnection cn = new SqlConnection(cs); 
     SqlCommand cmd = new SqlCommand("Select * from account where [email protected]", cn); 
     cmd.Parameters.AddWithValue("@username", textBox2.Text); 

     cn.Open(); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 
     cn.Close(); 

     if (ds.Tables[0].Rows.Count == 1) 
     { 
      // A user with this username exists, check the password. 
      if (ds.Tables[0].Rows[0][1] == textBox1.Text) 
      { 
       // The login succeeded, show Form2. 
       Form2.Show(); 
      } 
      else 
      { 
       MessageBox.Show("Invalid Login please check username and password"); 
      } 
     } 
     else 
     { 
      // No user with the given username exists. 
      MessageBox.Show("Invalid Login please check username and password"); 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

如果有與指定的用戶名沒有用戶,你會得到「無效登錄」的錯誤。

如果用戶登錄的密碼與數據庫中的密碼不匹配,則會出現「無效登錄」錯誤。

如果系統無法連接到數據庫,則會從您的catch塊中收到錯誤消息。

如果一切順利,當用戶登錄時應顯示Form2。

+0

謝謝你,我試過你的代碼沒有錯誤我也改變了這一行if(ds.Tables [0] .Rows [0] [1] == textBox1.Text)to ds.Tables [0] .Rows [0] [1] .ToString()== textBox1.Text)將其轉換爲字符串,因爲我們有對象和字符串,但沒有任何更改:(請參閱上面的快照我更新了我的問題 – Nerman

相關問題