I solved it and created WIX Database Installer like so:
Product.wxs:
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:iis="http://schemas.microsoft.com/wix/IIsExtension"
xmlns:util="http://schemas.microsoft.com/wix/UtilExtension"
xmlns:sql="http://schemas.microsoft.com/wix/SqlExtension">
<Product Id="*" Name="MySetup2" Language="1033" Version="1.0.0.0" Manufacturer="Hewlett-Packard Company" UpgradeCode="bf1da750-c2fe-4026-9d2b-9d291a61a8b5">
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
<Binary Id="success_bmp" SourceFile="$(sys.SOURCEFILEDIR)success.bmp" />
<Binary Id="error_bmp" SourceFile="$(sys.SOURCEFILEDIR)error.bmp" />
<MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
<MediaTemplate />
<Feature Id="ProductFeature" Title="MySetup2" Level="1" ConfigurableDirectory="INSTALLDIR">
<ComponentGroupRef Id="ProductComponents" />
<ComponentRef Id='SqlComponent.IntegratedAuthentication' />
<ComponentRef Id='SqlComponent.SQLAuthentication' />
</Feature>
<Property Id="WIXUI_INSTALLDIR" Value="INSTALLDIR" />
<Binary Id="SqlScriptSQLAuthentication" SourceFile="c:TempMyDb.sql" />
<Binary Id="SqlScriptIntegratedAuthentication" SourceFile="c:TempMyDb.sql" />
<!-- .NET Framework 3.0 SP 1 must be installed -->
<Property Id="FRAMEWORKBASEPATH">
<RegistrySearch Id="FindFrameworkDir" Root="HKLM" Key="SOFTWAREMicrosoft.NETFramework" Name="InstallRoot" Type="raw"/>
</Property>
<UIRef Id="MyWebUI" />
<util:User Id="SQLUser" Name="[DATABASE_USERNAME]" Password="[DATABASE_PASSWORD]" />
<Property Id='DATABASE_USERNAME'></Property>
<Property Id='DATABASE_PASSWORD'></Property>
<Property Id='DATABASE_NAME'></Property>
<Property Id="DATABASE_SERVER">(local)</Property>
<Property Id="DATABASE_LOGON_TYPE">DatabaseIntegratedAuth</Property>
<Property Id="USEINTEGRATEDSECURITY" Secure="yes"></Property>
</Product>
<Fragment>
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLDIR" Name="MySetup2" >
</Directory>
</Directory>
<Component Id='SqlComponent.SQLAuthentication' Guid='665D641C-3570-4b96-9CA5-2B4C12594A35' KeyPath='yes'>
<Condition><![CDATA[USEINTEGRATEDSECURITY<>1]]></Condition>
<sql:SqlDatabase Id='SqlDatabase.SQLAuthentication' Database='[DATABASE_NAME]' User='SQLUser' Server='[DATABASE_SERVER]' CreateOnInstall='yes' DropOnUninstall='yes' ContinueOnError='no' />
<sql:SqlScript Id='SqlScriptSQLAuthentication' BinaryKey='SqlScriptSQLAuthentication' SqlDb='SqlDatabase.SQLAuthentication' ExecuteOnInstall='yes' />
</Component>
<Component Id='SqlComponent.IntegratedAuthentication' Guid='E5DF48AE-2338-4029-9FDF-8DAA6AD0216D' KeyPath='yes'>
<Condition>USEINTEGRATEDSECURITY = 1</Condition>
<sql:SqlDatabase Id='SqlDatabase.IntegratedAuthentication' Database='[DATABASE_NAME]' Server='[DATABASE_SERVER]' CreateOnInstall='yes' DropOnUninstall='yes' ContinueOnError='no' />
<sql:SqlScript Id='SqlScriptIntegratedAuthentication' BinaryKey='SqlScriptIntegratedAuthentication' SqlDb='SqlDatabase.IntegratedAuthentication' ExecuteOnInstall='yes' />
</Component>
</Directory>
</Fragment>
<Fragment>
<ComponentGroup Id="ProductComponents" Directory="INSTALLDIR">
<!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->
<!-- <Component Id="ProductComponent"> -->
<!-- TODO: Insert files, registry keys, and other resources here. -->
<!-- </Component> -->
</ComponentGroup>
</Fragment>
</Wix>
Notice "Condition" operator based on Windows authentication vs. Server authentication.
Custom Action:
namespace CustomActions
{
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Security.Principal;
using Microsoft.Deployment.WindowsInstaller;
using Microsoft.SqlServer.Management.Smo;
using View = Microsoft.Deployment.WindowsInstaller.View;
public static class CustomActions
{
#region Public Methods and Operators
[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;
}
[CustomAction]
public static ActionResult VerifySqlConnection(Session session)
{
try
{
//Debugger.Break();
session.Log("VerifySqlConnection: Begin");
var builder = new SqlConnectionStringBuilder
{
DataSource = session["DATABASE_SERVER"],
InitialCatalog = "master",
ConnectTimeout = 5
};
if (session["DATABASE_LOGON_TYPE"] != "DatabaseIntegratedAuth")
{
builder.UserID = session["DATABASE_USERNAME"];
builder.Password = session["DATABASE_PASSWORD"];
}
else
{
builder.IntegratedSecurity = true;
}
using (var connection = new SqlConnection(builder.ConnectionString))
{
if (connection.CheckConnection(session))
{
session["ODBC_CONNECTION_ESTABLISHED"] = "1";
}
else
{
session["ODBC_CONNECTION_ESTABLISHED"] = string.Empty;
}
}
session.Log("VerifySqlConnection: End");
}
catch (Exception ex)
{
session.Log("VerifySqlConnection: exception: {0}", ex.Message);
throw;
}
return ActionResult.Success;
}
#endregion
#region Methods
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;
}
private static bool HasAdminRights()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
private static bool CheckConnection(this SqlConnection connection, Session session)
{
try
{
if (connection == null)
{
return false;
}
connection.Open();
var canOpen = connection.State == ConnectionState.Open;
connection.Close();
return canOpen;
}
catch (SqlException ex)
{
session["ODBC_ERROR"] = ex.Message;
return false;
}
}
#endregion
}
}
MyWebUI.wxs
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<EnsureTable Id='ComboBox'/>
<!--The custom action DLL itself.-->
<Binary Id="WebAppCA"
SourceFile="C:TempMvcApplication4aCustomActionsinDebugCustomActions.CA.dll" />
<!--The custom action to enumerate the web sites and app pools into the
appropriate combo boxes.-->
<CustomAction Id="EnumerateSqlServers"
BinaryKey="WebAppCA"
DllEntry="EnumerateSqlServers"
Execute="immediate"
Return="check" />
<CustomAction Id="VerifySqlConnection"
BinaryKey="WebAppCA"
DllEntry="VerifySqlConnection"
Execute="immediate"
Return="check" />
<!--Make sure the enumerate web sites and app pools custom action gets
called, but only called if we are doing and install.-->
<InstallUISequence>
<Custom Action="EnumerateSqlServers"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…