修改数据库

时间:2016-03-02 19:58:52

标签: c# database wix custom-action

我一直在疯狂地想弄清楚为什么视图没有更新。从我在网上看到的所有帖子中我都认为我做得对,但我一直得到Null错误。如果我尝试使用session.Database.FilePath以这种方式加载数据库,则表明FilePath为null。视图本身返回Dummy Row,因此数据库就在那里。这很奇怪。

以下属性

<Property Id="IIS_SITE" />

二进制和自定义操作

<CustomAction Id="UpdateComboBoxes" DllEntry="UpdateComboBoxes" BinaryKey="UpdateComboBoxes" Execute="immediate" Return="check" />
<Binary Id="UpdateComboBoxes" SourceFile="..\ProjectName.CustomActions\bin\Release\ProjectName.CustomActions.CA.dll"/>

安装UI序列

<InstallUISequence>
  <Custom Action="UpdateComboBoxes" Before="CostFinalize"></Custom>
</InstallUISequence>

控件

   <Control Id="IisSite" Type="ComboBox" Sorted="yes" ComboList="yes" Property="IIS_SITE" X="45" Y="85" Width="220" Height="18" >
      <ComboBox Property="IIS_SITE" >
        <ListItem Text="Dummy" Value="Dummy"/>
      </ComboBox>
    </Control>

自定义操作

     [CustomAction]
    public static ActionResult UpdateComboBoxes(Session session)
    {
        session.Log("Begin Custom Action UpdateComboBoxes");
        try
        {
            session.Log(string.Format("Database Location is: {0}", session.Database.FilePath));
            var database = session.Database;
                using (var view = database.OpenView("SELECT * FROM ComboBox WHERE Property = 'IIS_SITE'"))
                {

                    view.Execute();
                    session.Log("Executed view");

                    var isReadOnly = database.IsReadOnly;

                    session.Log(string.Format("Database is read only: {0}", isReadOnly));
                    session.Log(string.Format("# of rows in ComboBox Table: {0}",
                        view.Database.CountRows("ComboBox", "Property = 'IIS_SITE'")));

                    using (var serverManager = new ServerManager())
                    {
                        session.Log("Accessed Server Manager");
                        var index = 1;
                        var rowIndex = 1;
                        session.Log(string.Format("Going through {0} sites", serverManager.Sites.Count));
                        foreach (var site in serverManager.Sites)
                        {
                            if (!string.IsNullOrEmpty(site.Name))
                            {
                                session.Log(string.Format("Site # {0} {1}", index, site.Name));
                                var record = session.Database.CreateRecord(4);
                                //Property
                                record.SetString(1, "IIS_SITE");
                                //Order
                                record.SetString(2, rowIndex.ToString());
                                //Value
                                record.SetString(3, site.Name);
                                //Text
                                record.SetString(4, site.Name);

                                session.Log(string.Format("Modifying the view for site # {0}", index));
                                view.Modify(ViewModifyMode.InsertTemporary, record);
                            }
                            session.Log("Incrementing index");
                            index++;
                            rowIndex++;
                        }
                    }

                    session.Log("Closing the view");
                }
        }
        catch (Exception e)
        {

            session.Log(string.Format("ERROR in UpdateComboBoxes: {0}", e.Message));
            session.Log(e.StackTrace);
            var inner = e.InnerException;
            if(inner != null)
            {
                session.Log(string.Format("{0}{1}", "\t", inner.Message));
                session.Log(string.Format("{0}{1}", "\t", inner.StackTrace));
            }
            while ((inner = inner.InnerException) != null)
            {
                session.Log(string.Format("{0}{1}", "\t", inner.Message));
                session.Log(string.Format("{0}{1}", "\t", inner.StackTrace));
            }
            return ActionResult.Failure;
        }
        return ActionResult.Success;
    }

我得到的错误:

  

MSI(c)(B0!F4)[14:46:40:369]:注意:1:2259 2:3:4:
  UpdateComboBoxes中的错误:执行期间函数失败。      在Microsoft.Deployment.WindowsInstaller.View.Modify(ViewModifyMode模式,记录记录)      在VideoQuestionInstaller.CustomActions.CustomActions.UpdateComboBoxes(会话会话)   自定义操作引发的异常:   System.Reflection.TargetInvocationException:调用目标抛出了异常。 ---&GT; System.NullReferenceException:未将对象引用设置为对象的实例。      在VideoQuestionInstaller.CustomActions.CustomActions.UpdateComboBoxes(会话会话)      ---内部异常堆栈跟踪结束---      在System.RuntimeMethodHandle.InvokeMethod(对象目标,对象参数,签名sig,布尔构造函数)      at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,Object parameters,Object arguments)      在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object parameters,CultureInfo culture)      在Microsoft.Deployment.WindowsInstaller.CustomActionProxy.InvokeCustomAction(Int32 sessionHandle,String entryPoint,IntPtr remotingDelegatePtr)

我查了一下,错误2259意味着数据库无法更新,这一点非常清楚,因为错误之前的最后一个日志是:修改网站#1的视图。

有没有人对我做错了什么有什么想法,以及为什么ComboBox数据库没有更新?

提前致谢!

1 个答案:

答案 0 :(得分:0)

没关系,我想出了问题所在。这是我如何解决它:

我添加了另一个隐藏的控件,以便创建组合框表而不必将假值放入我将要使用的实际组合框中

    <Property Id="HIDDEN_IIS_SITE" />

    <Control Id="DummyComboBox" Hidden="yes" Type="ComboBox" Sorted="yes" ComboList="yes" Property="HIDDEN_IIS_SITE" X="45" Y="85" Width="220" Height="18" >
      <ComboBox Property="HIDDEN_IIS_SITE" >
        <ListItem Text="Dummy" Value="Dummy"/>
      </ComboBox>
    </Control>

然后我更改了自定义操作,以便它获得实际的现有行数,然后在添加记录时添加到数字中,以便使用订单的正确数字

    [CustomAction]
    public static ActionResult UpdateComboBoxes(Session session)
    {
        session.Log("Begin Custom Action UpdateComboBoxes");
        try
        {
            var database = session.Database;
                using (var view = database.OpenView("SELECT * FROM ComboBox WHERE Property = 'IIS_SITE'"))
                {

                    view.Execute();
                    session.Log("Executed view");

                    var index = view.Database.CountRows("ComboBox", "Property = 'IIS_SITE'");

                    using (var serverManager = new ServerManager())
                    {
                        foreach (var site in serverManager.Sites)
                        {
                            if (!string.IsNullOrEmpty(site.Name))
                            {
                                var record = session.Database.CreateRecord(4);
                                //Property
                                record.SetString(1, "IIS_SITE");
                                //Order
                                record.SetString(2, (++index).ToString());
                                //Value
                                record.SetString(3, site.Name);
                                //Text
                                record.SetString(4, site.Name);

                                view.InsertTemporary(record);

                                session.Log("Inserted new record");
                            }
                        }
                    }
                    session.Log("Closing the view");
                }
        }
        catch (Exception e)
        {

            session.Log(string.Format("ERROR in UpdateComboBoxes: {0}", e.Message));
            session.Log(e.StackTrace);
            var inner = e.InnerException;
            if(inner != null)
            {
                session.Log(string.Format("{0}{1}", "\t", inner.Message));
                session.Log(string.Format("{0}{1}", "\t", inner.StackTrace));
            }
            while (inner != null && (inner = inner.InnerException) != null)
            {
                session.Log(string.Format("{0}{1}", "\t", inner.Message));
                session.Log(string.Format("{0}{1}", "\t", inner.StackTrace));
            }
            return ActionResult.Failure;
        }
        return ActionResult.Success;
    }