Wix Load items into ComboBox

可紊 提交于 2019-11-30 10:23:46

I figured it out:

Dialogs.wxs:

...                <Control Id="ServerLabel" Type="Text" X="20" Y="62" Width="80" Height="25" NoPrefix="yes" Text="SQL Database:" />
                <Control Id="Server" Type="ComboBox" Height="16" Width="180" X="110" Y="60" Property="DATABASE_SERVER">
                    <ComboBox Property="DATABASE_SERVER">
                        <ListItem Text="[DATABASE_SERVER]" Value="[DATABASE_SERVER]" />
                    </ComboBox>
                    <Publish Property="LOGON_VALID" Value="0">1</Publish>
                </Control>
...

CustomAction:

    [CustomAction]
    public static ActionResult EnumerateSqlServers(Session session)
    {
        if (null == session)
        {
            throw new ArgumentNullException("session");
        }

        session.Log("EnumerateSQLServers: Begin");

        // Check if running with admin rights and if not, log a message to
        // let them know why it's failing.
        if (false == HasAdminRights())
        {
            session.Log("EnumerateSQLServers: " + "ATTEMPTING TO RUN WITHOUT ADMIN RIGHTS");
            return ActionResult.Failure;
        }

        ActionResult result;

        DataTable dt = SmoApplication.EnumAvailableSqlServers(false);
        DataRow[] rows = dt.Select(string.Empty, "IsLocal desc, Name asc");
        result = EnumSqlServersIntoComboBox(session, rows);

        session.Log("EnumerateSQLServers: End");
        return result;
    }

    private static ActionResult EnumSqlServersIntoComboBox(Session session, IEnumerable<DataRow> rows)
    {
        try
        {
            //Debugger.Break();

            session.Log("EnumSQLServers: Begin");

            View view = session.Database.OpenView("DELETE FROM ComboBox WHERE ComboBox.Property='DATABASE_SERVER'");
            view.Execute();

            view = session.Database.OpenView("SELECT * FROM ComboBox");
            view.Execute();

            Int32 index = 1;
            session.Log("EnumSQLServers: Enumerating SQL servers");
            foreach (DataRow row in rows)
            {
                String serverName = row["Name"].ToString();

                // Create a record for this web site. All I care about is
                // the name so use it for fields three and four.
                session.Log("EnumSQLServers: Processing SQL server: {0}", serverName);

                Record record = session.Database.CreateRecord(4);
                record.SetString(1, "DATABASE_SERVER");
                record.SetInteger(2, index);
                record.SetString(3, serverName);
                record.SetString(4, serverName);

                session.Log("EnumSQLServers: Adding record");
                view.Modify(ViewModifyMode.InsertTemporary, record);
                index++;
            }

            view.Close();

            session.Log("EnumSQLServers: End");
        }
        catch (Exception ex)
        {
            session.Log("EnumSQLServers: exception: {0}", ex.Message);
            throw;
        }

        return ActionResult.Success;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!