Look at the full version string, nut just the first 5 chars of it. A successful upgrade will change the version string.
How to tell if SQL Server automated upgrade is successful from C# WinForms app
-
01-06-2022 - |
문제
I'm automating an upgrade of SQL Server 2005 Express to SQL Server 2008R2 Express via a WinForms app that is used to upgrade our application. The application is deployed at some 800+ locations, so we don't want any manual steps.
I've got the following code mostly written to perform the upgrade. I need to know, what's best practice for determining if the SQL Server installer completed successfully? Should I just look for an exit code of 0 for the process? Is that good enough; i.e. could it still exit with code of 0 if the upgrade had a problem and was rolled back (I'd test this, but don't know the best way to simulate a failure)?
Is there any other way to determine if the upgrade was successful in my C# app, so I can handle it properly if there was any error encountered by the SQL Server Installer?
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;
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.
//What should I do here to determine if the SQL Server Installer completed successfully? Look at just the exit code?
}
else
{
//The process exceeded timeout. Do something about it; like throw exception, or whatever
}
}
catch(Exception ex)
{
//Handle your exceptions here
}
해결책