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. namespace SqlExp
  8. {
  9. public class SqlExpInstall
  10. {
  11. #region Internal variables
  12. //Variables for setup.exe command line
  13. private string instanceAction = "Install"; // Required
  14. private string instanceFeature = "SQL,Tools"; // Required
  15. public string instanceName = "SQLEXPRESS"; // Required
  16. public string installSqlDir = "C:\\Program Files\\"; // Optional
  17. public string installSqlSharedDir = "C:\\Program Files\\"; // Optional
  18. public string installSqlDataDir = "C:\\Program Files\\Microsoft SQL Server\\"; // Optional
  19. public bool sqlAutoStart = true; // Optional
  20. public bool sqlBrowserAutoStart = false; // Optional
  21. public string sqlBrowserAccount = "NT AUTHORITY\\SYSTEM"; // Optional
  22. public string sqlBrowserPassword = ""; // Optional
  23. public string sqlAccount = "NT AUTHORITY\\SYSTEM"; // Required
  24. public string sqlPassword = "testdemo"; // Required
  25. public bool sqlSecurityMode = true; // Optional
  26. public string saPassword = "tetdemo123"; // Required when SECURITYMODE=SQL
  27. public string sqlCollation = "SQL_Latin1_General_Cp1_CS_AS"; // Optional
  28. public bool disableNetworkProtocols = true; // Optional
  29. public bool errorReporting = true; // Optional
  30. public string sqlExpressSetupFileLocation = Path.Combine(@"C:\Downloads", "sqlexpr.exe"); // Required
  31. public bool enableRANU = false; // Optional
  32. public string sysAdminAccount = "Builtin\\Administrators"; // Required
  33. public string agtSqlAccount = "NT AUTHORITY\\Network Service"; // Required
  34. public bool sqlServiceLicence = true; // Required
  35. #endregion
  36. private string BuildCommandLine()
  37. {
  38. StringBuilder strCommandLine = new StringBuilder();
  39. if (!IsNullOrEmpty(instanceAction))
  40. {
  41. strCommandLine.Append(" ACTION=\"").Append(instanceAction).Append("\"");
  42. }
  43. if (!IsNullOrEmpty(instanceFeature))
  44. {
  45. strCommandLine.Append(" FEATURES=\"").Append(instanceFeature).Append("\"");
  46. }
  47. if (!IsNullOrEmpty(installSqlDir))
  48. {
  49. strCommandLine.Append(" INSTANCENAME=\"").Append(instanceName).Append("\"");
  50. }
  51. if (!IsNullOrEmpty(sqlAccount))
  52. {
  53. strCommandLine.Append(" SQLSVCACCOUNT=\"").Append(sqlAccount).Append("\"");
  54. }
  55. if (!IsNullOrEmpty(sqlPassword))
  56. {
  57. strCommandLine.Append(" SQLSVCPASSWORD=\"").Append(sqlPassword).Append("\"");
  58. }
  59. if (!IsNullOrEmpty(sysAdminAccount))
  60. {
  61. strCommandLine.Append(" SQLSYSADMINACCOUNTS=\"").Append(sysAdminAccount).Append("\"");
  62. }
  63. if (!IsNullOrEmpty(agtSqlAccount))
  64. {
  65. strCommandLine.Append(" AGTSVCACCOUNT=\"").Append(agtSqlAccount).Append("\"");
  66. }
  67. if (sqlSecurityMode == true)
  68. {
  69. strCommandLine.Append(" SECURITYMODE=SQL");
  70. }
  71. if (!IsNullOrEmpty(saPassword))
  72. {
  73. strCommandLine.Append(" SAPWD=\"").Append(saPassword).Append("\"");
  74. }
  75. if (!IsNullOrEmpty(sqlCollation))
  76. {
  77. strCommandLine.Append(" SQLCOLLATION=\"").Append(sqlCollation).Append("\"");
  78. }
  79. if (errorReporting == true)
  80. {
  81. strCommandLine.Append(" ERRORREPORTING=1");
  82. }
  83. else
  84. {
  85. strCommandLine.Append(" ERRORREPORTING=0");
  86. }
  87. if (enableRANU == true)
  88. {
  89. strCommandLine.Append(" ENABLERANU=1");
  90. }
  91. else
  92. {
  93. strCommandLine.Append(" ENABLERANU=0");
  94. }
  95. if (sqlServiceLicence == true)
  96. {
  97. strCommandLine.Append(" IACCEPTSQLSERVERLICENSETERMS=1");
  98. }
  99. else
  100. {
  101. strCommandLine.Append(" IACCEPTSQLSERVERLICENSETERMS=0");
  102. }
  103. return strCommandLine.ToString();
  104. }
  105. public int InstallExpress()
  106. {
  107. Process myProcess = new Process();
  108. myProcess.StartInfo.FileName = sqlExpressSetupFileLocation;
  109. myProcess.StartInfo.Arguments = "/q" + BuildCommandLine();
  110. /* /q -- Specifies that setup run with no user interface. */
  111. myProcess.StartInfo.UseShellExecute = false;
  112. myProcess.Start();
  113. return myProcess.Id;
  114. }
  115. private static bool IsNullOrEmpty(string str)
  116. {
  117. return (str == null) || (str == string.Empty);
  118. }
  119. }
  120. }
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. namespace SqlExp
  6. {
  7. static class Program
  8. {
  9. /// <summary>
  10. /// The main entry point for the application.
  11. /// </summary>
  12. [STAThread]
  13. static void Main()
  14. {
  15. InstallSqlEngine()
  16. }
  17. private static void InstallSqlEngine()
  18. {
  19. SqlExpInstall EI = new SqlExpInstall();
  20. EI.sqlExpressSetupFileLocation = Path.Combine(@"C:\Downloads", "sqlexpr.exe"); //Provide location for the Express setup file
  21. int pid = EI.InstallExpress();
  22. // now let's wait till the setup is complete
  23. Process installp = Process.GetProcessById(pid);
  24. if (installp != null)
  25. installp.WaitForExit();
  26. int cc = installp.ExitCode;
  27. }
  28. }
  29. }
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