-->

Install SQL Server 2012 Express Programmatically

2019-08-15 02:16发布

问题:

I work on Setup application to install all requirement for my WPF product, one of the requirement is SQL Server 2012 Express, the code below is to install it after I generate the configuration file for silent installation :

private void SetupSQLServer()
{
        string result = "";
        string commandLine = "";

        if (os64)
            commandLine = string.Format(@"{0}\SQLServer\sql64\setup.exe PCUSOURCE={0}\SQLServer\sql64 /SAPWD=""p@ssw0rd"" /CONFIGURATIONFILE={0}\SQLServer\ConfigurationFile64.ini /HIDECONSOLE", setupFolder);
        else
            commandLine = string.Format(@"{0}\SQLServer\sql86\setup.exe PCUSOURCE={0}\SQLServer\sql86 /SAPWD=""p@ssw0rd"" /CONFIGURATIONFILE={0}\SQLServer\ConfigurationFile32.ini /HIDECONSOLE", setupFolder);

        startInfo.WorkingDirectory = setupFolder;
        startInfo.Arguments = "/c " + commandLine;
        startInfo.RedirectStandardOutput = true;
        startInfo.UseShellExecute = false;

        process.StartInfo = startInfo;

        try
        {
            process.Start();
        }
        catch (Exception e)
        {
            result = e.Message;
        }

        result = result + "\n" + process.StandardOutput.ReadToEnd();

        UpdateStepResult(result);
    }

There's no error in the code but it does not work .. when I run the code the command window appear and disappear and nothing happen.

UPDATE:

When I used:

fileName = string.Format(@"{0}\SQLServer\sql64\setup.exe", setupFolder);

The installation is run but without configuration file, when I used:

fileName = string.Format(@"{0}\SQLServer\sql64\setup.exe /CONFIGURATIONFILE={0}\SQLServer\sql64\ConfigurationFile64.ini", setupFolder);

It gives me this error "The system cannot find the file specified" !!! The file is exist in the same folder !!

Please can you help me to discover the mistake.

Thanks in advance.

回答1:

The ProcessStartInfo requires the FileName property to be valid. Your code above doesn't set it but pass everything as Arguments.

Probably you need to separate the command line in two parts. The Executable to run and the arguments to pass

   if (os64)
   {
        fileName = string.Format("{0}\SQLServer\sql64\setup.exe", setupFolder);
        commandLine = string.Format(@"PCUSOURCE={0}\SQLServer\sql64 /SAPWD=""p@ssw0rd"" /CONFIGURATIONFILE={0}\SQLServer\ConfigurationFile64.ini /HIDECONSOLE", setupFolder);
    }
    else
    {
         // Same for 32 bit
         .....
    }
    ....
    startInfo.FileName = fileName;
    ....