WiX: Read content of file before installation

2020-02-14 20:46发布

问题:

I want to read the content of a file in WiX, so that I can use them during the installation [they are SQL scripts that I want to execute with a custom action]

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="INSTALLFOLDER">
            <Component Id="cmp35BC97AC12F21A6F6154759C2D9B5430" Guid="*">
                <File Id="filDEDB241BDDF1A0CD0B001190398F5F5F" KeyPath="yes" Source="SourceDir\TestScript - Copy.sql" />
            </Component>
            <Component Id="cmp221D9003B2C58612888DBC0D1CEBC730" Guid="*">
                <File Id="filF6EA2F11A51AF10E1517FFB9152307EE" KeyPath="yes" Source="SourceDir\TestScript.sql" />
            </Component>
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="AgentX.Database.Installer">
            <ComponentRef Id="cmp35BC97AC12F21A6F6154759C2D9B5430" />
            <ComponentRef Id="cmp221D9003B2C58612888DBC0D1CEBC730" />
        </ComponentGroup>
    </Fragment>
</Wix>

I have used this code to check the content of the file:

var fileView = session.Database.OpenView("SELECT * FROM File");
            fileView.Execute(null);

            foreach (Record fileRec in fileView)
            {

                session.Log("\t{0}", fileRec["FileName"]);
            }

I want to read the sql files, embedded in the MSI.

回答1:

SqlScript: I almost never use this feature, but WiX has a SqlScript Element that should be able to do the job for you. I keep wondering if SQL scripts should be run by an application and not by a setup, but that is another discussion.

Snippet (from WiX tutorial - downloadable sample towards bottom):

<Binary Id='CreateTable' SourceFile='CreateTable.sql' />

<..>

<Component Id='SqlComponent' Guid='YOURGUID-D8C7-4102-BA84-9702188FA316'>
  <sql:SqlDatabase Id='SqlDatabase' Database='Foobar' User='SQLUser' Server='[SQLSERVER]'
    CreateOnInstall='yes' DropOnUninstall='yes' ContinueOnError='yes'>
    <sql:SqlScript Id='CreateTable' BinaryKey='CreateTable' ExecuteOnInstall='yes' />
  </sql:SqlDatabase>
</Component>

Links:

  • General WiX: Further WiX links here (documentation and help material links)

SQL:

  • WiX tutorial: https://www.firegiant.com/wix/tutorial/sql/creating-a-database/ (same link as above)
  • WiX mailing list: Maybe see the bottom entry here (don't create database fresh on install)
  • Stackoverflow:
    • How to run sql script to create database using Wix
    • Running SQL Script file through Wix using <sql:SqlScript> element


标签: c# wix