使用WIX创建/更新数据库

时间:2014-09-16 12:19:28

标签: wix installation custom-action

我目前正在开发一个WIX 3.8安装项目,该项目应该在安装时部署数据库。如果数据库已存在,则应使用脚本更新数据库。不好的是,我无法区分这两种情况。以下几行介绍了我目前的内容:

<Property Id="SQLDATABASEEXISTS" Value="dummy" />

<Binary Id="CustomActions" SourceFile="..\CustomActions\bin\$(var.Configuration)\CustomActions.CA.dll" />

<CustomAction Id="CheckDatabaseExistence" BinaryKey="CustomActions" DllEntry="CheckSqlServerExistence" Return="check" Execute="immediate" />

<InstallExecuteSequence>
    <Custom Action="CheckDatabaseExistence" Before="CostFinalize" />
</InstallExecuteSequence>

<util:User Id="SqlUser" Name="username" Password="password" />
<sql:SqlDatabase Id="SqlExistingDatabase" Database="Data_[CUSTOMER_NAME]" Instance="SQLEXPRESS" Server="." User="SqlUser" />

<Binary Id="SqlCreateScript" SourceFile="SqlCreateScript.sql" />
<Binary Id="SqlUpdateScript" SourceFile="SqlUpdateScript.sql" />

<ComponentGroup Id="DatabaseComponents">
    <Component Id="DBNewDatabase" Directory="DATA_DIR" Guid="*" KeyPath="yes">
        <Condition><![CDATA[NOT SQLDATABASEEXISTS]]></Condition>
        <sql:SqlDatabase Id="SqlNewDatabase" Database="Data_[CUSTOMER_NAME]" ConfirmOverwrite="yes" CreateOnInstall="yes" CreateOnReinstall="yes" CreateOnUninstall="no" DropOnInstall="no" DropOnReinstall="no" DropOnUninstall="no" Instance="SQLEXPRESS" Server="." User="SqlUser">
            <sql:SqlFileSpec Id="SqlDatabaseDataFile" Name="DB_Data" Filename="C:\DB\[CUSTOMER_NAME]\DB_Data.mdf" />
            <sql:SqlLogFileSpec Id="SqlDatabaseLogFile" Name="DB_Log" Filename="C:\DB\[CUSTOMER_NAME]\DB_Data.ldf" />
            <sql:SqlScript Id="SqlCreateScript" ExecuteOnInstall="yes" ExecuteOnReinstall="yes" ExecuteOnUninstall="no" BinaryKey="SqlCreateScript" />
        </sql:SqlDatabase>
    </Component>
    <Component Id="DBExistingDatabase" Directory="DATA_DIR" Guid="*" KeyPath="yes">
        <Condition><![CDATA[SQLDATABASEEXISTS]]></Condition>
        <sql:SqlScript Id="SqlUpdateScript" ExecuteOnInstall="yes" ExecuteOnReinstall="yes" ExecuteOnUninstall="no" BinaryKey="SqlUpdateScript" User="SqlUser" SqlDb="SqlExistingDatabase" />
    </Component>
</ComponentGroup>

CustomAction代码非常简单:

[CustomAction]
public static ActionResult CheckSqlServerExistence(Session session)
{
    session.Log("Begin CheckSqlServerExistence");

    string connectionString = @"Data Source=.\SQLEXPRESS;Connect Timeout=60;Initial Catalog=Data_" + session["CUSTOMER_NAME"] + @";Persist Security Info=True;User ID=username;Password=password";
    try
    {
        SqlConnection connection = new SqlConnection(connectionString);
        connection.Open();
        connection.Close();
        session["SQLDATABASEEXISTS"] = "true";
    }
    catch (Exception ex)
    {
        session.Log("Database connection not possible. Database treated as not existing. Exception: " + ex.Message);
        session["SQLDATABASEEXISTS"] = "false";
    }

    session.Log("Database exists: " + session["SQLDATABASEEXISTS"]);

    return ActionResult.Success;
}

目前的行为是设置始终执行&#34;更新案例&#34;。所以我也将条件修改为

<Condition><![CDATA[SQLDATABASEEXISTS = "false"]]></Condition>

<Condition><![CDATA[SQLDATABASEEXISTS = "true"]]></Condition>

但这也没有帮助。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

<Custom Action="CheckDatabaseExistence" After="CostInitialize" />

和条件:

<Condition><![CDATA[SQLDATABASEEXISTS = "false"]]></Condition>
<Condition><![CDATA[SQLDATABASEEXISTS = "true"]]></Condition>

似乎CostFinalize已经太晚了,无法获得属性值。