Search code examples
c#databasewindows-installer

Problem with modifying an MSI database property


I need to modify the UpgradeCode property of the Upgrade MSI table via C#. This code works ok with other tables' properties, but throws an error when I'm trying to modify these.

using (var database = new Database(TEMPDATABASE, DatabaseOpenMode.Direct))        
{
    string upgradeCode = Guid.NewGuid().ToString("B").ToUpper();
    database.Execute("Update `Upgrade` Set `Upgrade`.`UpgradeCode` = '{0}'", upgradeCode);
}

The error is:

Microsoft.Deployment.WindowsInstaller.InstallerException: 'Function failed during execution.'


Solution

  • I got curious and pillaged github.com - and it giveth the following: Full project - just download it as a whole.

    The actual code was (some unicode line feed issues in the file on github.com, I have fixed them up here):

    public static void UpdateUpgradeTable(this Database db, Guid upgradeCode)
    {
        using (View view = db.OpenView("SELECT * FROM `Upgrade`", new object[0]))
        {
            view.Execute();
            using (Record record = view.Fetch())
            {
                record[1] = upgradeCode.ToString("B").ToUpperInvariant();
                view.Replace(record);
            }
    
            db.Commit();
        }
    }
    

    I took the above and made the following mock-up (very ugly, but it worked):

    using (Database db = new Database(@"C:\Test.msi", DatabaseOpenMode.Direct))
    {
        using (View view = db.OpenView("SELECT * FROM `Upgrade`", new object[0]))
        {
            view.Execute();
            using (Record record = view.Fetch())
            {
                record[1] = "{777888DD-1111-1111-1111-222222222222}";
                record[2] = "";
                record[3] = "4.0.1";
                record[4] = "";
                record[5] = "1";
                record[6] = "";
                record[7] = "WIX_UPGRADE_DETECTED";
                view.Replace(record);
            }
    
            db.Commit();
    
            using (Record record = view.Fetch())
            {
                record[1] = "{777888DD-1111-1111-1111-222222222222}";
                record[2] = "";
                record[3] = "";
                record[4] = "4.0.1";
                record[5] = "1";
                record[6] = "";
                record[7] = "WIX_DOWNGRADE_DETECTED";
                view.Replace(record);
            }
    
            db.Commit();
        }
    }