如何在MySQL查询成功执行时使用"尝试捕获"块显示消息



我一直在代码中使用'try catch finally'为c# Windows窗体查询MySQL数据库。代码运行良好:当catch块标记错误时,消息框将显示错误消息。

在最后一个块中,我编写了一个消息框,当数据库成功更新时将显示。

如果没有错误信息,则一切正常。成功消息显示。

但是如果出现错误,则在catch块中显示错误消息,然后在finally块中显示成功消息。

有谁知道一个解决方案,让程序显示错误消息或成功消息时,mysql更新?

谢谢,彼得

下面是一个代码示例:

private void btnUpdateEmployeeTable_Click(object sender, EventArgs e)//更新Employee表中的记录{字符串myConnection = @"server=localhost;数据库= shopdb;用户名= * *;密码= * * ;convert zero 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");

        }
    }

您可以轻松地将成功代码移到更高的位置。如果在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
        }
     }

相关内容

  • 没有找到相关文章

最新更新