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