As the first step we will add three dialogs needed to support SQL Server installationa and configuration.

SQL Server mode selection step

First we will add a completely custom step that will allow users to select if they want use existing SQL Server or install a dedicated one locally. Depending on the GUI framework you are using the are slight variations.

Windows Forms

When using WinForms you need to:

  • add an Inherited User Control (right click on Gui project node, Add->New Item... ->Windows Forms->Inherited User Control->Browse...->Select SharpSetup.WinForms.dll (from Assemblies folder of SharpSetup installation directory)->ModernActionStep->OK, read more) named SqlModeStep;
  • change step Title and Subtitle properties in Property Editor to fit your needs;
  • add two radio buttons named rbDedicatedSql (with text "Use local dedicated SQL Server (install if needed)") and rbExternalSql (with text "Use existing SQL Server instance") and position them as you see fit;
  • add event handler for MoveNext event with the following code:
private void SqlModeStep_MoveNext(object sender, ChangeStepEventArgs e)
{
var dedicated = rbDedicatedSql.Checked;
    var dedicatedInstalled = !string.IsNullOrEmpty(WindowsRegistry.GetStringValue(@"HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL", "MYINSTANCE", WindowsRegistryReadType.PreferNative));
    Globals.SetVariable("DEDICATEDSQL", dedicated);
    //do not install sql if already installed or not needed
    MsiConnection.Instance.SetProperty("SQL_INSTALLED", dedicatedInstalled || !dedicated ? "true" : "");
    if (dedicated)
    MsiConnection.Instance.SetProperty("SQL_SERVER", ".\\MYINSTANCE");
}
Windows Presentation Foundation

When using WPF you need to:

  • add a new empty action step (right click on Gui project node, Add->New Item... ->SharpSetup->WPF->Empty action dialog (WPF)->click Add button) named SqlModeStep.xaml;
  • change resource values for SqlModeStep_Title, SqlModeStep_Subtitle and SqlModeStep_MessageTop in Properties\Resources.resx to fit your needs;
  • add the following code to SqlModeStep.xaml:
    <StackPanel Margin="10">
    <RadioButton Name="rbDedicatedSql" Content="Use local dedicated SQL Server (install if needed)" Margin="2" IsChecked="True" />
        <RadioButton Name="rbExternalSql" Content="Use existing SQL Server instance" Margin="2" />
    </StackPanel>
  • add event handler for MoveNext event with the following code:
    private void ModernActionStep_MoveNext(object sender, SharpSetup.UI.Wpf.Base.ChangeStepRoutedEventArgs e)
    {
    var dedicated = rbDedicatedSql.IsChecked ?? true;
        var dedicatedInstalled = !string.IsNullOrEmpty(WindowsRegistry.GetStringValue(@"HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL", "MYINSTANCE", WindowsRegistryReadType.PreferNative));
        Globals.SetVariable("DEDICATEDSQL", dedicated);
        //do not install sql if already installed or not needed
        MsiConnection.Instance.SetProperty("SQL_INSTALLED", dedicatedInstalled || !dedicated ? "true" : "");
        if (dedicated)
        MsiConnection.Instance.SetProperty("SQL_SERVER", ".\\MYINSTANCE");
    }

SQL Server instance selection step

The next dialog to add is one that will allow user to select or enter instance name to use and provide credentials. To add this dialog:

  • open "Add New Item" dialog and select "SQL server dialog" under SharpSetup->Windows Forms;
  • set name to eg. SqlStep.cs and click OK button;
  • in Property Editor add handler for Entering event with the following code:
private void SqlStep_Entering(object sender, ChangeStepEventArgs e)
{
if (Globals.GetVariable<bool>("DEDICATEDSQL"))
    Wizard.ContinueMove();
}
  • in event handler for MoveNext event add the first two lines so that it looks like below:
private void SqlStep_MoveNext(object sender, SharpSetup.Base.ChangeStepEventArgs e)
{
if (Globals.GetVariable<bool>("DEDICATEDSQL"))
return;
if (!sssMain.IsConnectionDataValid)
{
MessageBox.Show(this, Resources.SqlStep_CannotConnectDialogText, Resources.SqlStep_CannotConnectDialogCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
e.Continue = false;
}
}

SQL Server database selection step

The last dialog to add is where the user will be able to select database to use for the application. To add this dialog:

  • open "Add New Item" dialog and select "SQL database dialog" under SharpSetup->Windows Forms;
  • in Property Editor add handler for Entering event with the following code:
private void SqlDatabaseStep_Entering(object sender, ChangeStepEventArgs e)
{
if (Globals.GetVariable<bool>("DEDICATEDSQL"))
    {
    if (!string.IsNullOrEmpty(MsiConnection.Instance.GetProperty("SQL_INSTALLED")))
        SetupDatabaseList();
        Wizard.ContinueMove();
    }
}

Making the new dialogs display

To make the new steps display at appropriate places go to code view for SetupWizard.cs and add the following code:

AddStep(new SqlModeStep());
AddStep(new SqlStep());
AddStep(new SqlDatabaseStep());

at the beginning of InstallationMode.Install and InstallationMode.Upgrade sections.

Next steps

In the next articles we will add mechanism to detect existence of SQL Server instance on local system, mechanism of installing it in case it is missing, mechanism for creating and updating database structure.