How to run sql script to create database using Wix


I'm running into problem creating database with Wix. The examples(see link below) i can find are all creating database first using wix and running a create table sql script. The problem is that i need to set the wix to run sql script with CREATE DATABASE to create the database and tables, and NOT to create the database first using the SqlDatabase tag in .wxs file. Is this possible to do this in Wix? I'm using SQL Server Express. Any example on how to do this would be helpful.

Example of sql script:

CREATE TABLE Test (Value1 CHAR(50), Value2 INTEGER)
CREATE INDEX TestIndex ON Test (Value1)

Thanks in advance. Any help is much appreciated.

Best Solution

Why don't you have a script file containing SQL code to create the database.

<Wix xmlns="" 
  <Product Name='SQL app 1.0' Id='DB501C18-86C7-4D14-AEC0-86416A69ABDE' Language='1033' Codepage='1252'
     Version='1.0.0' Manufacturer='ABC Ltd.'>
            <Package Id='????????-????-????-????-????????????' Keywords='Installer' Description="SQL App 1.0 Installer"
              Comments='Comment.' Manufacturer='ABC Ltd.' InstallerVersion='100'
              Languages='1033' Compressed='yes' SummaryCodepage='1252' />
        <Media Id='1' Cabinet='' EmbedCab='yes' />
        <User Id="MySQLUser" Name="[SQLUSER]" Password="[SQLUSERPASSWORD]"></User>
        <Directory Id='TARGETDIR' Name='SourceDir'>
            <Directory Id='ProgramFilesFolder' Name='PFiles'>
                <Directory Id='INSTALLDIR' Name='TestSQL'>
                      <Component Id="MySqlComponent" Guid="C50999A0-02FD-42d5-9F65-7375318DD328">
                        <SqlDatabase Id="MySqlDatabase" Database="MyDatabase" Server="[SQLSERVER]" Instance="[SQLINSTANCE]"
                            CreateOnInstall="yes" DropOnUninstall="yes" User="MySQLUser" ContinueOnError="yes">
                              <SqlScript Id="CreateDatabase" ExecuteOnInstall="yes" BinaryKey="CreateTablesBin"></SqlScript>
        <Binary Id="CreateTablesBin" src="CreateDatabase.sql"></Binary>
        <Feature Id='Complete' Level='1' Description="Full" Title="Full Installation">
            <ComponentRef Id='MySqlComponent' />
Related Question