- 我有一個窗口。我有一堂課。
- 該窗口是一個密碼登錄窗口,因此它有一個用戶名,密碼箱和登錄按鈕的文本框。這個窗口後面
代碼:
using MySql.Data.MySqlClient;
namespace Masca
{
/// <summary>
/// Interaction logic for Login.xaml
/// </summary>
public partial class Login : Elysium.Controls.Window
{
InvalidLogin secondForm;
// This window now knows that the loginc class exists. The class hold all MySQL related code
public loginc loginc;
// THis window now knows about the MainWindow. This is the window that will be opened after authetication checks are successful.
MainWindow window;
// The login window is now publically available to other windows.
public Login()
{
InitializeComponent();
}
//This is the event that is carried out when the user clicks 'Login'
public void Logon_Click(object sender, RoutedEventArgs e)
{
// instantiate the class
loginc = new loginc();
//Check to see if the username is blank
if (username.Text == "")
{
//If it is, show the custom dialogue box "nousername"
new nousername().ShowDialog();
}
//Then check to see if the password is blank
else if (password.Password == "")
{
//If it is, show the custom dialogue box "nopassword"
new nopassword().ShowDialog();
}
// However if they both have content..
else if (username.Text != "" && password.Password != "")
{
// Trigger this method in the loginc class.
loginc.Login();
}
}
// This method is triggered by the login method in the loginc class if the credentials are valid
public void login()
{
window = new MainWindow(username.Text);
window.Show();
this.Close();
}
// This method is also triggered by the login method in the loginc class but only if the credentials are not valid
public void failLogin()
{
//If the entered username and password does not matchup with any record in the database, show the error messagebox 'InvalidLogin';
secondForm = new InvalidLogin();
//The form that opened the dialoge box is 'Login', the login window;
secondForm.setCreatingForm = this;
// Keep that in mind when you show the dialogue box;
secondForm.ShowDialog();
}
-The類認爲,將用於檢查數據庫中的用戶名和密碼的MySQL的代碼。
using MySql.Data.MySqlClient;
namespace Masca
{
public class loginc
{
//This class now knows about the login window.
public Login login;
// Constructor
public loginc()
{
}
// This method is triggered by the 'Logon_Click' event in the Login window
public void Login()
{
//instantiate the Login window
login = new Login();
//Database connection parameters
string sqlcon = "datasource=localhost;port=3306;username = root; password = root";
//Query to excecute
string query = "SELECT * FROM logon.login where username = '"+login.username.Text+"' and password = '"+login.password.Password+"';";
//Declarations
MySqlConnection con = new MySqlConnection(sqlcon);
MySqlCommand cmd = new MySqlCommand (query,con);
MySqlDataReader rdr;
// Excecution
con.Open();
rdr = cmd.ExecuteReader();
int count = 0;
while (rdr.Read())
{
count = count + 1;
}
// If the username and password matches a record in the database table..
if (count == 1)
{
//Trigger the login method in the login window
login.login();
}
// Otherwise...
else
{
//Trigger the failLogin method in the login window
login.failLogin();
}
con.Close();
}
那麼這個問題呢?每次輸入正確或錯誤的憑證登錄時,我都會收到無效憑證對話框。
我猜(我可能是錯的)這很可能是因爲我實例化的類。我所引用的(在查詢中)+ login.username.Text +和+ login.password.Password +的詳細信息是從登錄窗口的另一個實例收集的,而不是我輸入的那個實例。因此,它收集的詳細信息是空白。
這也可以解釋爲什麼當我使用登錄表中的實際憑證替換+ login.username.Text +和+ login.password.Password +時,登錄窗口保持打開狀態,即使在MainWindow打開後,當我編程時它關閉。因爲它完全關閉了一個不同的實例。
即使有我所有的猜測,我仍然不知道這個問題的可行解決方案。我能想到的唯一一個是使用單例模式的靜態類來確保程序在運行時只有一個類和窗口的實例。我不太確定該從哪一個開始。
任何想法?
你熟悉傳遞參數的概念並從函數返回值? –
我聞到SQL注入。 – Yahya