gpt4 book ai didi

c# - 从类中运行 MySQL 查询。值(value)观没有正确传递到类(class)

转载 作者:行者123 更新时间:2023-11-29 13:00:28 27 4
gpt4 key购买 nike

就是这里。

-我有一个窗口。我还有课。

-该窗口是一个密码登录窗口,因此它有一个用户名文本框、一个密码框和一个登录按钮。

此窗口背后的代码:

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();
}

-该类保存 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+ 时,即使在主窗口打开后,当我对其进行编程时,登录窗口仍保持打开状态关闭。因为它完全关闭了一个不同的实例。

即使有我的所有猜测,我仍然不知道解决我的这个问题的可行方法。我唯一能想到的就是使用单例模式使用静态类来确保程序在运行时只拥有类和窗口的一个实例。但我不太确定从哪里开始。

有什么想法吗?

最佳答案

只需让您的 loginc.Login() 方法将 Login 窗口作为参数,并使用它而不是创建一个新窗口;

即改变;

public void Login ()
{
//instantiate the Login window
login = new Login();

进入

public void Login (Login login)
{
// you now have the existing login window as "login".

并将其称为;

loginc.Login(this);  // pass this object (the Login window) as a parameter

另一种(可能更好)的方法是将登录名和密码作为字符串传递给 loginc.Login(),这样 loginc 类就不需要知道关于 Login 类中的字段。

您还应该真正考虑参数化您的查询,其中一个登录框中的 ' 会破坏您的查询,并允许其他人直接针对您的查询编写自己的 SQL数据库。

( Here you can read up a little on SQL injection )

关于c# - 从类中运行 MySQL 查询。值(value)观没有正确传递到类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23425490/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com