public void TestMethod1() { //System.Configuration.Configuration configSection = ConfigurationManager.OpenExeConfiguration(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MvcInstallerV3.UnitTests.dll.config")); Configuration configSection = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //Configuration configSection = WebConfigurationManager.OpenWebConfiguration(null); if (configSection == null) { throw new InvalidOperationException("Configuration file not available."); } InstallerConfig config = Serializer<InstallerConfig>.Deserialize(AppDomain.CurrentDomain.BaseDirectory + @"\installer.config"); IConnectionStringComponent component = new ConnectionStringComponent(config); IConfigurationFactory factory = new ConfigurationFactory(component); factory.Execute(config, configSection); Console.WriteLine("Before save..."); var result = configSection.ConnectionStrings.ConnectionStrings[0].ConnectionString; Console.WriteLine(result); configSection.Save(); Console.WriteLine("After save..."); result = configSection.ConnectionStrings.ConnectionStrings[0].ConnectionString; Console.WriteLine(result); string connString = configSection.ConnectionStrings.ConnectionStrings[0].ConnectionString; Console.WriteLine(connString); }
/// <summary> /// This will execute the sql statements. /// </summary> /// <param name="tableStatements"></param> /// <param name="dbName"></param> private static void ExecuteStatements(string[] tableStatements, InstallerConfig config) { if (tableStatements.Length > 0) { using (SqlConnection conn = new SqlConnection()) { IConnectionStringComponent component = new ConnectionStringComponent(config); conn.ConnectionString = component.GetConnString(); conn.Open(); using (SqlCommand command = new SqlCommand(string.Empty, conn)) { foreach (string statement in tableStatements) { command.CommandText = statement; command.ExecuteNonQuery(); } } } } }
/// <summary> /// This will execute the sql statements. /// </summary> /// <param name="tableStatements"></param> /// <param name="dbName"></param> private void ExecuteStatements(string[] tableStatements, InstallerConfig config) { if (tableStatements.Length > 0) { using (SqlConnection conn = new SqlConnection()) { IConnectionStringComponent component = new ConnectionStringComponent(config); conn.ConnectionString = component.GetConnString(); conn.Open(); using (SqlCommand command = new SqlCommand(string.Empty, conn)) { foreach (string statement in tableStatements) { command.CommandText = statement; command.ExecuteNonQuery(); } } } } }
/// <summary> /// Write the new connection string to the web.config file and update the membership sections if necessary. /// </summary> private static void UpdateWebConfig(InstallerConfig config) { System.Configuration.Configuration configSection = WebConfigurationManager.OpenWebConfiguration("~"); if (configSection == null) { throw new InvalidOperationException("Configuration file not available."); } IConnectionStringComponent component = new ConnectionStringComponent(config); IConfigurationFactory factory = new ConfigurationFactory(component); factory.Execute(config, configSection); // You can uncomment this if you want to add a LocalSqlServer connection. // I am connecting to the ASPNETDB tables with the same connection string since they are both in the same database. // If you create a separate ASPNETDB database, then you can uncomment the following and set the connection string manually. //ConnectionStringSettings LocalSqlServer = new ConnectionStringSettings("LocalSqlServer", component.GetConnString(), "System.Data.SqlClient"); //ConnectionStringsSection connSection = configSection.ConnectionStrings; //connSection.ConnectionStrings.Add(LocalSqlServer); configSection.Save(); }
/// <summary> /// This will execute the sql statements. /// </summary> /// <param name="tableStatements"></param> /// <param name="dbName"></param> private static void ExecuteStatements(string[] tableStatements, InstallerConfig config) { if (tableStatements.Length > 0) { using (SqlConnection conn = new SqlConnection()) { // Make sure we're not returning the EntityFramework connection string. config.Database.EntityFrameworkEntitiesName = ""; IConnectionStringComponent component = new ConnectionStringComponent(config); conn.ConnectionString = component.GetConnString(); conn.Open(); using (SqlCommand command = new SqlCommand(string.Empty, conn)) { foreach (string statement in tableStatements) { command.CommandText = statement; command.ExecuteNonQuery(); } } } } }
/// <summary> /// Write the new connection string to the web.config file and update the membership sections if necessary. /// </summary> private static void UpdateWebConfig(InstallerConfig config) { System.Configuration.Configuration configSection = WebConfigurationManager.OpenWebConfiguration("~"); if (configSection == null) { throw new InvalidOperationException("Configuration file not available."); } IConnectionStringComponent component = new ConnectionStringComponent(config); IConfigurationFactory factory = new ConfigurationFactory(component); factory.Execute(config, configSection); // You can uncomment this if you want to add a LocalSqlServer connection. // I am connecting to the ASPNETDB tables with the same connection string since they are both in the same database. // If you create a separate ASPNETDB database, then you can uncomment the following and set the connection string manually. //ConnectionStringSettings LocalSqlServer = new ConnectionStringSettings("LocalSqlServer", component.GetConnString(), "System.Data.SqlClient"); //ConnectionStringsSection connSection = configSection.ConnectionStrings; //connSection.ConnectionStrings.Add(LocalSqlServer); configSection.Save(); Fix(); }