gpt4 book ai didi

error-handling - 成功执行mysql查询时如何使用 'try catch finally' block 显示消息

转载 作者:行者123 更新时间:2023-12-03 08:56:28 24 4
gpt4 key购买 nike

我一直在C#Windows表单的代码中使用“try catch finally”来查询MySQL数据库。该代码运行良好:当catch块标记错误时,将在消息框中显示错误消息。

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

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

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

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

谢谢,

彼得

这是一个代码示例:

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

}

关于error-handling - 成功执行mysql查询时如何使用 'try catch finally' block 显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23714306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com