-1

I am trying to make a Login form in Visual Studio using C++ and Winforms. However, I am having trouble with connecting to the database. I've tried changing the connection string to the one specified in Visual Studio and to connection strings I've found online that are related to MySQL.

If anyone can at least provide a resource that describes how to connect to a MySQL database with winforms in c++, that would be extremely helpful (and maybe how to compare user inputted data from the form with data from the database)!

The code for the button click in the form:

public: User^ user = nullptr;

private: System::Void btnOK_Click(System::Object^ sender, System::EventArgs^ e) {
    String^ username = this->tbUsername->Text;
    String^ password = this->tbPassword->Text;
    if (username->Length == 0 || password->Length == 0) {
        MessageBox::Show("Username or Password is empty!", "ERROR", MessageBoxButtons::OK, MessageBoxIcon::Error);
        return;
    }
    try {

        String^ connString = "server=localhost;user id=root;password=password;database=iadb";
        SqlConnection sqlConn(connString);
        sqlConn.Open();

        String^ sqlQuery = "SELECT * FROM users WHERE username=@username AND password=@password";
        SqlCommand command(sqlQuery, % sqlConn);
        command.Parameters->AddWithValue("@username", username);
        command.Parameters->AddWithValue("@password", password);
        

        SqlDataReader^ reader = command.ExecuteReader();
        if (reader->Read()) {
            user = gcnew User;
            user->id = reader->GetInt32(0);
            user->username = reader->GetString(1);
            user->password = reader->GetString(2);
            user->email = reader->GetString(3);
            this->Close();
        }
        else {
            MessageBox::Show("Email or Password incoorect!", "ERROR", MessageBoxButtons::OK);
        }
    }
    catch (sql::SQLException e) {
        MessageBox::Show("Could not connect to Database!", "ERROR", MessageBoxButtons::OK, MessageBoxIcon::Error);
    }
Victor
  • 9
  • 2

1 Answers1

0

So, for anyone in the future, I've managed to get it working! Firstly, I had to learn how to make the same exact program in C#.

Here's the answer:

First you include a MySql.Data Reference: Here is how to do it!

Next, you need to use the namespace using namespace MySql::Data::MySqlClient;

Finally, you need to replace all the sql classes with MySql, here's an example: SqlConnection sqlConn(connString) -> MySqlConnection sqlConn(connString)

Here's the code:

public: User^ user = nullptr;

private: System::Void btnOK_Click(System::Object^ sender, System::EventArgs^ e) {
    String^ username = this->tbUsername->Text;
    String^ password = this->tbPassword->Text;
    if (username->Length == 0 || password->Length == 0) {
        MessageBox::Show("Username or Password is empty!", "ERROR", MessageBoxButtons::OK, MessageBoxIcon::Error);
        return;
    }
    try {
        String^ connString = L"SERVER=localhost;DATABASE=iadb;UID=root;PASSWORD=password;";
        MySqlConnection sqlConn(connString);
        sqlConn.Open();
        
        String^ query = "SELECT * FROM users WHERE username=@username AND password=@password";
        MySqlCommand command(query, % sqlConn);
        command.Parameters->AddWithValue("@username", username);
        command.Parameters->AddWithValue("@password", password);

        MySqlDataReader^ reader = command.ExecuteReader();
        if (reader->Read()) {
            user = gcnew User;
            user->id = reader->GetInt32(0);
            user->username = reader->GetString(1);
            user->password = reader->GetString(2);
            user->email = reader->GetString(3);

            this->Close();
        }
        else {
            MessageBox::Show("Username or password incorrect!", "ERROR", MessageBoxButtons::OK, MessageBoxIcon::Error);
        }
    }
    catch (MySqlException^ e) {
        MessageBox::Show("Could not connect to Database!", "ERROR", MessageBoxButtons::OK, MessageBoxIcon::Error);
    }
}
Victor
  • 9
  • 2