在MSI安装程序脚本中尝试/捕获

时间:2011-05-06 20:00:55

标签: c# windows installer windows-installer

我有一个Windows Installer提示用户输入MySQL信息(服务器,端口,用户名,密码),并希望在完成设置之前确保参数正确。

我有一个安装程序项目,其中自定义操作链接到安装程序类,我正在使用“安装”方法进行错误检查。我的错误发生在catch()部分。

非常感谢任何建议或意见。

编辑:当我点击[Next]时,安装程​​序将安装应用程序,然后提示MessageBox(如果有错误),但完成安装。我希望它能够回滚并调出前一个屏幕。

Edit2:从MySqlException捕获的异常是:“无法连接任何指定的MySQL主机”,这是正确的,但安装程序不会返回/回滚。

public override void Install(IDictionary stateSaver)
{
    base.Install(stateSaver);

    // parameters from installer
    // generate connection string conStr
    MySqlConnection conn = new MySqlConnection(conStr);
    MySqlCommand cmd = new MySqlCommand();

    // open connection and create database
    try
    {
        conn.Open();

        cmd.Connection = conn;
        // create database
        // create table
        // insert values to test
    }
    catch (MySqlException ex)
    {
      //I would like this to go back to the prior page
      //where it asks for user input
        MessageBox.Show("There was a problem connecting to the database.");
        this.Rollback(stateSaver); // not working?
    }

    // close connection
    conn.Close();
}

1 个答案:

答案 0 :(得分:1)

要强制回滚当前安装,可以重新抛出异常。你只是在消息框中显示。

相关问题