我目前正在从事个人项目&我无法理解为什么应用程序在出现问题时不会告诉用户。
private void button1_Click(object sender, EventArgs e)
{
if (connect.State == ConnectionState.Open)
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = connect;
cmd.CommandText = "INSERT INTO USERS (Namn,Personnummer,Username,Password,accountid,Avtal,Startdatum) values(@parameter1,@parameter2,@parameter3,@parameter4,@parameter5,@parameter6,@parameter7)";
cmd.Parameters.AddWithValue("@parameter1", (textBox1.Text));
cmd.Parameters.AddWithValue("@parameter2", textBox2.Text);
cmd.Parameters.AddWithValue("@parameter3", (textBox3.Text));
cmd.Parameters.AddWithValue("@parameter4", (textBox4.Text));
cmd.Parameters.AddWithValue("@parameter5", textBox5.Text);
cmd.Parameters.AddWithValue("@parameter6", (textBox6.Text));
cmd.Parameters.AddWithValue("@parameter7", (textBox7.Text));
MySqlDataReader accessed = cmd.ExecuteReader();
MessageBox.Show("Account created!");
}
else
{
MessageBox.Show("Something went wrong, could'nt establish a stable connection");
}
}
至于现在,如果稳定的连接无法启动,应用程序就会崩溃,并且不会告诉用户消息框部分。
答案 0 :(得分:2)
private void button1_Click(object sender, EventArgs e)
{
try{
if (connect.State == ConnectionState.Open)
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = connect;
cmd.CommandText = "INSERT INTO USERS (Namn,Personnummer,Username,Password,accountid,Avtal,Startdatum) values(@parameter1,@parameter2,@parameter3,@parameter4,@parameter5,@parameter6,@parameter7)";
cmd.Parameters.AddWithValue("@parameter1", (textBox1.Text));
cmd.Parameters.AddWithValue("@parameter2", textBox2.Text);
cmd.Parameters.AddWithValue("@parameter3", (textBox3.Text));
cmd.Parameters.AddWithValue("@parameter4", (textBox4.Text));
cmd.Parameters.AddWithValue("@parameter5", textBox5.Text);
cmd.Parameters.AddWithValue("@parameter6", (textBox6.Text));
cmd.Parameters.AddWithValue("@parameter7", (textBox7.Text));
MySqlDataReader accessed = cmd.ExecuteReader();
MessageBox.Show("Account created!");
}
else
{
MessageBox.Show("Something went wrong, could'nt establish a stable connection");
}
}
catch
{
MessageBox.Show("Something went wrong, could'nt establish a stable connection");
}
}
您可以通过各种方式处理Catch
。得到确切的错误。喜欢:
try{}
catch(Exception ex)
{MessageBox.Show(ex.message);}
MSDN link for try catch你可以找到任何其他合适的方法。