2014-05-17 1 views
0

MySQL 데이터베이스를 쿼리하기위한 C# Windows 폼의 코드에서 'try catch catch'를 사용했습니다. 코드가 잘 작동합니다. catch 블록이 오류를 플래그하면 메시지 상자에 오류 메시지가 표시됩니다.mysql 쿼리가 성공적으로 실행될 때 'try catch finally'블록을 사용하여 메시지를 표시하는 방법

finally 블록에서 데이터베이스가 성공적으로 업데이트 된시기를 나타내는 메시지 상자를 코딩했습니다.

오류 메시지가 없으면 모두 정상적으로 작동합니다. 성공 메시지가 표시됩니다.

그러나 오류가있는 경우 catch 블록의 오류 메시지가 표시되고 finally 블록의 성공 메시지가 표시됩니다.

mysql이 업데이트 될 때 오류 메시지 또는 성공 메시지를 표시하는 프로그램을 얻는 해결책을 아는 사람이 있습니까? { 문자열 myConnection과 = @ "

개인 무효 btnUpdateEmployeeTable_Click (개체를 보낸 사람, EventArgs입니다 E) Employee 테이블에서 레코드를 업데이트 // :

감사합니다,

피터 여기

은 코드 샘플입니다 server = localhost; database = shopdb; 사용자 이름 = **; 암호 = **; 0 datetime 변환 = True "; MySqlConnection Connect = null;

 try 
     { 
      Connect = new MySqlConnection(myConnection); 
      Connect.Open(); //Open the connection 

      //This is the mysql command that we will query into the db. 
      //It uses Prepared statements and the Placeholders for faster, more secure processing. 

      String updateQuery = "UPDATE employee SET emp_lName = @empLastName, emp_fName = @empFirstName WHERE emp_number = @empNum"; 

      MySqlCommand cmdInsertEmployeeToDataBase = new MySqlCommand(updateQuery, Connect); 
      cmdInsertEmployeeToDataBase.Prepare(); 

      //Bind the value to the placeholder    

      cmdInsertEmployeeToDataBase.Parameters.AddWithValue("@empNum", this.txtEmployeeNo.Text); 
      cmdInsertEmployeeToDataBase.Parameters.AddWithValue("@empLastName", this.txtEmployeeLastName.Text); 
      cmdInsertEmployeeToDataBase.Parameters.AddWithValue("@empFirstName", this.txtEmployeeFirstName.Text);     

      cmdInsertEmployeeToDataBase.ExecuteNonQuery(); //Execute the mysql command 


     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message + "\nDatabase could not be updated \n" + "Please try again"); 

     } 
     finally 
     { 
      if (Connect != null) 
      { 
       Connect.Close(); //Close the connection 
      } 


      MessageBox.Show("Database update successful"); 


     } 
    } 
+0

Worked light a charm! 간단하고 뛰어난 솔루션. 감사!! – user3629850

답변

0

성공 코드를 쉽게 위로 이동할 수 있습니다. MessageBox.Show("Database update successful"); 행 앞에 예외가 발생하면 절대로 실행되지 않습니다.

 try 
    { 
     Connect = new MySqlConnection(myConnection); 
     Connect.Open(); //Open the connection 

     //This is the mysql command that we will query into the db. 
     //It uses Prepared statements and the Placeholders for faster, more secure processing. 

     String updateQuery = "UPDATE employee SET emp_lName = @empLastName, emp_fName = @empFirstName WHERE emp_number = @empNum"; 

     MySqlCommand cmdInsertEmployeeToDataBase = new MySqlCommand(updateQuery, Connect); 
     cmdInsertEmployeeToDataBase.Prepare(); 

     //Bind the value to the placeholder    

     cmdInsertEmployeeToDataBase.Parameters.AddWithValue("@empNum", this.txtEmployeeNo.Text); 
     cmdInsertEmployeeToDataBase.Parameters.AddWithValue("@empLastName", this.txtEmployeeLastName.Text); 
     cmdInsertEmployeeToDataBase.Parameters.AddWithValue("@empFirstName", this.txtEmployeeFirstName.Text); 

     cmdInsertEmployeeToDataBase.ExecuteNonQuery(); //Execute the mysql command 

     MessageBox.Show("Database update successful"); 

    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message + "\nDatabase could not be updated \n" + "Please try again"); 

    } 
    finally 
    { 
     if (Connect != null) 
     { 
      Connect.Close(); //Close the connection 
     } 

    }