所以我在 c# 中有以下字符串,它返回一个 mysql 查询。
string id = cmd.ExecuteScalar().ToString();
问题是如果这个字符串的输入无效,它会使应用程序崩溃。这不应该使应用程序崩溃,而是显示:
MessageBox.Show("Your key (" + KeyNr + ") was invalid or not active. \nPlease check if it is the correct key or you need to buy another month.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
尝试尝试并没有运气。有人可以帮忙吗?
我的一些尝试:
try
{
string id = cmd.ExecuteScalar().ToString();
}
catch (Exception ex)
{
MessageBox.Show("Your key (" + KeyNr + ") was invalid or not active. \nPlease check if it is the correct key or you need to buy another month.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
和
try
{
string id = cmd.ExecuteScalar().ToString();
}
catch (catch (MySqlException))
{
MessageBox.Show("Your key (" + KeyNr + ") was invalid or not active. \nPlease check if it is the correct key or you need to buy another month.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
一切都给了我:
Error 2 The name 'id' does not exist in the current context