Error handling 如何使用';最后试试catch';块以在成功执行mysql查询时显示消息
我在C#Windows窗体的代码中使用了“try-catch-finally”来查询MySQL数据库。代码运行良好:当catch块标记错误时,消息框将显示错误消息 在finally块中,我编写了一个消息框,它将在数据库成功更新时显示 如果没有错误消息,则一切正常。成功消息显示 但是,如果有错误,将在catch块中显示错误消息,然后在finally块中显示成功消息 有没有人知道一种解决方案,可以让程序在更新mysql时显示错误消息或成功消息 谢谢 彼得 下面是一个代码示例: 私有void btnUpdateEmployeeTable_单击(对象发送者,事件参数)//更新Employee表中的记录 { 字符串myConnection=@“服务器=localhost;数据库=shopdb;用户名=**;密码=**;转换零日期时间=True”; MySqlConnection=nullError handling 如何使用';最后试试catch';块以在成功执行mysql查询时显示消息,error-handling,try-catch-finally,Error Handling,Try Catch Finally,我在C#Windows窗体的代码中使用了“try-catch-finally”来查询MySQL数据库。代码运行良好:当catch块标记错误时,消息框将显示错误消息 在finally块中,我编写了一个消息框,它将在数据库成功更新时显示 如果没有错误消息,则一切正常。成功消息显示 但是,如果有错误,将在catch块中显示错误消息,然后在finally块中显示成功消息 有没有人知道一种解决方案,可以让程序在更新mysql时显示错误消息或成功消息 谢谢 彼得 下面是一个代码示例: 私有void btnU
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
}
}
工作灯的魅力!简单,精彩的解决方案。谢谢!!