2013-03-08 76 views
0

我目前正試圖在C#中設計一個atm機器,我對此很新。 我想我的登錄屏幕返回到我的歡迎屏幕3嘗試登錄失敗後,但我不知道從哪裏開始,以及如何實現我的代碼到我的程序來做到這一點。如何在登錄屏幕上創建登錄限制?

我當前的代碼是我的登錄屏幕如下:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.OleDb; 
using System.Data.Common; 

namespace bankkk 
{ 
    public partial class FrmLogin : Form 
    { 
     public FrmLogin() 
     { 
      InitializeComponent(); 
     } 

     public static OleDbConnection con = new OleDbConnection(); 

     string dbProvider; 
     string dbSource; 

     OleDbDataAdapter da; 

     public static DataSet ds1 = new DataSet(); 

     string sql; 
     string pin; 
     int rownum = 0; 
     bool valid = false; 

     private void FrmLogin_Load(object sender, EventArgs e) 
     { 
      dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"; 
      dbSource = "Data Source = 'd:\\bank11.accdb'"; 
      con.ConnectionString = dbProvider + dbSource; 
      ds1 = new DataSet(); 
      con.Open(); 
      sql = " SELECT tblCustomers.* FROM tblCustomers"; 
      da = new OleDbDataAdapter(sql, con); 
      rownum = da.Fill(ds1, "tblCustomers"); 

      con.Close(); 
     } 

     private void btnexit_Click(object sender, EventArgs e) 
     { 

      System.Environment.Exit(0); 
      this.Close(); 

     } 


     //METHOD VALIDATE 

     private bool validate() 
     { 
      ds1 = new DataSet(); 
      con.Open(); 

      sql = "SELECT tblCustomers.* FROM tblCustomers WHERE ((tblCustomers.AccountNo) = '" + txtAccount.Text + "')"; 
      da = new OleDbDataAdapter(sql, con); 
      rownum = da.Fill(ds1, "tblCustomers"); 
      con.Close(); 

      if (rownum != 1) 
      { 
       MessageBox.Show("Not a valid Account"); 
       return false; 
      } 
      else 
      { 
       pin = ds1.Tables["tblCustomers"].Rows[0][4].ToString(); 
       if (pin == txtPin.Text) 
       { 
        return true; 
       } 
       else 
       { 
        MessageBox.Show("INVALID PIN"); 
        return false; 
       } 
      } 
     } 

     private void btnLogin_Click(object sender, EventArgs e) 
     { 
      valid = validate(); 
      if (valid == true) 
      { 
       if (txtAccount.Text == "11111111" && txtPin.Text == "9999") 
       { 
        Frmmanager Manager = new Frmmanager(); 
        this.Close(); 
        Manager.Show(); 
       } 
       else 
       { 
        frmaccount account = new frmaccount(); 
        this.Close(); 
        account.Show(); 

        { 
         txtAccount.Clear(); 
         txtPin.Clear(); 
        } 
       } 
      } 
     } 

     private void btnlogin_Click_1(object sender, EventArgs e) 
     { 
      valid = validate(); 
      if (valid == true) 
      { 
       if (txtAccount.Text == "11111111" && txtPin.Text == "9999") 
       { 
        Frmmanager Manager = new Frmmanager(); 
        this.Close(); 
        Manager.Show(); 
       } 
       else 
       { 
        frmaccount account = new frmaccount(); 
        this.Close(); 
        account.Show(); 

        { 
         txtAccount.Clear(); 
         txtPin.Clear(); 
        } 
       } 
      } 
     } 
    } 
} 
+0

O.o在執行SQL語句之後,您不知道從哪裏開始?您是否考慮過從頭開始製作簡單的登錄應用程序,而不使用數據庫? – 2013-03-08 20:02:37

+0

您只需將失敗的登錄數存儲在一個變量中,然後檢查該變量。 – 2013-03-08 20:02:46

+0

使用變量來存儲失敗的登錄嘗試的次數,在每次登錄失敗時增加這個值,檢查按鈕上的變量值是否按順序點擊 – 2013-03-08 20:03:04

回答

1

你說返回到我的歡迎屏幕,所以我會假設你正在使用,而不是.ShowDialog()FrmLogin顯示.Show()。這樣,你只需要關閉你的舊形式。

如果你只是在做.Show(),你可以做這樣的事情:

public partial class FrmWelcome { 

    //in some part of your code... 
    var frmLogin = new FrmLogin(); 

    //when the login form is closed, show this one. 
    //Depending on your application, you might want to add some boolean variable 
    //to the Login Form that will be true if the user authentication failed, and 
    //show this form again only if it is true. 
    frmLogin.Closed += (s, e) => this.Show(); 

    this.Hide(); 
    frmLogin.Show(); 
} 

嘗試下面的代碼。這個想法是有一個failedAttempts變量,每當你的驗證代碼失敗時它會增加。當它等於3時,您只需關閉表格(參見上文)。

namespace bankkk 
{ 
    public partial class FrmLogin : Form 
    { 
     public FrmLogin() 
     { 
      InitializeComponent(); 
     } 

     ... 

     int failedAttempts = 0; 

     private void btnlogin_Click_1(object sender, EventArgs e) 
     { 
      valid = validate(); 
      if (!valid) { 
       //Increment the number of failed attempts 
       failedAttempts += 1; 

       //If equal to 3 
       if (failedAttempts == 3) { 
        //Just close this window 
        this.Close(); 
       } 
      } 
      else 
      { 
       //the same code you were using... 
      } 
     } 
    } 
} 
+0

如果你爲他做這件事,他不會學習。 :P – 2013-03-08 20:11:08

+0

@DavidHughes我編輯了我的答案,除了代碼外還添加了一些解釋:) – 2013-03-08 20:16:31

+0

謝謝Thats Great :) – 2013-03-09 17:40:23

0

您可以添加一個變量到您的程序並增加它以限制嘗試登錄的次數。要限制帳戶嘗試的次數,請添加一個表以將包括無效嘗試在內的登錄信息存儲到數據庫。將該表格鏈接到您的客戶表格。當無效嘗試登錄到有效的客戶帳戶時會增加無效嘗試的次數並將其寫回登錄表。在成功登錄後,您可以將無效嘗試的次數設置爲0.