简体   繁体   中英

Uninstall SQL Server Express 2005 then install SQL Server 2008 R2 Express in C# ClickOnce Winform App

My app is deployed internally using ClickOnce and has a prerequisite of SQL Server 2005 Express.

I want to upgrade my user's to SQL Server 2008 R2 Express. What are my options that don't involve me 'touching' all 300 laptops?

My idea, in theory only, was to drop the SQL Server Express prereq completely, put an 'upgrade' prompt in my app and give the users a couple days to click it, then re-add SQL Server Express as a prereq but as the new version.

I think this would work though I am open to suggestions otherwise. However, my actual question is how I can accomplish my 'upgrade' prompt. How do I uninstall SQL Server Express in a C# Winform app?

Thanks,

See this MSDN article on how to install SQL Server 2008 R2 silently (but why not do 2012 instead? :)

A quick cheat is (I have done this with non express editions, but should be the same process) to go through manual upgrade first to gather all the answers for the config and settings, and just before executing actual upgrade, in the final step you should see towards the bottom a path to the answer (ini) file (see below image), if you cancel and grab that file, you can run it in command line like Setup.exe /ConfigurationFile=MyConfigurationFile.INI

在此输入图像描述

Once you test it out, you should be able to create something that pulls the binaries and answer file into the user's pc, and spawn a process to run the setup in silent mode. Though you should make sure your users are admins first of course.

To Uninstall: Run setup with the uninstall option like Setup.exe /Action=Uninstall /FEATURES=SQL,AS,RS,IS,Tools /INSTANCENAME=MSSQLSERVER , see the Uninstall Parameters section. If my memory serves me well, you can actually just do Setup.exe /Action=Uninstall /INSTANCENAME=MSSQLSERVER to remove everything for the particular instance you wish to remove, but I may be wrong, so test first

I'm working on a similar requirement to automate an upgrade from SQL 2005 Express to SQL 2008 R2 Express from a WinForms application.

You don't actually have to uninstall SQL 2005 to do the upgrade. You can just upgrade straight from 2005 to 2008R2 without any uninstall required.

My code looks something like this (modified to simplify and remove proprietary stuff)...

try
{

    //First, find the version of the currently installed SQL Server Instance
    string sqlString = "SELECT SUBSTRING(CONVERT(VARCHAR, SERVERPROPERTY('productversion')), 0, 5)";
    string sqlInstanceVersion = string.Empty;                

    //_database was initialized elsewhere - it's from Enterprise Library
    using (DbCommand cmd = _database.GetSqlStringCommand(sqlString))
    {
        sqlInstanceVersion = cmd.ExecuteScalar().ToString();
    }

    if (sqlInstanceVersion.Equals(String.Empty))
    {
        //TODO throw an exception or do something else
    }

    //11.00 = SQL2012, 10.50 = SQL2008R2, 10.00 = SQL2008, 9.00 = SQL2005, 8.00 = SQL2000
    switch (sqlInstanceVersion)
    {
        case "11.00":
        case "10.50":
        case "10.00":
            //Log that the version is already up to date and return
            return;
        case "9.00":
        case "8.00":
            //We are on SQL 2000 or 2005, so continue with upgrade to 2008R2
            break;
        default:
            //TODO throw an exception for unsupported SQL Server version
            break;
    }

    string upgradeArgumentString = "/Q /ACTION=upgrade /INSTANCENAME={0} /ENU /IACCEPTSQLSERVERLICENSETERMS";
    string instanceName = "YourInstanceNameHere";
    string installerFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\SQLEXPR_x86_ENU.exe"; 

    if (!File.Exists(installerFilePath))
    {
        throw new FileNotFoundException(string.Format("Unable to find installer file: {0}", installerFilePath));
    }

    Process process = new Process
    {
        StartInfo = { FileName = installerFilePath, Arguments = String.Format(upgradeArgumentString, instanceName), UseShellExecute = false }
    };

    process.Start();

    if (process.WaitForExit(SQLSERVER_UPGRADE_TIMEOUT))
    {
        //Do something here when the process completes within timeout.
        //Probably look at exit code, or whatever to determine if it was successful
    }
    else
    {
        //The process exceeded timeout.  Do something about it; like throw exception, or whatever
    }
}
catch(Exception ex)
{
    //Handle your exceptions here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM