Sunday, December 18, 2011

12-Tutorial 12: SQL Calls to a MySQL Database in Visual C++.NET 2010, 2008

Tutorial 12: SQL Calls to a MySQL Database in Visual C++.NET 2010, 2008 (Updated 12/11/11)
1.  Download the MySQL database server/client: at http://www.mysql.com and select mysql community server>mysqlcommunity server>windows>windowsx86 zip/setup>pick a mirror>new user>No thanks just take me to the downloads>mysql-5.0.41-win32.zip.
Then install the executable by selecting custom. In the second window (as shown below) select that developer components should be installed to your hard drive, but go with the defaults on everything else. To download the VC.NET 2010 verion of the MySQL Database Project click here or for the 2008 version click here.

 MySQL Setup Wizard 

Then select skip sign-up.  Go with Detailed configuration.>Developer Machine>Multifunctional Database>C:  Installation Path>Decision Support (DSS)/OLAP>Enable TCP/IP Networking>Standard Character Set>Install as Windows Service and Include Bin Directory in Windows Path >Modify Security Settings (just done within MySQL…not as settings on your computer)  and type a root password (don’t forget this…or you will have to reinstall it. Here I set the password to "student") >Enable root access from remote machines>Execute

2.  Download the MySQL ODBC driver (open database connectivity)at database products>MySQL drivers>ODBC Driver for MySQL >Download>windows>windows x86 zip/setup.   Install as follows by first clicking Next as shown below:
Next Select Typical as shown below and click next:
Then click install as shown below:
Here I am using MySQL Connector/ODBC 3.51.

3.  Create Database and Tables by going to Start>All Programs>MySQL>MySQL Server 5.0 >MySQL command line client>(you are now at a command line)  type the password (I used "student") entered and hit enter> 
Then enter the following given in yellow:

Enter password: **********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 5.0.17-nt 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 
mysql> create database mysampledatabase;
Query OK, 1 row affected (0.00 sec) 
mysql> use mysampledatabase;
Database changed
mysql> create table nameExample(first VARCHAR(255),last VARCHAR(255));
Query OK, 0 rows affected (0.06 sec) 
mysql> insert into nameExample(first,last) values ('Jim','Krumm');
Query OK, 1 row affected (0.03 sec) 
mysql> insert into nameExample(first,last) values ('Bob','Villa');
Query OK, 1 row affected (0.03 sec) 
mysql> insert into nameExample(first,last) values ('George',' Washington ');
Query OK, 1 row affected (0.03 sec) 
mysql> select * from nameExample;
+--------+------------+
| first  | last       |
+--------+------------+
| Jim    | Krumm      |
| Bob    | Villa      |
| George | Washington |
+--------+------------+
3 rows in set (0.00 sec) 
mysql>exit

It is important to note that a single database file has not been created (unlike Access), rather it is put in a collection of database files usually in a common set of  files. Also in MySQL it is appropriate to name columns first and last if you wish.

4.  Making a Program in Visual C++.NET 2010:

A.  Go to New Project>Visual C++>Windows Form Application: Name the programi MyMySQLVCProgram.

B.  Make the following form:
The Form's setup

  C.  Enter the following code to the form : This program does not use any of the built in database tools in the toolbox for Microsoft Visual C++.NET. It makes the link to the database from scratch. When running the program, hit the open connection button before executing any of the other buttons, and close the connection when finished. Particularly make sure the code highlighted in yellow is included. 
#pragma once
#include <iostream>
using namespace std;

namespace MyMySQLVCProgram {

        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;
        using namespace Odbc;

        /// <summary>
        /// Summary for Form1
        /// </summary>
        public ref class Form1 : public System::Windows::Forms::Form
        {
        public:
                OdbcConnection ^myConnection;
                Form1(void)
                {
                        InitializeComponent();
                        //
                        //TODO: Add the constructor code here
                        //
                }

        protected:
                /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                ~Form1()
                {
                        if (components)
                        {
                                delete components;
                        }
                }
        private: System::Windows::Forms::TextBox^  textBox6;
        protected:
        internal: System::Windows::Forms::Button^  Button1;
        private:
        private: System::Windows::Forms::Label^  label6;
        internal:
        private: System::Windows::Forms::GroupBox^  groupBox3;
        private: System::Windows::Forms::TextBox^  textBox5;
        private: System::Windows::Forms::Label^  label5;
        private: System::Windows::Forms::TextBox^  textBox4;
        internal: System::Windows::Forms::Button^  Button6;
        private:
        private: System::Windows::Forms::TextBox^  textBox3;
        internal:
        private: System::Windows::Forms::Label^  label4;
        private: System::Windows::Forms::Label^  label3;
        internal: System::Windows::Forms::Button^  Button8;
        private:
        private: System::Windows::Forms::GroupBox^  groupBox2;
        internal:
        private: System::Windows::Forms::TextBox^  textBox2;
        private: System::Windows::Forms::TextBox^  textBox1;
        private: System::Windows::Forms::Label^  label2;
        internal: System::Windows::Forms::Button^  Button7;
        private:
        private: System::Windows::Forms::Label^  label1;
        internal:
        internal: System::Windows::Forms::Button^  Button4;
        private:
        internal: System::Windows::Forms::Button^  Button5;
        internal: System::Windows::Forms::Button^  Button9;
        internal: System::Windows::Forms::GroupBox^  GroupBox1;
        internal: System::Windows::Forms::Button^  Button3;
        internal: System::Windows::Forms::Button^  Button2;
        internal: System::Windows::Forms::ListBox^  lstRecords;

        private:
                /// <summary>
                /// Required designer variable.
                /// </summary>
                System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                void InitializeComponent(void)
                {
                        this->textBox6 = (gcnew System::Windows::Forms::TextBox());
                        this->Button1 = (gcnew System::Windows::Forms::Button());
                        this->label6 = (gcnew System::Windows::Forms::Label());
                        this->groupBox3 = (gcnew System::Windows::Forms::GroupBox());
                        this->textBox5 = (gcnew System::Windows::Forms::TextBox());
                        this->label5 = (gcnew System::Windows::Forms::Label());
                        this->textBox4 = (gcnew System::Windows::Forms::TextBox());
                        this->Button6 = (gcnew System::Windows::Forms::Button());
                        this->textBox3 = (gcnew System::Windows::Forms::TextBox());
                        this->label4 = (gcnew System::Windows::Forms::Label());
                        this->label3 = (gcnew System::Windows::Forms::Label());
                        this->Button8 = (gcnew System::Windows::Forms::Button());
                        this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
                        this->textBox2 = (gcnew System::Windows::Forms::TextBox());
                        this->textBox1 = (gcnew System::Windows::Forms::TextBox());
                        this->label2 = (gcnew System::Windows::Forms::Label());
                        this->Button7 = (gcnew System::Windows::Forms::Button());
                        this->label1 = (gcnew System::Windows::Forms::Label());
                        this->Button4 = (gcnew System::Windows::Forms::Button());
                        this->Button5 = (gcnew System::Windows::Forms::Button());
                        this->Button9 = (gcnew System::Windows::Forms::Button());
                        this->GroupBox1 = (gcnew System::Windows::Forms::GroupBox());
                        this->Button3 = (gcnew System::Windows::Forms::Button());
                        this->Button2 = (gcnew System::Windows::Forms::Button());
                        this->lstRecords = (gcnew System::Windows::Forms::ListBox());
                        this->groupBox3->SuspendLayout();
                        this->groupBox2->SuspendLayout();
                        this->GroupBox1->SuspendLayout();
                        this->SuspendLayout();
                        //
                        // textBox6
                        //
                        this->textBox6->Location = System::Drawing::Point(27, 172);
                        this->textBox6->Name = L"textBox6";
                        this->textBox6->Size = System::Drawing::Size(65, 20);
                        this->textBox6->TabIndex = 8;
                        //
                        // Button1
                        //
                        this->Button1->Location = System::Drawing::Point(15, 36);
                        this->Button1->Name = L"Button1";
                        this->Button1->Size = System::Drawing::Size(146, 29);
                        this->Button1->TabIndex = 11;
                        this->Button1->Text = L"Open Connection";
                        this->Button1->UseVisualStyleBackColor = true;
                        this->Button1->Click += gcnew System::EventHandler(this, &Form1::Button1_Click);
                        //
                        // label6
                        //
                        this->label6->AutoSize = true;
                        this->label6->Location = System::Drawing::Point(23, 153);
                        this->label6->Name = L"label6";
                        this->label6->Size = System::Drawing::Size(70, 13);
                        this->label6->TabIndex = 12;
                        this->label6->Text = L"Modified Last";
                        //
                        // groupBox3
                        //
                        this->groupBox3->Controls->Add(this->textBox6);
                        this->groupBox3->Controls->Add(this->label6);
                        this->groupBox3->Controls->Add(this->textBox5);
                        this->groupBox3->Controls->Add(this->label5);
                        this->groupBox3->Controls->Add(this->textBox4);
                        this->groupBox3->Controls->Add(this->Button6);
                        this->groupBox3->Controls->Add(this->textBox3);
                        this->groupBox3->Controls->Add(this->label4);
                        this->groupBox3->Controls->Add(this->label3);
                        this->groupBox3->Location = System::Drawing::Point(570, 36);
                        this->groupBox3->Name = L"groupBox3";
                        this->groupBox3->Size = System::Drawing::Size(120, 243);
                        this->groupBox3->TabIndex = 15;
                        this->groupBox3->TabStop = false;
                        this->groupBox3->Text = L"Modify";
                        //
                        // textBox5
                        //
                        this->textBox5->Location = System::Drawing::Point(26, 125);
                        this->textBox5->Name = L"textBox5";
                        this->textBox5->Size = System::Drawing::Size(65, 20);
                        this->textBox5->TabIndex = 11;
                        //
                        // label5
                        //
                        this->label5->AutoSize = true;
                        this->label5->Location = System::Drawing::Point(23, 107);
                        this->label5->Name = L"label5";
                        this->label5->Size = System::Drawing::Size(69, 13);
                        this->label5->TabIndex = 10;
                        this->label5->Text = L"Modified First";
                        //
                        // textBox4
                        //
                        this->textBox4->Location = System::Drawing::Point(27, 75);
                        this->textBox4->Name = L"textBox4";
                        this->textBox4->Size = System::Drawing::Size(65, 20);
                        this->textBox4->TabIndex = 9;
                        //
                        // Button6
                        //
                        this->Button6->Location = System::Drawing::Point(6, 198);
                        this->Button6->Name = L"Button6";
                        this->Button6->Size = System::Drawing::Size(104, 31);
                        this->Button6->TabIndex = 5;
                        this->Button6->Text = L"Find and Modify";
                        this->Button6->UseVisualStyleBackColor = true;
                        this->Button6->Click += gcnew System::EventHandler(this, &Form1::Button6_Click);
                        //
                        // textBox3
                        //
                        this->textBox3->Location = System::Drawing::Point(27, 37);
                        this->textBox3->Name = L"textBox3";
                        this->textBox3->Size = System::Drawing::Size(65, 20);
                        this->textBox3->TabIndex = 8;
                        //
                        // label4
                        //
                        this->label4->AutoSize = true;
                        this->label4->Location = System::Drawing::Point(42, 60);
                        this->label4->Name = L"label4";
                        this->label4->Size = System::Drawing::Size(23, 13);
                        this->label4->TabIndex = 7;
                        this->label4->Text = L"last";
                        //
                        // label3
                        //
                        this->label3->AutoSize = true;
                        this->label3->Location = System::Drawing::Point(42, 20);
                        this->label3->Name = L"label3";
                        this->label3->Size = System::Drawing::Size(23, 13);
                        this->label3->TabIndex = 6;
                        this->label3->Text = L"first";
                        //
                        // Button8
                        //
                        this->Button8->Location = System::Drawing::Point(15, 181);
                        this->Button8->Name = L"Button8";
                        this->Button8->Size = System::Drawing::Size(146, 31);
                        this->Button8->TabIndex = 7;
                        this->Button8->Text = L"List Sorted by First and Last";
                        this->Button8->UseVisualStyleBackColor = true;
                        this->Button8->Click += gcnew System::EventHandler(this, &Form1::Button8_Click);
                        //
                        // groupBox2
                        //
                        this->groupBox2->Controls->Add(this->textBox2);
                        this->groupBox2->Controls->Add(this->textBox1);
                        this->groupBox2->Controls->Add(this->label2);
                        this->groupBox2->Controls->Add(this->Button7);
                        this->groupBox2->Controls->Add(this->label1);
                        this->groupBox2->Controls->Add(this->Button4);
                        this->groupBox2->Controls->Add(this->Button5);
                        this->groupBox2->Location = System::Drawing::Point(182, 131);
                        this->groupBox2->Name = L"groupBox2";
                        this->groupBox2->Size = System::Drawing::Size(343, 142);
                        this->groupBox2->TabIndex = 8;
                        this->groupBox2->TabStop = false;
                        this->groupBox2->Text = L"Enter First Last Names";
                        //
                        // textBox2
                        //
                        this->textBox2->Location = System::Drawing::Point(163, 37);
                        this->textBox2->Name = L"textBox2";
                        this->textBox2->Size = System::Drawing::Size(65, 20);
                        this->textBox2->TabIndex = 7;
                        //
                        // textBox1
                        //
                        this->textBox1->Location = System::Drawing::Point(79, 36);
                        this->textBox1->Name = L"textBox1";
                        this->textBox1->Size = System::Drawing::Size(65, 20);
                        this->textBox1->TabIndex = 6;
                        //
                        // label2
                        //
                        this->label2->AutoSize = true;
                        this->label2->Location = System::Drawing::Point(102, 20);
                        this->label2->Name = L"label2";
                        this->label2->Size = System::Drawing::Size(23, 13);
                        this->label2->TabIndex = 5;
                        this->label2->Text = L"first";
                        //
                        // Button7
                        //
                        this->Button7->Location = System::Drawing::Point(168, 67);
                        this->Button7->Name = L"Button7";
                        this->Button7->Size = System::Drawing::Size(146, 26);
                        this->Button7->TabIndex = 6;
                        this->Button7->Text = L"Delete";
                        this->Button7->UseVisualStyleBackColor = true;
                        this->Button7->Click += gcnew System::EventHandler(this, &Form1::Button7_Click);
                        //
                        // label1
                        //
                        this->label1->AutoSize = true;
                        this->label1->Location = System::Drawing::Point(185, 21);
                        this->label1->Name = L"label1";
                        this->label1->Size = System::Drawing::Size(23, 13);
                        this->label1->TabIndex = 4;
                        this->label1->Text = L"last";
                        //
                        // Button4
                        //
                        this->Button4->Location = System::Drawing::Point(16, 62);
                        this->Button4->Name = L"Button4";
                        this->Button4->Size = System::Drawing::Size(146, 31);
                        this->Button4->TabIndex = 3;
                        this->Button4->Text = L"Find WildCard Partial Match";
                        this->Button4->UseVisualStyleBackColor = true;
                        this->Button4->Click += gcnew System::EventHandler(this, &Form1::Button4_Click);
                        //
                        // Button5
                        //
                        this->Button5->Location = System::Drawing::Point(84, 109);
                        this->Button5->Name = L"Button5";
                        this->Button5->Size = System::Drawing::Size(146, 27);
                        this->Button5->TabIndex = 4;
                        this->Button5->Text = L"Insert";
                        this->Button5->UseVisualStyleBackColor = true;
                        this->Button5->Click += gcnew System::EventHandler(this, &Form1::Button5_Click);
                        //
                        // Button9
                        //
                        this->Button9->Location = System::Drawing::Point(15, 231);
                        this->Button9->Name = L"Button9";
                        this->Button9->Size = System::Drawing::Size(146, 31);
                        this->Button9->TabIndex = 13;
                        this->Button9->Text = L"Close Connection";
                        this->Button9->UseVisualStyleBackColor = true;
                        this->Button9->Click += gcnew System::EventHandler(this, &Form1::Button9_Click);
                        //
                        // GroupBox1
                        //
                        this->GroupBox1->Controls->Add(this->groupBox2);
                        this->GroupBox1->Controls->Add(this->Button1);
                        this->GroupBox1->Controls->Add(this->Button8);
                        this->GroupBox1->Controls->Add(this->Button9);
                        this->GroupBox1->Controls->Add(this->Button3);
                        this->GroupBox1->Controls->Add(this->Button2);
                        this->GroupBox1->Controls->Add(this->lstRecords);
                        this->GroupBox1->Location = System::Drawing::Point(21, 17);
                        this->GroupBox1->Name = L"GroupBox1";
                        this->GroupBox1->Size = System::Drawing::Size(531, 292);
                        this->GroupBox1->TabIndex = 14;
                        this->GroupBox1->TabStop = false;
                        this->GroupBox1->Text = L"Stuff to Do";
                        this->GroupBox1->Enter += gcnew System::EventHandler(this, &Form1::GroupBox1_Enter);
                        //
                        // Button3
                        //
                        this->Button3->Location = System::Drawing::Point(12, 134);
                        this->Button3->Name = L"Button3";
                        this->Button3->Size = System::Drawing::Size(149, 31);
                        this->Button3->TabIndex = 2;
                        this->Button3->Text = L"List The Records";
                        this->Button3->UseVisualStyleBackColor = true;
                        this->Button3->Click += gcnew System::EventHandler(this, &Form1::Button3_Click);
                        //
                        // Button2
                        //
                        this->Button2->Location = System::Drawing::Point(15, 85);
                        this->Button2->Name = L"Button2";
                        this->Button2->Size = System::Drawing::Size(144, 29);
                        this->Button2->TabIndex = 1;
                        this->Button2->Text = L"Get Number of Records";
                        this->Button2->UseVisualStyleBackColor = true;
                        this->Button2->Click += gcnew System::EventHandler(this, &Form1::Button2_Click);
                        //
                        // lstRecords
                        //
                        this->lstRecords->FormattingEnabled = true;
                        this->lstRecords->Location = System::Drawing::Point(266, 19);
                        this->lstRecords->Name = L"lstRecords";
                        this->lstRecords->Size = System::Drawing::Size(247, 95);
                        this->lstRecords->TabIndex = 0;
                        //
                        // Form1
                        //
                        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                        this->ClientSize = System::Drawing::Size(710, 326);
                        this->Controls->Add(this->groupBox3);
                        this->Controls->Add(this->GroupBox1);
                        this->Name = L"Form1";
                        this->Text = L"VC.NET MySQL Database Program";
                        this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
                        this->groupBox3->ResumeLayout(false);
                        this->groupBox3->PerformLayout();
                        this->groupBox2->ResumeLayout(false);
                        this->groupBox2->PerformLayout();
                        this->GroupBox1->ResumeLayout(false);
                        this->ResumeLayout(false);

                }
#pragma endregion
        private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                         }
private: System::Void Button1_Click(System::Object^  sender, System::EventArgs^  e)
                 {
                         //Open Connection button
                          if(myConnection==nullptr)
                                  {
                                          myConnection= gcnew OdbcConnection;
                                  }
                         
                         if(myConnection->State==ConnectionState::Open)
                                  {
                                          myConnection->Close();
                                  }                            
                                  String  ^connectionstring="";
                                  String ^username="root";
                                  String ^password="student";
                                  String ^server="localhost";
                                  String ^driver="{MySQL ODBC 3.51 Driver}";
                                  String ^database = "mysampledatabase";
                                  connectionstring = "Driver=" + driver + "; Server=" + server + "; User=" + username + "; Password=" + password + ";Database=" + database + ";";
                                  myConnection->ConnectionString = connectionstring;
                                  try
                                  {
                      myConnection->Open();
                                  }
                  catch (Exception ^e)
                                  {
                                          MessageBox::Show(e->ToString());
                                  }
                                  if (myConnection->State!=ConnectionState::Open)
                                   {
                                           MessageBox::Show("Error Opening Connection");
                                   }
                                  else if(myConnection->State==ConnectionState::Open)
                                          MessageBox::Show("Connection opened!");
                 }
private: System::Void Button2_Click(System::Object^  sender, System::EventArgs^  e)
                 {
                         //Get Number of Records button
                         if(myConnection->State!=ConnectionState::Open)
                          {
                                  MessageBox::Show("error");
                                return;
                          }
              OdbcCommand ^sqlCommand=gcnew OdbcCommand;
                          sqlCommand->Connection=myConnection;
              sqlCommand->CommandText= "select count(*) as RecordCount from nameExample";
                          String ^result=sqlCommand->ExecuteScalar()->ToString();
                          MessageBox::Show(result);
                 }
private: System::Void Button3_Click(System::Object^  sender, System::EventArgs^  e)
                 {
                         //List Records button
                         String ^temp="";
                    if(myConnection->State!=ConnectionState::Open)
                        {
                                MessageBox::Show("Connection is not open");
                                return;
                        }
                        OdbcCommand ^sqlCommand=gcnew OdbcCommand;
                        OdbcDataReader ^result;
                        sqlCommand->Connection=myConnection;
                        sqlCommand->CommandText = "select * from nameExample";
                        result = sqlCommand->ExecuteReader();
                        lstRecords->Items->Clear();
                        while (result->Read())
                        {
                                lstRecords->Items->Add(result->GetValue(0)->ToString() + ", " + result->GetValue(1)->ToString());
                        }
                       
                        result=nullptr;
                        sqlCommand=nullptr;
                 }
private: System::Void Button4_Click(System::Object^  sender, System::EventArgs^  e)
                 {
                         //Find Wildcard Match button
                         lstRecords->Items->Clear();
                         String ^lastname="";
                         String ^firstname="";
                         if(textBox1->Text=="" && textBox2->Text=="")
                         {
                                 MessageBox::Show("Enter First or Last or First and Last Name");
                                 return;
                         }
                         firstname=textBox1->Text;
                         lastname=textBox2->Text;
             String ^sqlFind="";
                         OdbcCommand ^sqlCommand=gcnew OdbcCommand;
                         OdbcDataReader ^result;
                         int found=-1;
                         sqlFind = "select * from nameExample Where last like '%" + lastname + "%' and first like '%" + firstname + "%'";
                         sqlCommand->Connection=myConnection;
                         sqlCommand->CommandText = sqlFind;
                         result = sqlCommand->ExecuteReader();
                         
             found = 0;
                         while(result->Read())
                         {
                lstRecords->Items->Add(result->GetValue(0)->ToString() + " " + result->GetValue(1)->ToString());
                found = found + 1;
                         }
                         MessageBox::Show(found + " record(s) found!");
                         result=nullptr;
                         sqlCommand=nullptr;
                 }
private: System::Void Button5_Click(System::Object^  sender, System::EventArgs^  e)
                 {
                         //Insert button
                         lstRecords->Items->Clear();
                         String ^lastname="";
                         String ^firstname="";
                         
                         if(textBox1->Text=="" || textBox2->Text=="")
                         {
                                 MessageBox::Show("Both First and Last Name Required!");
                                 return;
                         }
                         firstname=textBox1->Text;
                         lastname=textBox2->Text;
             String ^sqlInsert="";
                         
                         OdbcCommand ^sqlCommand=gcnew OdbcCommand;
                         int  result=-1;
                          sqlInsert = "insert into nameExample(first, last) values ('" + firstname + "', '" + lastname + "')";
                          sqlCommand->Connection = myConnection;
                         sqlCommand->CommandText = sqlInsert;
                         
                         result = sqlCommand->ExecuteNonQuery();

                         if(result==0)
                         {
                                 MessageBox::Show("No records inserted!");
                         }
                         else
                         {
                                 MessageBox::Show(result + " record inserted!");
                         }
                         result=-1;
                         sqlCommand=nullptr;
                 }
private: System::Void Button6_Click(System::Object^  sender, System::EventArgs^  e)
                 {
                         //Find and Modify button
                         String ^firstname;
                         String ^lastname;
                         String ^newfirstname;
                         String ^newlastname;
                         if(textBox3->Text=="" && textBox4->Text == "" && textBox5->Text=="" && textBox6->Text=="")
                         {
                                 MessageBox::Show("Enter target first and last names, and modified first and last names!");
                                 return;
                         }
                         firstname=textBox3->Text;
                         lastname=textBox4->Text;      
                         newfirstname=textBox5->Text;
                         newlastname=textBox6->Text;
                         String ^sqlFind;
                         String ^sqlUpdate;
                         OdbcCommand ^sqlCommand=gcnew OdbcCommand;
                         OdbcCommand ^sqlUpdateCommand=gcnew OdbcCommand;
                         OdbcDataReader ^result;
                         int updatecount;
             sqlFind = "select * from nameExample Where last = '" + lastname + "' and first = '" + firstname + "'";
             sqlCommand->Connection = myConnection;
                         sqlCommand->CommandText = sqlFind;
                         result = sqlCommand->ExecuteReader();
                         if(!result->HasRows)
                         {
                                 MessageBox::Show("No records found to modify");
                         }
                         else
                         {     
                                 result->Close();
                                 result=nullptr;
                                 sqlUpdate = "update nameExample set first = '" + newfirstname + "', last = '" + newlastname + "' where first = '" + firstname + "' and last = '" + lastname + "'";
                                 sqlUpdateCommand->Connection = myConnection;
                                 sqlUpdateCommand->CommandText = sqlUpdate;
                                 updatecount = sqlUpdateCommand->ExecuteNonQuery();
                                 if(updatecount==0)
                                 {
                                         MessageBox::Show("No records updated!");
                                 }
                                 else
                                 {
                                         MessageBox::Show("Record updated!");
                                 }
                         }
                         result=nullptr;
                         sqlUpdateCommand=nullptr;//();
                         sqlCommand=nullptr;
                 }
private: System::Void Button7_Click(System::Object^  sender, System::EventArgs^  e)
                 {
                         //Delete button
                         String ^lastname;
                         String ^firstname;
                         firstname=textBox1->Text;
                         lastname=textBox2->Text;
                         String ^sqlDelete;
                         int result;
                         OdbcCommand ^sqlCommand=gcnew OdbcCommand;
                         sqlDelete = "delete from nameExample Where last = '" + lastname + "' and first = '" + firstname + "'";
             sqlCommand->Connection = myConnection;
                         sqlCommand->CommandText = sqlDelete;
                         result = sqlCommand->ExecuteNonQuery();
                         if (result==0)
                         {
                 MessageBox::Show("No records deleted!");
                         }
                         else
                         {
                 MessageBox::Show(result + " record(s) deleted!");
                         }
                         sqlCommand=nullptr;
                 }
private: System::Void Button8_Click(System::Object^  sender, System::EventArgs^  e)
                 {
                         //List Sorted button
                         if( myConnection->State != ConnectionState::Open)
                         {
                                 return;
                         }
             OdbcCommand ^sqlCommand=gcnew OdbcCommand;
                         OdbcDataReader ^result;
                         sqlCommand->Connection = myConnection;
                         sqlCommand->CommandText = "select * from nameExample order by last, first";
             result = sqlCommand->ExecuteReader();
                         lstRecords->Items->Clear();
                         while(result->Read())
                         {
                                 lstRecords->Items->Add(result->GetValue(0)->ToString() + ", " + result->GetValue(1)->ToString());
                         }
                         sqlCommand=nullptr;
                 }
private: System::Void Button9_Click(System::Object^  sender, System::EventArgs^  e)
                 {
                         //Close Connection button
                         if(myConnection==nullptr)
                        {
                                MessageBox::Show("No Connection to close");
                                                  return;
                        }

                        if(myConnection->State!=ConnectionState::Open)
                        {
                                MessageBox::Show("Connection is not open");
                                return;
                        }
                        myConnection->Close();
                        if(myConnection->State==ConnectionState::Closed)
                        {
                                MessageBox::Show("Connection is closed");
                                return;
                        }
                 }
private: System::Void GroupBox1_Enter(System::Object^  sender, System::EventArgs^  e) {
                 }
};
}
To run the program:
1. Click Open Connecton before pressing any other button
2. Perform any of the operations on any of the other buttons.
3. To quit click Close Connection.
This is what the program looks like when it runs:
  
  (Back) 

0 comments:

Post a Comment