internal void RunScripts() { // récupération chaine de connection string connectionString = null; if (!string.IsNullOrEmpty(CsFile)) { // recuperation connectionString depuis le fichier var doc = XDocument.Load(CsFile); foreach (var el in doc.Root.Elements()) { if (el.Name != "add") { continue; } var name = el.Attribute("name"); if (name?.Value != ConnectionStringCode) { continue; } connectionString = el.Attribute("connectionString")?.Value; break; } if (string.IsNullOrEmpty(connectionString)) { Console.WriteLine("Error: No connection string " + ConnectionStringCode + " found in file " + CsFile); return; } } else { if (ConfigurationManager.ConnectionStrings.Count == 0) { Console.WriteLine("Error: No connection string have been found in Config/Database.config"); return; } connectionString = ConfigurationManager.ConnectionStrings[ConnectionStringCode].ConnectionString; } if (connectionString.Contains("provider connection string=\"")) { connectionString = connectionString.Split(new[] { "\"" }, StringSplitOptions.None)[1]; } Console.WriteLine("Using the following connection string: "); Console.WriteLine(connectionString); // list scripts files to be run, with the correct order List <string> listFichiers = new List <string>(); if (!string.IsNullOrEmpty(SqlPath)) { try { listFichiers = ScriptDetector.FindFilesInDirectory(SqlPath); } catch (Exception ex) { LogHelper.LogAndInfo("ERROR : " + ex.FormatForLog()); return; } } else if (!string.IsNullOrEmpty(SqlFile)) { listFichiers.Add(SqlFile); } if (listFichiers == null || !listFichiers.Any(x => !string.IsNullOrEmpty(x))) { LogHelper.LogAndInfo("ERROR: No file to run"); return; } #region run scripts and log execution int nbScript = 0, nbErreurs = 0, nbSucces = 0; foreach (string fichier in listFichiers) { nbScript++; LogHelper.LogAndInfo("RUN SCRIPT " + fichier); string messageErreur = string.Empty; try { string scriptContent = FileHelper.GetFileContent(fichier); using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); var scripts = SplitSqlStatements(scriptContent); foreach (var script in scripts) { nbScript++; var command = new SqlCommand(script, connection) { CommandTimeout = 172800 // 48 heures }; command.ExecuteNonQuery(); } } nbSucces++; } catch (Exception ex) { messageErreur = ex.FormatForLog(); LogHelper.LogAndInfo("ERROR running script " + fichier + " " + messageErreur); nbErreurs++; } } LogHelper.LogAndInfo(string.Format("FINISHED : {0} scripts run, {1} success and {2} errors", nbScript, nbSucces, nbErreurs)); #endregion }