public static ActionResult ExecuteSql(Session session) { try { var valide = TestSqlConnection(session); if (valide == ActionResult.Success) { var groupConfig = new GroupConfiguration { Database = GetSessionProperty(session, "DATABASE_NAME", false), IsWinAuthentification = GetSessionProperty(session, "DATABASE_WINDOWSAUTHENTICATION", false) == "1", Password = GetSessionProperty(session, "DATABASE_PASSWORD", false).Trim(), Server = GetSessionProperty(session, "DATABASE_SERVERNAME", false).Trim(), User = GetSessionProperty(session, "DATABASE_USERNAME", false).Trim() }; SetSessionProperty(session, "WINDOWSAUTHENTICATION", groupConfig.IsWinAuthentification + ""); var ctx = new GroupContextFactory(groupConfig); var exist = ctx.DatabaseExists(); try { // create data base with entity framework if (!exist) { ctx.DatabaseInitialize(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { // MessageBox.Show(exist.ToString()); // Save(groupConfig); } // MessageBox.Show("00"); } return(ActionResult.Success); } catch (Exception ex) { MessageBox.Show(ex.Message); return(ActionResult.Failure); } }
public static void Save(GroupConfiguration configs) { try { if (configs == null) { throw new ArgumentNullException("config"); } string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); string configFile = System.IO.Path.Combine(appPath, "DeclarationPlus.UIWin.exe.config"); MessageBox.Show(configFile); var configFileMap = new ExeConfigurationFileMap(); configFileMap.ExeConfigFilename = configFile; // System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None); // load the config file var fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = "DeclarationPlus.UIWin.exe.config"; // fileMap.ExeConfigFilename = "appconfig"; var config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None); MessageBox.Show(configs.GetConnection()); // get the connection string section var connectionStringsSection = config.GetSection("connectionStrings") as ConnectionStringsSection; if (connectionStringsSection == null) { throw new InvalidOperationException("Operation invalide! [config file section]"); } MessageBox.Show(connectionStringsSection.ConnectionStrings.ToString()); // get the connection string setting. var cnxSetting = connectionStringsSection.ConnectionStrings["Connection"]; // modify the configuration if (cnxSetting == null) { // add the connection string if it doesn't already exist cnxSetting = new ConnectionStringSettings( "Connection", configs.GetConnection()) { ProviderName = "System.Data.SqlClient" }; connectionStringsSection.ConnectionStrings.Add(cnxSetting); MessageBox.Show(connectionStringsSection.CurrentConfiguration.FilePath); } // save changes if (!connectionStringsSection.SectionInformation.IsProtected) { connectionStringsSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); connectionStringsSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }