Sunday, February 17, 2013

Visual C++ Windows Form Application Mysql Connection


1.Create a new C++ Windows Form Application.

2.Go to Project > Project Name Properties > Add New Reference > Select the Tab .NET

3. Scroll down till you see MySQL.Data then Double Click it or Click it and Press OK then Press OK again (If you don't see it then you cant have the MySQL Connector Installed).


4. Double Click on the Form then Scroll the code till you see using namespace System::Drawing;

5. Below that type using namespace MySql::Data::MySqlClient;

6.Go back to the Form Design and Add a Button to do the connection.So when we click the button , the button will establish the connection to MySQL.

Button means your item!

7.Double click on the Button to go to code section and add the following Code:

Fill in the Port, Username, Password and Hostname with your MySQL Information.

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 password='" + this->password->Text +"' ;", conDataBase ); 
MySqlDataReader ^myReader; 
try{
conDataBase->Open(); 
myReader = cmdDataBase->ExecuteReader(); 
int count=0;
while (myReader->Read()) {
      // username->Text  +=(myReader->GetInt32(0) + ", " + myReader->GetString(1)+", "+myReader->GetString(2) + ", " + myReader->GetInt32(3)+ "\r\n");
    
}

}
catch (Exception^ ex)
{
MessageBox::Show(ex->Message);
}


8.Now run the program and see the response in the message box .




Visual C++ Tutorial 2 -Windows Forms Application: 

Mysql Connection Part 1 




Visual C++ Tutorial 3 -Windows Forms Application: Mysql Connection Part 2 




C++ Windows Form Application Mysql Connection
Using Visual C++ Windows Forms with MySQL
C++ Windows Form Application Mysql Connection
Mysql And Visual C++ 2010 Windows Form Application
MySQL :: Connecting to MySQL with a WindowsForm application
C++ Form textbox string into SQL Database
visual c++ CLR windows form application and mysql

3 comments:

  1. Can someone add please a preparedstatement syntax for the example above?

    in C++ connector it would be written as
    pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)");
    for (int i = 1; i <= 10; i++) {
    pstmt->setInt(1, i);
    pstmt->executeUpdate();
    }
    How to write it in .Net connector using VC++?

    ReplyDelete
  2. here is the answer:
    http://dev.mysql.com/doc/connector-net/en/connector-net-programming-prepared-preparing.html

    ReplyDelete