Connecting to MySQL DB

Hey all, I'm relatively new to connecting to DB. I was able to successfully accomplish this task but I have a few questions.

Below is my code for connecting to the database and retrieving information/sending information.

This was built more for a proof of concept, not efficiency or much exception handling and isn't very dynamic. I was just wondering if this code would be considered 'accepted' by programming conventions, and if not, are there any problems/flaws with connecting to a database with the below parameters? Is there anything you see wrong/should do differently?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
String^ constring = L"datasource=localhost;port=3306;username=root;password=root";
MySqlConnection^ conDatabase = gcnew MySqlConnection(constring);

MySqlCommand^ cmdDatabase = gcnew MySqlCommand("select * from database.edata
where username='" + this->usernameTextBox->Text + "' 
and password='" + this->passwordTextBox->Text + "';", conDatabase);

MySqlDataReader^ myReader;

	try {
		conDatabase->Open();
		myReader = cmdDatabase->ExecuteReader();
		int count = 0;
		while (myReader->Read()) {
			count++;
		}

		if (count == 0)
			MessageBox::Show("Invalid Username or Password");
		else if (count == 1) {
			PlaySound(L"C:\\Users\\Matt\\Music\\Sounds\\whoosh.wav", NULL, SND_FILENAME);
			this->Hide();
			MyForm2^ f2 = gcnew MyForm2();
			f2->ShowDialog();
		}
		else if (count > 1)
			MessageBox::Show("Duplicate Username and Passwords");
	
	}
	catch (Exception^ex) {
		MessageBox::Show(ex->Message);
	}
Last edited on
If you want to get the count of matching rows use "select count(*) from ..." and cmdDatabase->ExecuteScalar().
Topic archived. No new replies allowed.