Esempio n. 1
0
 private void clearButton_Click(object sender, EventArgs e)
 {
     MySqlStatementTextBox.Clear();
     Result.Items.Clear();
 }
Esempio n. 2
0
 private void ExecuteButton_Click(object sender, EventArgs e)
 {
     Result.Items.Clear();
     try
     {
         // MySqlConnection to declare the server address, id, password, database
         using (MySqlConnection Connection = new MySqlConnection("server=10.158.56.53;uid=csci473g58;pwd=wordpass58;database=csci473g58;"))
         {
             Connection.Open();                                                      // Opens the connection to perfrom MySql operations
             MySqlCommand QueryCommand = new MySqlCommand(MySqlStatementTextBox.Text, Connection);
             string       query        = MySqlStatementTextBox.Text.Substring(0, 6); // Gets the first 6 letters of the query text box
             // If the query is select command
             if (query.ToLower() == "select")
             {
                 try
                 {
                     MySqlDataReader Reader = QueryCommand.ExecuteReader(); // MySqlReader to execute the select command
                     WriteColumn((IDataRecord)Reader);                      // ReadColumn() to add the column names to the Result
                     while (Reader.Read())
                     {
                         WriteSingleRow((IDataRecord)Reader);         // Calls the ReadSingleRow() to add the result to the Result
                     }
                     Reader.Close();
                     MySqlStatementTextBox.Clear();
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show("Error in query entered \n" + ex);      // When the query is not executed due to error, exception is shown
                     MySqlStatementTextBox.Clear();
                 }
             }
             // If the query is insert command
             else if (query.ToLower() == "insert")
             {
                 try
                 {
                     QueryCommand.ExecuteNonQuery();         // Executes the insert query
                     Result.Items.Add("New record is added");
                     MySqlStatementTextBox.Clear();
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show("Record is not inserted \n" + ex);      // When the query is not executed due to error, exception is shown
                     MySqlStatementTextBox.Clear();
                 }
             }
             // If the query is something else
             else
             {
                 Result.Items.Clear();
                 Result.Items.Add("The entered MySql command is not yet implemented");
                 MySqlStatementTextBox.Clear();
             }
             Connection.Close();         // Closes the connection
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Connection is not established \n" + ex);       // Shows the exception if the connection is not established
         Application.Exit();
     }
 }