diff --git a/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs b/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs index d09eb51..1d11a5d 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs @@ -112,7 +112,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS SQLDataBase sqld = p.Parameters as SQLDataBase; try { - if (FunctionIsNotAvailable(sqld)) { return o; } + if (NoDBNameSpecified(sqld)) { return o; } SQLDataBaseManagerCore.BackupSqlData(sqld,TS); ColorConsole.WriteLine($"SQLDB data scripts created. Name:{sqld.Xml_Description}", ConsoleColor.Green); } @@ -142,7 +142,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS SQLDataBase sqld = p.Parameters as SQLDataBase; try { - if (FunctionIsNotAvailable(sqld)) { return o; } + if (NoDBNameSpecified(sqld)) { return o; } SQLDataBaseManagerCore.BackupSqlScripts(sqld,TS); ColorConsole.WriteLine($"SQLDB code scripts created. Name:{sqld.Xml_Description}", ConsoleColor.Green); } @@ -172,7 +172,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS SQLDataBase sqld = p.Parameters as SQLDataBase; try { - if (FunctionIsNotAvailable(sqld)) { return o; } + if (NoDBNameSpecified(sqld)) { return o; } SQLDataBaseManagerCore.CreateBackup(sqld,null,TS); ColorConsole.WriteLine($"Database backup created. Name:{sqld.DBName}", ConsoleColor.Green); } @@ -180,10 +180,10 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS } return o; } - private static bool FunctionIsNotAvailable(SQLDataBase ssqldb) + private static bool NoDBNameSpecified(SQLDataBase ssqldb) { const string FUNNOTAVAILTXT = "Function is not available for server connection strings wit no DB name specified!"; - if (string.IsNullOrWhiteSpace(SQLDataBaseManagerCore.GetDBName(ssqldb.SQLCS))) { ColorConsole.WriteLine(FUNNOTAVAILTXT, ConsoleColor.Red); return true; } + if (string.IsNullOrWhiteSpace(SQLDataBaseManagerCore.GetDBName(ssqldb.SQLCS))) { ColorConsole.WriteLine(FUNNOTAVAILTXT, ConsoleColor.Red); ssqldb.DBName = "master"; return true; } return false; } private static object RestoreDataBase(object parameter, object o) @@ -208,7 +208,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS SQLDataBase sqld = p.Parameters as SQLDataBase; try { - if (FunctionIsNotAvailable(sqld)) { return o; } + if (NoDBNameSpecified(sqld)) { return o; } var targetdirectorypath = sqld.Xml_PhysicalFilesDirectoryPath; var targetdbname = sqld.DBName; bool restorefromzip = sqld.Xml_CreateZip; @@ -505,7 +505,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS var p = sr.SelectedParameterList.FirstOrDefault(); SQLDataBase sqld = p.Parameters as SQLDataBase; - if (FunctionIsNotAvailable(sqld)) { return o; } + if (NoDBNameSpecified(sqld)) { return o; } getparametersloop: List DbusrToExecuteList = new List(); @@ -656,8 +656,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS var DateTimeNow = DateTime.Now; movedbanswer = YesOrNo_MoveDbToRemoteServer(config, selectedsqldbindexes); - if (FunctionIsNotAvailable(movedbanswer.Dbfrom)) { continue; } - if (FunctionIsNotAvailable(movedbanswer.Dbto)) { continue; } + if (NoDBNameSpecified(movedbanswer.Dbfrom)) { continue; } + if (NoDBNameSpecified(movedbanswer.Dbto)) { continue; } if (movedbanswer.Exit) break; if (movedbanswer.Yes) { @@ -827,7 +827,7 @@ GO var p = sr.SelectedParameterList.FirstOrDefault(); if (p == null) { return o; } SQLDataBase sqld = p.Parameters as SQLDataBase; - if (FunctionIsNotAvailable(sqld)) { return o; } + if (NoDBNameSpecified(sqld)) { return o; } getparametersloop:; @@ -1031,6 +1031,7 @@ GO ReturnInfoJSON result = null; using (var sqlc = ServerConnectionPool.GetSqlConnection(ss.SQLCS, open: true)) { + if (string.IsNullOrWhiteSpace(sqlc.Database)) { sqlc.ChangeDatabase("master"); } if (multirunmode) { result = SQLDataBaseManagerCore.ExecuteMultirunSQLScript(sqlc, ss, ExitAtThisPoint); } else { @@ -1099,7 +1100,7 @@ GO SQLDataBase sqld = p.Parameters as SQLDataBase; try { - if (FunctionIsNotAvailable(sqld)) { return o; } + if (NoDBNameSpecified(sqld)) { return o; } ColorConsole.WriteLine(prefix: $"Enter the userid/password (for the user in favour you want to drop the DB). Format:", bracket: "()", text: "[USERID[,PASSWORD]]:", f: ConsoleColor.Yellow); ColorConsole.WriteLine(prefix: " ", text: "[USERID,PASSWORD empty]", bracket: "[]", suffix: $":use windows authentication with current user", f: ConsoleColor.Yellow); ColorConsole.WriteLine(prefix: " ", text: "USERID", bracket: "[]", suffix: $":use windows authentication with this user", f: ConsoleColor.Yellow); @@ -1185,7 +1186,7 @@ GO SQLDataBase sqld = p.Parameters as SQLDataBase; try { - if (FunctionIsNotAvailable(sqld)) { return o; } + if (NoDBNameSpecified(sqld)) { return o; } ColorConsole.WriteLine(prefix: $"Enter the shrink options. Format:", bracket:"()", text:"OPTION,FreeSpacePercent:", f:ConsoleColor.Yellow); ColorConsole.WriteLine(prefix:" ", text:"Default", bracket: "[]", suffix: $": (default) compacts the data and removes free space from the DB. Use togethere with FreeSpacePercent.", f:ConsoleColor.Yellow); ColorConsole.WriteLine(prefix: " ", text: "NoTruncate",bracket:"[]", suffix: $": compacts the data, but does not remove free space from the DB. FreeSpacePercent is not applicable.", f: ConsoleColor.Yellow); @@ -1229,7 +1230,7 @@ GO SQLDataBase sqld = p.Parameters as SQLDataBase; try { - if (FunctionIsNotAvailable(sqld)) { return o; } + if (NoDBNameSpecified(sqld)) { return o; } var restoredbname = ColorConsole.ReadLine($"Enter the name of the DB to copy '{sqld.DBName}' to. Empty={sqld.DBName}. EX=exit.", ConsoleColor.Yellow, suffix: " --> "); if (restoredbname.ToUpper() == "EX") { continue; } else if (string.IsNullOrWhiteSpace(restoredbname)) { restoredbname = sqld.DBName; } @@ -1301,6 +1302,7 @@ GO } else if (lineix == 2) { + if (!st.isSQLCSselected) { return Menu.MenuItemDisplayerReturnValue.SAMELINE; } ColorConsole.Write($"{st.SQLCS}", ConsoleColor.Yellow, prefix: "SQL DB connection:", suffix: ", "); ColorConsole.WriteLine(" "); return Menu.MenuItemDisplayerReturnValue.NEXTLINE; @@ -2637,10 +2639,42 @@ GO public SQLDBStatus Status; public string PhysicalFilesDirectoryPath; public string SizeString; + public bool isSQLCSselected + { + get + { + var sqlcsList = this.Xml_SQLConnectionString.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList(); + return sqlcsList.Count() == 1 || !string.IsNullOrWhiteSpace(this.Xml_SQLConnectionStringSelected); + } + } public string SQLCS { - get { return XmlProcessing.ConnectionStringStore.GetSQL(this.Xml_SQLConnectionString); } + get + { + if (string.IsNullOrWhiteSpace(this.Xml_SQLConnectionStringSelected)) { SelectSQLConnectionString(); } + if (string.IsNullOrWhiteSpace(this.Xml_SQLConnectionStringSelected)) { return null; } + return XmlProcessing.ConnectionStringStore.GetSQL(this.Xml_SQLConnectionStringSelected); + } } + private string Xml_SQLConnectionStringSelected; + + public void SelectSQLConnectionString(int? forceselection=null) + { + this.Xml_SQLConnectionStringSelected = null; + var sqlcsList = this.Xml_SQLConnectionString.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList(); + if (forceselection.HasValue) + { + if (forceselection.Value >=0 && sqlcsList.Count() > forceselection.Value) { this.Xml_SQLConnectionStringSelected = sqlcsList[forceselection.Value]; } + return; + } + else + { + if (sqlcsList.Count() == 1) { this.Xml_SQLConnectionStringSelected = sqlcsList[0]; return; } + var selection = ColorConsole.ReadLine("Select SQL connection string to use:", bracket: "[]", suffix: "ENTER==first", validitylist: sqlcsList); + if (selection == "EX") return; + if (string.IsNullOrWhiteSpace(selection)) { this.Xml_SQLConnectionStringSelected = sqlcsList[0]; } else { this.Xml_SQLConnectionStringSelected = selection; } + } + } #endregion fields public enum SQLDBStatus { NoAccess, OK, } @@ -2649,29 +2683,44 @@ GO { try { - string dbname = SQLDataBaseManagerCore.GetDBName(this.SQLCS); - if (string.IsNullOrWhiteSpace(dbname)) + if (!this.isSQLCSselected) { - var sqlc = ServerConnectionPool.GetSqlConnection(this.SQLCS, open: true); - this.Status = SQLDBStatus.OK; - this.DBName = ""; - this.DataSource = SQLDataBaseManagerCore.GetDataSource(this.SQLCS); - } - else + this.Status = SQLDBStatus.OK; + this.DBName = ""; + this.DataSource = this.Xml_SQLConnectionString; + } + else { - GetPhysicalFilesLocationAndSize(this.SQLCS, out this.SizeString, out this.PhysicalFilesDirectoryPath); - this.Status = this.PhysicalFilesDirectoryPath.StartsWith("ERR") ? SQLDBStatus.NoAccess : SQLDBStatus.OK; - this.DBName = SQLDataBaseManagerCore.GetDBName(this.SQLCS); - this.DataSource = SQLDataBaseManagerCore.GetDataSource(this.SQLCS); - } + string dbname = SQLDataBaseManagerCore.GetDBName(this.SQLCS); + if (string.IsNullOrWhiteSpace(dbname)) + { + var sqlc = ServerConnectionPool.GetSqlConnection(this.SQLCS, open: true); + this.Status = SQLDBStatus.OK; + this.DBName = ""; + this.DataSource = SQLDataBaseManagerCore.GetDataSource(this.SQLCS); + } + else + { + GetPhysicalFilesLocationAndSize(this.SQLCS, out this.SizeString, out this.PhysicalFilesDirectoryPath); + this.Status = this.PhysicalFilesDirectoryPath.StartsWith("ERR") ? SQLDBStatus.NoAccess : SQLDBStatus.OK; + this.DBName = SQLDataBaseManagerCore.GetDBName(this.SQLCS); + this.DataSource = SQLDataBaseManagerCore.GetDataSource(this.SQLCS); + } + } } catch { this.Status = SQLDBStatus.NoAccess; this.PhysicalFilesDirectoryPath = ""; - this.DBName = SQLDataBaseManagerCore.GetDBName(this.SQLCS); - this.DataSource = SQLDataBaseManagerCore.GetDataSource(this.SQLCS); - this.SizeString = ""; + this.DBName = ""; + this.DataSource = ""; + this.SizeString = ""; + try + { + this.DBName = SQLDataBaseManagerCore.GetDBName(this.SQLCS); + this.DataSource = SQLDataBaseManagerCore.GetDataSource(this.SQLCS); + } + catch { } } return this; } @@ -2909,9 +2958,41 @@ GO public string ScriptParameters=null; public string MultirunParameters=null; public string SQLConnectionString=null; + private string SQLConnectionStringSelected=null; + public bool isSQLCSselected + { + get + { + var sqlcsList = this.SQLConnectionString.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList(); + return sqlcsList.Count() == 1 || !string.IsNullOrWhiteSpace(this.SQLConnectionStringSelected); + } + } public string SQLCS { - get { return XmlProcessing.ConnectionStringStore.GetSQL(this.SQLConnectionString); } + get + { + if (string.IsNullOrWhiteSpace(this.SQLConnectionStringSelected)) { SelectSQLConnectionString(); } + if (string.IsNullOrWhiteSpace(this.SQLConnectionStringSelected)) { return null; } + return XmlProcessing.ConnectionStringStore.GetSQL(this.SQLConnectionStringSelected); + } + } + + public void SelectSQLConnectionString(int? forceselection = null) + { + this.SQLConnectionStringSelected = null; + var sqlcsList = this.SQLConnectionString.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList(); + if (forceselection.HasValue) + { + if (forceselection.Value >= 0 && sqlcsList.Count() > forceselection.Value) { this.SQLConnectionStringSelected = sqlcsList[forceselection.Value]; } + return; + } + else + { + if (sqlcsList.Count() == 1) { this.SQLConnectionStringSelected = sqlcsList[0]; return; } + var selection = ColorConsole.ReadLine("Select SQL connection string to use:", bracket: "[]", suffix: "[ENTER]==first", validitylist: sqlcsList); + if (selection == "EX") return; + if (string.IsNullOrWhiteSpace(selection)) { this.SQLConnectionStringSelected = sqlcsList[0]; } else { this.SQLConnectionStringSelected = selection; } + } } public SQLScript() { } @@ -3066,7 +3147,8 @@ GO { var scb = new SqlConnectionStringBuilder(sqlconnectionstring); if (!string.IsNullOrWhiteSpace(dbname)) { scb.InitialCatalog = dbname; } - if (!string.IsNullOrWhiteSpace(userid) && !string.IsNullOrWhiteSpace(userpassword)) { scb.UserID = userid; scb.Password = userpassword; } + if (string.IsNullOrWhiteSpace(scb.InitialCatalog)) { scb.InitialCatalog = "master"; scb.AttachDBFilename = ""; } + if (!string.IsNullOrWhiteSpace(userid) && !string.IsNullOrWhiteSpace(userpassword)) { scb.UserID = userid; scb.Password = userpassword; } else if (!string.IsNullOrWhiteSpace(userid) && string.IsNullOrWhiteSpace(userpassword)) { scb.UserID = userid; scb.IntegratedSecurity = true; } var sqlc = new SqlConnection(scb.ToString()); if (open) Open(sqlc); @@ -3075,15 +3157,12 @@ GO public static void Open(SqlConnection sqlc) { if (IsMasterInacessible(sqlc) || IsInacessible(sqlc)) { throw new Exception($"SQL server {sqlc.DataSource} is inaccessible!"); } - if (!string.IsNullOrWhiteSpace(sqlc.Database)) + try { sqlc.Open(); } + catch (Exception ex) { - try { sqlc.Open(); } - catch (Exception ex) - { - MarkMasterInacessible(sqlc); - MarkInacessible(sqlc); - throw ex; - } + MarkMasterInacessible(sqlc); + MarkInacessible(sqlc); + throw ex; } } private static bool IsMasterDBConnected(SqlConnection sqlc) diff --git a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs index eca01a1..c4de42c 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.31.3.0")] -[assembly: AssemblyFileVersion("1.31.3.0")] +[assembly: AssemblyVersion("1.31.4.0")] +[assembly: AssemblyFileVersion("1.31.4.0")] -- libgit2 0.21.2