从c#插入msi数据库

时间:2016-01-11 07:11:06

标签: c# windows-installer

我正在尝试在msi文件中插入一个新属性。我可以使用以下代码更新msi数据库文件。有没有办法将新值添加到表中。我无法找到任何。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsInstaller;

namespace msiExample
{
[System.Runtime.InteropServices.ComImport(), System.Runtime.InteropServices.Guid("000C1090-0000-0000-C000-000000000046")]
class Installer { }
class msiMain
{
    static void Main(string[] args)
    {
     WindowsInstaller.Installer ins = (WindowsInstaller.Installer)new Installer();

            string strFileMsi = @"C:\APP.msi";

           System.Console.WriteLine("STARTING SECOND QUERY");
            Database db2 = ins.OpenDatabase(strFileMsi, WindowsInstaller.MsiOpenDatabaseMode.msiOpenDatabaseModeDirect);
            View vw2 = db2.OpenView(@"Select * FROM Property where Value='Unknown'");  

            vw2.Execute(null);

            Record rcrd2 = vw2.Fetch();
            while (rcrd2 != null)
            {
                System.Console.WriteLine(rcrd2.get_StringData(1));
                rcrd2.set_StringData(1,"No data");
                vw2.Modify(WindowsInstaller.MsiViewModify.msiViewModifyUpdate, rcrd2);

                rcrd2 = vw2.Fetch();

            }               
            db2.Commit();
            vw2.Close();
            System.Console.WriteLine("completed");   
    }
  }
}

3 个答案:

答案 0 :(得分:3)

Windows Installer XML(WiX)部署工具基础(DTF)库在这里有很多帮助。我知道这样做最简单的方法是:

using Microsoft.Deployment.WindowsInstaller.Linq;
using (QDatabase database = new QDatabase(@"C:\data\test.msi", DatabaseOpenMode.Direct))
{
  var record = database.Properties.NewRecord();
  record.Property = "MyProperty";
  record.Value = "MyValue";
  record.Insert();
}

如果您仍然想要SQL,那么:

using Microsoft.Deployment.WindowsInstaller;
using (Database database = new Database(@"C:\data\test.msi", DatabaseOpenMode.Direct))
{
  database.Execute("INSERT INTO `Property` (`Property`, `Value`) VALUES('MyProperty', 'MyValue')");
}

答案 1 :(得分:2)

来自Christopher Painter的DTF答案更好,但根据您的代码使用com objet:

WindowsInstaller.View vw2 = db2.OpenView("INSERT INTO Property (Property, Value) VALUES ('property_name', 'property_value')");
vw2.Execute(null);
db2.Commit();
vw2.Closed();

答案 2 :(得分:0)

使用`作为表和列的名称
'用于字符串值