How To Install SQL Server 2008 R2 Silently From C# Application

Installing a new instance of SQL Server at the command prompt enables you to specify the features to install and how they should be configured. You can also specify silent, basic, or full interaction with the setup of the user interface. Here, I am using the "silent" type of interaction.
 
When you install SQL Server at the command prompt, specify the setup parameters for your installation at the command prompt as part of your installation syntax.
  1. using System;  
  2. using System.IO;  
  3. using System.Text;  
  4. using Microsoft.Win32;  
  5. using System.Diagnostics;  
  6. using System.Windows.Forms;  
  7.   
  8. namespace SqlExp
  9. {  
  10.     public class SqlExpInstall  
  11.     {  
  12.         #region Internal variables  
  13.   
  14.         //Variables for setup.exe command line  
  15.         private string instanceAction = "Install";                                                          // Required  
  16.         private string instanceFeature = "SQL,Tools";                                                       // Required  
  17.         public string instanceName = "SQLEXPRESS";                                                          // Required  
  18.         public string installSqlDir = "C:\\Program Files\\";                                                // Optional  
  19.         public string installSqlSharedDir = "C:\\Program Files\\";                                          // Optional  
  20.         public string installSqlDataDir = "C:\\Program Files\\Microsoft SQL Server\\";                      // Optional  
  21.         public bool sqlAutoStart = true;                                                                    // Optional  
  22.         public bool sqlBrowserAutoStart = false;                                                            // Optional  
  23.         public string sqlBrowserAccount = "NT AUTHORITY\\SYSTEM";                                           // Optional  
  24.         public string sqlBrowserPassword = "";                                                              // Optional  
  25.         public string sqlAccount = "NT AUTHORITY\\SYSTEM";                                                  // Required  
  26.         public string sqlPassword = "testdemo";                                                             // Required  
  27.         public bool sqlSecurityMode = true;                                                                 // Optional  
  28.         public string saPassword = "tetdemo123";                                                           // Required when SECURITYMODE=SQL  
  29.         public string sqlCollation = "SQL_Latin1_General_Cp1_CS_AS";                                        // Optional  
  30.         public bool disableNetworkProtocols = true;                                                         // Optional  
  31.         public bool errorReporting = true;                                                                  // Optional  
  32.         public string sqlExpressSetupFileLocation = Path.Combine(@"C:\Downloads", "sqlexpr.exe");   // Required  
  33.         public bool enableRANU = false;                                                                     // Optional  
  34.         public string sysAdminAccount = "Builtin\\Administrators";                                          // Required  
  35.         public string agtSqlAccount = "NT AUTHORITY\\Network Service";                                      // Required  
  36.         public bool sqlServiceLicence = true;                                                               // Required  
  37.   
  38.         #endregion       
  39.   
  40.         private string BuildCommandLine()  
  41.         {  
  42.             StringBuilder strCommandLine = new StringBuilder();  
  43.   
  44.             if (!IsNullOrEmpty(instanceAction))  
  45.             {  
  46.                 strCommandLine.Append(" ACTION=\"").Append(instanceAction).Append("\"");  
  47.             }  
  48.   
  49.             if (!IsNullOrEmpty(instanceFeature))  
  50.             {  
  51.                 strCommandLine.Append(" FEATURES=\"").Append(instanceFeature).Append("\"");  
  52.             }  
  53.   
  54.             if (!IsNullOrEmpty(installSqlDir))  
  55.             {  
  56.                 strCommandLine.Append(" INSTANCENAME=\"").Append(instanceName).Append("\"");  
  57.             }       
  58.   
  59.             if (!IsNullOrEmpty(sqlAccount))  
  60.             {  
  61.                 strCommandLine.Append(" SQLSVCACCOUNT=\"").Append(sqlAccount).Append("\"");  
  62.             }  
  63.   
  64.             if (!IsNullOrEmpty(sqlPassword))  
  65.             {  
  66.                 strCommandLine.Append(" SQLSVCPASSWORD=\"").Append(sqlPassword).Append("\"");  
  67.             }  
  68.   
  69.             if (!IsNullOrEmpty(sysAdminAccount))  
  70.             {  
  71.                 strCommandLine.Append(" SQLSYSADMINACCOUNTS=\"").Append(sysAdminAccount).Append("\"");  
  72.             }  
  73.   
  74.             if (!IsNullOrEmpty(agtSqlAccount))  
  75.             {  
  76.                 strCommandLine.Append(" AGTSVCACCOUNT=\"").Append(agtSqlAccount).Append("\"");  
  77.             }  
  78.   
  79.             if (sqlSecurityMode == true)  
  80.             {  
  81.                 strCommandLine.Append(" SECURITYMODE=SQL");  
  82.             }  
  83.   
  84.             if (!IsNullOrEmpty(saPassword))  
  85.             {  
  86.                 strCommandLine.Append(" SAPWD=\"").Append(saPassword).Append("\"");  
  87.             }  
  88.   
  89.             if (!IsNullOrEmpty(sqlCollation))  
  90.             {  
  91.                 strCommandLine.Append(" SQLCOLLATION=\"").Append(sqlCollation).Append("\"");  
  92.             }  
  93.   
  94.             if (errorReporting == true)  
  95.             {  
  96.                 strCommandLine.Append(" ERRORREPORTING=1");  
  97.             }  
  98.             else  
  99.             {  
  100.                 strCommandLine.Append(" ERRORREPORTING=0");  
  101.             }  
  102.   
  103.             if (enableRANU == true)  
  104.             {  
  105.                 strCommandLine.Append(" ENABLERANU=1");  
  106.             }  
  107.             else  
  108.             {  
  109.                 strCommandLine.Append(" ENABLERANU=0");  
  110.             }  
  111.   
  112.             if (sqlServiceLicence == true)  
  113.             {  
  114.                 strCommandLine.Append(" IACCEPTSQLSERVERLICENSETERMS=1");  
  115.             }  
  116.             else  
  117.             {  
  118.                 strCommandLine.Append(" IACCEPTSQLSERVERLICENSETERMS=0");  
  119.             }  
  120.   
  121.             return strCommandLine.ToString();  
  122.     }  
  123.   
  124.         public int InstallExpress()  
  125.         {  
  126.             Process myProcess = new Process();  
  127.             myProcess.StartInfo.FileName = sqlExpressSetupFileLocation;  
  128.             myProcess.StartInfo.Arguments = "/q" + BuildCommandLine();  
  129.             /*  /q -- Specifies that setup run with no user interface. */  
  130.             myProcess.StartInfo.UseShellExecute = false;  
  131.             myProcess.Start();  
  132.             return myProcess.Id;  
  133.         }  
  134.   
  135.         private static bool IsNullOrEmpty(string str)  
  136.         {  
  137.             return (str == null) || (str == string.Empty);  
  138.         }  
  139.     }  

Now, call the SqlExpInstall class to install the SQL Server.
  1. using System;  
  2. using System.IO;  
  3. using System.Diagnostics;  
  4. using System.Windows.Forms;  
  5.   
  6. namespace SqlExp  
  7. {  
  8.     static class Program  
  9.     {  
  10.         /// <summary>  
  11.         /// The main entry point for the application.  
  12.         /// </summary>  
  13.         [STAThread]  
  14.         static void Main()  
  15.         {  
  16.             InstallSqlEngine()  
  17.         }  
  18.   
  19.         private static void InstallSqlEngine()  
  20.         {
  21.             SqlExpInstall EI = new SqlExpInstall();              
  22.             EI.sqlExpressSetupFileLocation = Path.Combine(@"C:\Downloads""sqlexpr.exe");  //Provide location for the Express setup file              
  23.             int pid = EI.InstallExpress();  
  24.   
  25.             // now let's wait till the setup is complete  
  26.             Process installp = Process.GetProcessById(pid);  
  27.             if (installp != null)  
  28.                 installp.WaitForExit();  
  29.             int cc = installp.ExitCode;  
  30.         }  
  31.     }  

I hope this is useful to all the readers. Happy Coding!
 
Reference - https://social.technet.microsoft.com/wiki/contents/articles/940.how-to-embed-sql-server-express-in-an-application.aspx