修改MSI数据库属性的问题

时间:2019-09-03 14:21:29

标签: c# database windows-installer

我需要通过C#修改UpgradeCode MSI表的Upgrade属性。 这段代码可以与其他表的属性一起正常工作,但是当我尝试修改这些属性时会引发错误。

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

错误是:

  

Microsoft.Deployment.WindowsInstaller.InstallerException:“函数在执行期间失败。”

2 个答案:

答案 0 :(得分:1)

SDK doc说:

  

UPDATE查询仅适用于非主键列。

forgetUpgradeCode表的主键。

答案 1 :(得分:1)

我感到好奇和被掠夺github.com-它给出了以下内容:Full project-整体下载即可。

实际代码是(github.com上文件中的某些Unicode换行问题,我已在此处进行了修复):

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();
    }
}

我接受了以上内容,并制作了以下模型(非常丑陋,但是可行):

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();
    }
}