Commit eeb190f8d8889739a5ae33d96cd3347f9c6ca019

Authored by Schwirg László
1 parent eca09089

v1.27.2.0

- DB lista előállításának gyorsítása
Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs
... ... @@ -38,6 +38,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS
38 38 #region Execute
39 39 public static object Execute(object o1 = null, object o2 = null)
40 40 {
  41 + ServerConnectionPool.Init();
41 42 var args = (o1 as Menu.ExecutorParameter).Args;
42 43 var functionkey = CommandLine.GetCommandLineArgument(args, CLP.CMD_FUNCTION);
43 44  
... ... @@ -910,7 +911,7 @@ GO
910 911 menufct.ClearMenuItemList();
911 912 foreach (var def in defList)
912 913 {
913   - var st = SQLDataBaseManagerCore.CollectDataBaseInfo(def);
  914 + var st = def.CollectInfo();
914 915 menufct.AddMenuItem(new Menu.Item(def.Xml_Key, null, null, new Menu.ExecutorParameter(pars: st)));
915 916 }
916 917 if (!silent) { menufct.DisplayItems(1); }
... ... @@ -921,14 +922,15 @@ GO
921 922 private static object DisplayDataBaseInfo(object obj, int lineix)
922 923 {
923 924 SQLDataBase st = (obj as Menu.ExecutorParameter).Parameters as SQLDataBase;
  925 + //SQLDataBaseManagerCore.GetPhysicalFilesLocationAndSize(st.SQLCS,out string dbsizetext,out string dbpathtext);
924 926 if (lineix == 0)
925 927 {
926 928 ColorConsole.Write($"{st.Xml_Description}", ConsoleColor.Black, ConsoleColor.White);
927   - var statuscolor = st.Status == SQLDataBaseManagerCore.SQLDBStatus.NoAccess ? ConsoleColor.Red:ConsoleColor.Green;
  929 + var statuscolor = st.Status == SQLDataBase.SQLDBStatus.NoAccess ? ConsoleColor.Red:ConsoleColor.Green;
928 930 ColorConsole.Write(st.Status.ToString(), statuscolor, bracket: "[]", prefix: " ", suffix: ". ");
929 931 ColorConsole.Write(st.DBName, statuscolor, bracket: "[]", prefix: "Database ", suffix: ". ");
930 932 ColorConsole.Write(st.DataSource, statuscolor, bracket: "[]", prefix: "from server ", suffix: ". ");
931   - if (st.Status != SQLDataBaseManagerCore.SQLDBStatus.NoAccess)
  933 + if (st.Status != SQLDataBase.SQLDBStatus.NoAccess)
932 934 {
933 935 }
934 936 if (st.Xml_IsRemoteDB) { ColorConsole.Write("REMOTE", ConsoleColor.Cyan, bracket: "[]", prefix: "", suffix: ""); }
... ... @@ -949,15 +951,13 @@ GO
949 951 }
950 952 else if (lineix == 3)
951 953 {
952   - var phypath = (SQLDataBaseManagerCore.GetPhysicalFilesLocation(st.SQLCS))??"ERR";
953   - ColorConsole.Write($"{phypath}", ConsoleColor.Yellow, prefix: "DB files physical location:", suffix: " ");
  954 + ColorConsole.Write($"{st.PhysicalFilesDirectoryPath}", ConsoleColor.Yellow, prefix: "DB files physical location:", suffix: " ");
954 955 ColorConsole.WriteLine(" ");
955 956 return " ";
956 957 }
957 958 else if (lineix == 4)
958 959 {
959   - var dbsize = SQLDataBaseManagerCore.GetSize(st.SQLCS);
960   - ColorConsole.Write(dbsize, ConsoleColor.Yellow, prefix: "DB size:", suffix: " ");
  960 + ColorConsole.Write(st.SizeString, ConsoleColor.Yellow, prefix: "DB size:", suffix: " ");
961 961 ColorConsole.WriteLine(" ");
962 962 return " ";
963 963 }
... ... @@ -980,29 +980,6 @@ GO
980 980 #region class SQLDataBaseManager
981 981 public static class SQLDataBaseManagerCore
982 982 {
983   - #region public CollectDataBaseInfo
984   - public static SQLDataBase CollectDataBaseInfo(SQLDataBase sqld)
985   - {
986   - try
987   - {
988   - sqld.PhysicalFilesDirectoryPath = (GetPhysicalFilesLocation(sqld.SQLCS)) ?? "";
989   - sqld.Status = string.IsNullOrWhiteSpace(sqld.PhysicalFilesDirectoryPath)? SQLDBStatus.NoAccess: SQLDBStatus.OK;
990   - sqld.DBName = GetDBName(sqld.SQLCS);
991   - sqld.DataSource = GetDataSource(sqld.SQLCS);
992   - sqld.SizeString = GetSize(sqld.SQLCS);
993   - }
994   - catch
995   - {
996   - sqld.Status = SQLDBStatus.NoAccess;
997   - sqld.PhysicalFilesDirectoryPath = "";
998   - sqld.DBName = GetDBName(sqld.SQLCS);
999   - sqld.DataSource = GetDataSource(sqld.SQLCS);
1000   - sqld.SizeString = "";
1001   - }
1002   - return sqld;
1003   - }
1004   - #endregion public CollectDataBaseInfo
1005   -
1006 983 public static void RelocatePhysicalFiles(SQLDataBase sqld, string targetdirectory, string restoredbname)
1007 984 {
1008 985 var dbbackupfilepath = CreateBackup(sqld, false,null);
... ... @@ -1055,7 +1032,7 @@ GO
1055 1032 Server sqlserver = null;
1056 1033 try
1057 1034 {
1058   - var sc = GetSqlConnection(sqlcs, "master");
  1035 + var sc = ServerConnectionPool.GetSqlConnection(sqlcs, "master");
1059 1036 sqlserver = SQLServerConnect(sc);if (sqlserver == null) { return; }
1060 1037  
1061 1038 var smoRestore = new Restore();
... ... @@ -1261,9 +1238,8 @@ GO
1261 1238 }
1262 1239 public static void CreateUser(string sqldbconnectionString, string dbusername, string dbrolenamecommalist,string loginname)
1263 1240 {
1264   - using (SqlConnection connection = new SqlConnection(sqldbconnectionString))
  1241 + using (SqlConnection connection = ServerConnectionPool.GetSqlConnection(sqldbconnectionString,open:true))
1265 1242 {
1266   - connection.Open();
1267 1243 // Create user
1268 1244 //USE [LearALM2]
1269 1245 //DROP USER[datareader]
... ... @@ -1351,7 +1327,7 @@ GO
1351 1327 Server sqlserver = null;
1352 1328 try
1353 1329 {
1354   - var sc = GetSqlConnection(sqldbconnectionString,null, grantoruser, grantoruserpsw);
  1330 + var sc = ServerConnectionPool.GetSqlConnection(sqldbconnectionString,null, grantoruser, grantoruserpsw);
1355 1331 sqlserver = SQLServerConnect(sc); if (sqlserver == null) return;
1356 1332 Login login = new Login(sqlserver, name);
1357 1333 login.LoginType = logintype;
... ... @@ -1406,7 +1382,7 @@ GO
1406 1382 Server sqlserver = null;
1407 1383 try
1408 1384 {
1409   - var sc = GetSqlConnection(cs, dbname:databasename, userid: userid, userpassword: password);
  1385 + var sc = ServerConnectionPool.GetSqlConnection(cs, dbname:databasename, userid: userid, userpassword: password);
1410 1386 sqlserver = SQLServerConnect(sc); if (sqlserver == null) return false;
1411 1387 if (sqlserver.Databases.Contains(databasename))
1412 1388 {
... ... @@ -1426,7 +1402,7 @@ GO
1426 1402 Dictionary<string, string> ReturnDict = new Dictionary<string, string>();
1427 1403 try
1428 1404 {
1429   - var sc = GetSqlConnection(cs, dbname: null, userid: userid, userpassword: password);
  1405 + var sc = ServerConnectionPool.GetSqlConnection(cs, dbname: null, userid: userid, userpassword: password);
1430 1406 sqlserver = SQLServerConnect(sc); if (sqlserver == null) return null;
1431 1407  
1432 1408 foreach (Database db in sqlserver.Databases)
... ... @@ -1489,18 +1465,9 @@ GO
1489 1465 var sqlcb = new SqlConnectionStringBuilder(cs);
1490 1466 return sqlcb.DataSource;
1491 1467 }
1492   - public enum SQLDBStatus { NoAccess, OK, }
1493   - public static SqlConnection GetSqlConnection(string sqlconnectionstring, string dbname = null, string userid = null, string userpassword = null)
1494   - {
1495   - var scb = new SqlConnectionStringBuilder(sqlconnectionstring);
1496   - if (!string.IsNullOrWhiteSpace(dbname)) { scb.InitialCatalog = dbname; }
1497   - if (!string.IsNullOrWhiteSpace(userid) && !string.IsNullOrWhiteSpace(userpassword)) { scb.UserID = userid; scb.Password = userpassword; }
1498   - else if (!string.IsNullOrWhiteSpace(userid) && string.IsNullOrWhiteSpace(userpassword)) { scb.UserID= userid; scb.IntegratedSecurity= true; }
1499   - return new SqlConnection(scb.ToString());
1500   - }
1501 1468 public static Server SQLServerConnect(string sqlconnectionstring,string dbname=null)
1502 1469 {
1503   - return SQLServerConnect(GetSqlConnection(sqlconnectionstring, dbname));
  1470 + return SQLServerConnect(ServerConnectionPool.GetSqlConnection(sqlconnectionstring, dbname));
1504 1471 }
1505 1472 public static Server SQLServerConnect(SqlConnection sqlconnection)
1506 1473 {
... ... @@ -1523,7 +1490,7 @@ GO
1523 1490  
1524 1491 public static void ShrinkDB(string sqlconnectionstring, ShrinkMethod shrinkmethod,int freespacepercent)
1525 1492 {
1526   - using (var sqlc = new SqlConnection(sqlconnectionstring))
  1493 + using (var sqlc =ServerConnectionPool.GetSqlConnection(sqlconnectionstring,open:false))
1527 1494 {
1528 1495 var sc = new ServerConnection(sqlc);
1529 1496 var srvr = new Server(sc);
... ... @@ -1532,74 +1499,13 @@ GO
1532 1499 }
1533 1500 }
1534 1501  
1535   - #region GetSize
1536   - /// <summary>
1537   - /// Visszadja a megadott adatbázis méretét
1538   - /// </summary>
1539   - /// <param name="sqlconnectionstring"></param>
1540   - /// <returns></returns>
1541   - public static string GetSize(string sqlconnectionstring)
1542   - {
1543   - try
1544   - {
1545   - using (var sqlc = new Microsoft.Data.SqlClient.SqlConnection(sqlconnectionstring))
1546   - {
1547   - var dbname = GetDBName(sqlconnectionstring);
1548   -
1549   - var getsizecmd = new Microsoft.Data.SqlClient.SqlCommand("sp_spaceused", sqlc);
1550   - getsizecmd.CommandType = System.Data.CommandType.StoredProcedure;
1551   - sqlc.Open();
1552   - var reader = getsizecmd.ExecuteReader();
1553   -
1554   - if (reader.HasRows)
1555   - {
1556   - while (reader.Read())
1557   - {
1558   - var dbn = reader["database_name"];
1559   - var dbs = reader["database_size"];
1560   - if (Convert.ToString(dbn) == dbname) { return Convert.ToString(dbs); };
1561   - }
1562   - }
1563   - sqlc.Close();
1564   - }
1565   - return "N/A";
1566   - }
1567   - catch { return "ERR"; }
1568   - }
1569   - #endregion GetSize
1570   -
1571   - #region GetPhysicalFilesLocation
1572   - /// <summary>
1573   - /// Returns the physical path to the directory that holds the files of the database
1574   - /// </summary>
1575   - /// <param name="sqlconnectionstring"></param>
1576   - /// <returns>the path, or null, if any error occurs</returns>
1577   - public static string GetPhysicalFilesLocation(string sqlconnectionstring)
1578   - {
1579   - try
1580   - {
1581   - int commandtimeout = 5000;
1582   - string scripttext = "";
1583   - scripttext += " DECLARE @physicalpath varchar(1000) = NULL;";
1584   - scripttext += " SELECT @physicalpath = f.physical_name";
1585   - scripttext += " FROM sys.master_files as f inner join sys.databases as d on f.database_id = d.database_id";
1586   - scripttext += " where d.name = '{DATABASE}' and f.type_desc = 'ROWS';";
1587   - scripttext += " SELECT 1 AS RETURNCODE, @physicalpath AS RETURNMESSAGE;";
1588   - var vars = new Dictionary<string, string>();
1589   - vars.Add(nameof(DBSubstitutionName.DATABASE), GetDBName(sqlconnectionstring));
1590   - vars.Add(nameof(DBSubstitutionName.DATASOURCE), "");
1591   - var result = ExecuteSQLScript(sqlconnectionstring, scripttext, commandtimeout, vars);
1592   - if (result.ReturnValue != 0) { return Path.GetDirectoryName(result.ReturnMessage); } else { return null; };
1593   - }
1594   - catch { return null; }
1595   - }
1596   - #endregion GetPhysicalFilesLocation
1597 1502 #region DBSubstitution
1598 1503 public enum DBSubstitutionName
1599 1504 {
1600 1505 DBKEY,DATABASE,DATASOURCE,DBOTYPE,DBONAME,DBDATAGROUP,BACKUPTS,SHRINKOPTION,SHRINKFREESPACEPERCENT,
1601 1506 }
1602 1507 #endregion DBSubstitution
  1508 +
1603 1509 #region ExecuteSQLScript
1604 1510 /// <summary>
1605 1511 /// Egy SQL script végrehajtása (GO-val lezárt batch-eket tartalmazhat)
... ... @@ -1621,66 +1527,55 @@ GO
1621 1527 /// </returns>
1622 1528 public static ReturnInfoJSON ExecuteSQLScript(string sqlconnectionstring, string sqltxt, int commandtimeout, Dictionary<string, string> vars)
1623 1529 {
1624   - using (var sqlc = new Microsoft.Data.SqlClient.SqlConnection(sqlconnectionstring))
1625   - {
1626   - sqltxt = VRH.Common.StringConstructor.ResolveConstructorR(vars, sqltxt, "{}@@");
1627   -
1628   - sqlc.Open();
1629   - string SQLBatchTxt = ""; // ebben lesznek az sql script-en belüli batch-ek összerakva
1630   - int SQLBatchIndex = 0; // az aktuális batch indexe
1631   - int scriptlineindex = 0; // az aktuális script sor indexe
1632   - System.Data.DataSet DataSet = null; // ebben lesz az eredmény (az utolsó batch eredménye)
1633   - sqltxt += "\r\nGO";// ha esetleg nem lenne, odatesszük a végére az utolsó batch-et lezáró GO-t
1634   - foreach (var scriptline in sqltxt.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
  1530 + using (var sqlc = ServerConnectionPool.GetSqlConnection(sqlconnectionstring,open:true)) { return ExecuteSQLScript(sqlc,sqltxt, commandtimeout, vars); }
  1531 + }
  1532 + public static ReturnInfoJSON ExecuteSQLScript(SqlConnection sqlconnection, string sqltxt, int commandtimeout, Dictionary<string, string> vars)
  1533 + {
  1534 + sqltxt = VRH.Common.StringConstructor.ResolveConstructorR(vars, sqltxt, "{}@@");
  1535 +
  1536 + var connectionwasopen = true;
  1537 + if (sqlconnection.State != System.Data.ConnectionState.Open) { ServerConnectionPool.Open(sqlconnection); connectionwasopen = false; }
  1538 + string SQLBatchTxt = ""; // ebben lesznek az sql script-en belüli batch-ek összerakva
  1539 + int SQLBatchIndex = 0; // az aktuális batch indexe
  1540 + int scriptlineindex = 0; // az aktuális script sor indexe
  1541 + System.Data.DataSet DataSet = null; // ebben lesz az eredmény (az utolsó batch eredménye)
  1542 + sqltxt += "\r\nGO";// ha esetleg nem lenne, odatesszük a végére az utolsó batch-et lezáró GO-t
  1543 + foreach (var scriptline in sqltxt.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
  1544 + {
  1545 + scriptlineindex += 1;
  1546 + if (Regex.Match(scriptline, @"^GO$").Success)// a batch utolső sora
1635 1547 {
1636   - scriptlineindex += 1;
1637   - if (Regex.Match(scriptline, @"^GO$").Success)// a batch utolső sora
1638   - {
1639   - if (!string.IsNullOrWhiteSpace(SQLBatchTxt))
1640   - {
1641   - SQLBatchIndex += 1;
1642   - DataSet = ExecuteSQLScriptBatch(sqlc, SQLBatchTxt, commandtimeout);
1643   - SQLBatchTxt = "";
1644   - }
1645   - }
1646   - else if (!string.IsNullOrWhiteSpace(scriptline))
  1548 + if (!string.IsNullOrWhiteSpace(SQLBatchTxt))
1647 1549 {
1648   - SQLBatchTxt += scriptline + "\r\n";
  1550 + SQLBatchIndex += 1;
  1551 + var sqlcommand = sqlconnection.CreateCommand();
  1552 + sqlcommand.CommandText = SQLBatchTxt;
  1553 + sqlcommand.CommandTimeout = commandtimeout;
  1554 + var DataAdapter = new Microsoft.Data.SqlClient.SqlDataAdapter(sqlcommand);
  1555 + DataSet = new System.Data.DataSet();
  1556 + DataAdapter.Fill(DataSet);
  1557 + SQLBatchTxt = "";
1649 1558 }
1650 1559 }
1651   -
1652   - sqlc.Close();
1653   - try
  1560 + else if (!string.IsNullOrWhiteSpace(scriptline))
1654 1561 {
1655   - var firstreturnedtable = DataSet.Tables[0];
1656   - var firstreturnedrow = firstreturnedtable.Rows[0];
1657   - var firstreturnedvalue = firstreturnedrow[0];
1658   - var secondreturnedvalue = firstreturnedrow[1];
1659   - var rv = Convert.ToInt32(firstreturnedvalue);
1660   - var rm = Convert.ToString(secondreturnedvalue);
1661   - return new ReturnInfoJSON() { ReturnValue = rv, ReturnMessage = rm, };
  1562 + SQLBatchTxt += scriptline + "\r\n";
1662 1563 }
1663   - catch { }
1664 1564 }
1665   - return new ReturnInfoJSON() { ReturnValue = 0, ReturnMessage = null, };
1666   - }
1667 1565  
1668   - /// <summary>
1669   - /// Egy SQL batch végrehajtása
1670   - /// </summary>
1671   - /// <param name="sqlc"></param>
1672   - /// <param name="sqlbatchtxt"></param>
1673   - /// <param name="commandtimeout"></param>
1674   - /// <returns></returns>
1675   - private static System.Data.DataSet ExecuteSQLScriptBatch(Microsoft.Data.SqlClient.SqlConnection sqlc, string sqlbatchtxt, int commandtimeout)
1676   - {
1677   - var sqlcommand = sqlc.CreateCommand();
1678   - sqlcommand.CommandText = sqlbatchtxt;
1679   - sqlcommand.CommandTimeout = commandtimeout;
1680   - var DataAdapter = new Microsoft.Data.SqlClient.SqlDataAdapter(sqlcommand);
1681   - var DataSet = new System.Data.DataSet();
1682   - DataAdapter.Fill(DataSet);
1683   - return DataSet;
  1566 + if (!connectionwasopen) { sqlconnection.Close(); }
  1567 + try
  1568 + {
  1569 + var firstreturnedtable = DataSet.Tables[0];
  1570 + var firstreturnedrow = firstreturnedtable.Rows[0];
  1571 + var firstreturnedvalue = firstreturnedrow[0];
  1572 + var secondreturnedvalue = firstreturnedrow[1];
  1573 + var rv = Convert.ToInt32(firstreturnedvalue);
  1574 + var rm = Convert.ToString(secondreturnedvalue);
  1575 + return new ReturnInfoJSON() { ReturnValue = rv, ReturnMessage = rm, };
  1576 + }
  1577 + catch { }
  1578 + return new ReturnInfoJSON() { ReturnValue = 0, ReturnMessage = null, };
1684 1579 }
1685 1580 #endregion ExecuteSQLScript
1686 1581 #region ExecuteSQLStoredProcedure
... ... @@ -1722,18 +1617,17 @@ GO
1722 1617  
1723 1618 public static List<SPparameter> ExecuteSQLStoredProcedure(string sqlconnectionstring, string storedprocedurename, int commandtimeout, List<SPparameter> SPparameters)
1724 1619 {
1725   - using (var sqlc = new Microsoft.Data.SqlClient.SqlConnection(sqlconnectionstring))
1726   - {
  1620 + using (var sqlc = ServerConnectionPool.GetSqlConnection(sqlconnectionstring,open:true))
  1621 + {
1727 1622 var sqlcommand = sqlc.CreateCommand();
1728 1623 sqlcommand.Connection = sqlc;
1729 1624 sqlcommand.CommandText = storedprocedurename;
1730 1625 sqlcommand.CommandTimeout = commandtimeout;
1731 1626 sqlcommand.CommandType = System.Data.CommandType.StoredProcedure;
1732 1627 //var DataAdapter = new Microsoft.Data.SqlClient.SqlDataAdapter(sqlcommand);
1733   - sqlc.Open();
1734 1628 foreach (var p in SPparameters)
1735 1629 {
1736   - var sqlparameter = new Microsoft.Data.SqlClient.SqlParameter()
  1630 + var sqlparameter = new SqlParameter()
1737 1631 {
1738 1632 ParameterName = p.Name,
1739 1633 SqlDbType = p.Type,
... ... @@ -2077,7 +1971,7 @@ GO
2077 1971  
2078 1972 public string DBName;
2079 1973 public string DataSource;
2080   - public SQLDataBaseManagerCore.SQLDBStatus Status;
  1974 + public SQLDBStatus Status;
2081 1975 public string PhysicalFilesDirectoryPath;
2082 1976 public string SizeString;
2083 1977 public string SQLCS
... ... @@ -2085,6 +1979,81 @@ GO
2085 1979 get { return XmlProcessing.ConnectionStringStore.GetSQL(this.Xml_SQLConnectionString); }
2086 1980 }
2087 1981 #endregion fields
  1982 + public enum SQLDBStatus { NoAccess, OK, }
  1983 +
  1984 + #region CollectInfo
  1985 + public SQLDataBase CollectInfo()
  1986 + {
  1987 + try
  1988 + {
  1989 + GetPhysicalFilesLocationAndSize(this.SQLCS, out this.SizeString, out this.PhysicalFilesDirectoryPath);
  1990 + this.Status = this.PhysicalFilesDirectoryPath=="ERR" ? SQLDBStatus.NoAccess : SQLDBStatus.OK;
  1991 + this.DBName = SQLDataBaseManagerCore.GetDBName(this.SQLCS);
  1992 + this.DataSource = SQLDataBaseManagerCore.GetDataSource(this.SQLCS);
  1993 + }
  1994 + catch
  1995 + {
  1996 + this.Status = SQLDBStatus.NoAccess;
  1997 + this.PhysicalFilesDirectoryPath = "";
  1998 + this.DBName = SQLDataBaseManagerCore.GetDBName(this.SQLCS);
  1999 + this.DataSource = SQLDataBaseManagerCore.GetDataSource(this.SQLCS);
  2000 + this.SizeString = "";
  2001 + }
  2002 + return this;
  2003 + }
  2004 + #endregion CollectInfo
  2005 +
  2006 + #region GetPhysicalFilesLocationAndSize
  2007 + /// <summary>
  2008 + /// Visszadja a megadott adatbázis méretét
  2009 + /// </summary>
  2010 + /// <param name="sqlconnectionstring"></param>
  2011 + /// <returns></returns>
  2012 + private void GetPhysicalFilesLocationAndSize(string sqlconnectionstring, out string dbsizetext, out string dbpathtext)
  2013 + {
  2014 + dbsizetext = null;
  2015 + dbpathtext = null;
  2016 + try
  2017 + {
  2018 + var sqlc = ServerConnectionPool.GetSqlConnection(sqlconnectionstring,open:true);
  2019 + string dbname = SQLDataBaseManagerCore.GetDBName(sqlconnectionstring);
  2020 + try
  2021 + {
  2022 + var getsizecmd = new SqlCommand("sp_spaceused", sqlc);
  2023 + getsizecmd.CommandType = System.Data.CommandType.StoredProcedure;
  2024 + var reader = getsizecmd.ExecuteReader();
  2025 +
  2026 + if (reader.HasRows)
  2027 + {
  2028 + while (reader.Read()) { if (Convert.ToString(reader["database_name"]) == dbname) { dbsizetext = Convert.ToString(reader["database_size"]); break; }; }
  2029 + }
  2030 + dbsizetext = dbsizetext ?? "N/A";
  2031 + }
  2032 + catch { }
  2033 +
  2034 + try
  2035 + {
  2036 + int commandtimeout = 5000;
  2037 + string scripttext = "";
  2038 + scripttext += " DECLARE @physicalpath varchar(1000) = NULL;";
  2039 + scripttext += " SELECT @physicalpath = f.physical_name";
  2040 + scripttext += " FROM sys.master_files AS f INNER JOIN sys.databases AS d ON f.database_id = d.database_id";
  2041 + scripttext += " WHERE d.name = '{DATABASE}' AND f.type_desc = 'ROWS';";
  2042 + scripttext += " SELECT 1 AS RETURNCODE, @physicalpath AS RETURNMESSAGE;";
  2043 + var vars = new Dictionary<string, string>();
  2044 + vars.Add(nameof(SQLDataBaseManagerCore.DBSubstitutionName.DATABASE), dbname);
  2045 + vars.Add(nameof(SQLDataBaseManagerCore.DBSubstitutionName.DATASOURCE), "");
  2046 + var result = SQLDataBaseManagerCore.ExecuteSQLScript(sqlc, scripttext, commandtimeout, vars);
  2047 + if (result.ReturnValue != 0) { dbpathtext = Path.GetDirectoryName(result.ReturnMessage); }
  2048 + }
  2049 + catch { }
  2050 + sqlc.Close();
  2051 + }
  2052 + catch { }
  2053 + dbsizetext = dbsizetext ?? "ERR";
  2054 + dbpathtext = dbpathtext ?? "ERR";
  2055 + }
  2056 + #endregion GetPhysicalFilesLocationAndSize
2088 2057  
2089 2058 #region basic constructor
2090 2059 public SQLDataBase() { }
... ... @@ -2265,4 +2234,45 @@ GO
2265 2234 #endregion SQLData class
2266 2235 }
2267 2236 #endregion SQLDataBase class
  2237 +
  2238 + public static class ServerConnectionPool
  2239 + {
  2240 + private static List<string> InaccessibleConnectionList = new List<string>();
  2241 + public static SqlConnection GetSqlConnection(string sqlconnectionstring, string dbname = null, string userid = null, string userpassword = null,bool open=false)
  2242 + {
  2243 + var scb = new SqlConnectionStringBuilder(sqlconnectionstring);
  2244 + if (!string.IsNullOrWhiteSpace(dbname)) { scb.InitialCatalog = dbname; }
  2245 + if (!string.IsNullOrWhiteSpace(userid) && !string.IsNullOrWhiteSpace(userpassword)) { scb.UserID = userid; scb.Password = userpassword; }
  2246 + else if (!string.IsNullOrWhiteSpace(userid) && string.IsNullOrWhiteSpace(userpassword)) { scb.UserID = userid; scb.IntegratedSecurity = true; }
  2247 + var sqlc = new SqlConnection(scb.ToString());
  2248 + if (open) Open(sqlc);
  2249 + return sqlc;
  2250 + }
  2251 + public static void Open(SqlConnection sqlc)
  2252 + {
  2253 + if (IsMasterInacessible(sqlc) || IsInacessible(sqlc))
  2254 + {
  2255 + throw new Exception($"DB {sqlc.Database} on server {sqlc.DataSource} is inaccessible!");
  2256 + }
  2257 + try { sqlc.Open(); }
  2258 + catch (Exception ex)
  2259 + {
  2260 + MarkMasterInacessible(sqlc);
  2261 + MarkInacessible(sqlc);
  2262 + throw ex;
  2263 + }
  2264 + }
  2265 + private static bool IsMasterDBConnected(SqlConnection sqlc)
  2266 + {
  2267 + try { (new SqlConnection((new SqlConnectionStringBuilder(sqlc.ConnectionString) { InitialCatalog = "master" }).ToString())).Open(); return true; }
  2268 + catch { return false; }
  2269 + }
  2270 + private static string MasterDBId(SqlConnection sqlc) { return sqlc.DataSource + "#master"; }
  2271 + private static string DBId(SqlConnection sqlc) { return sqlc.DataSource + "#" + sqlc.Database; }
  2272 + private static bool IsInacessible(SqlConnection sqlc) { return InaccessibleConnectionList.Contains(DBId(sqlc)); }
  2273 + private static bool IsMasterInacessible(SqlConnection sqlc) { return InaccessibleConnectionList.Contains(MasterDBId(sqlc)); }
  2274 + private static void MarkInacessible(SqlConnection sqlc) { if (!IsInacessible(sqlc)) { InaccessibleConnectionList.Add(DBId(sqlc)); }}
  2275 + private static void MarkMasterInacessible(SqlConnection sqlc) { if (!IsMasterInacessible(sqlc)) { InaccessibleConnectionList.Add(MasterDBId(sqlc)); } }
  2276 + public static void Init() { InaccessibleConnectionList.Clear(); }
  2277 + }
2268 2278 }
... ...
Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs
... ... @@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
32 32 // You can specify all the values or you can default the Build and Revision Numbers
33 33 // by using the '*' as shown below:
34 34 // [assembly: AssemblyVersion("1.0.*")]
35   -[assembly: AssemblyVersion("1.27.1.0")]
36   -[assembly: AssemblyFileVersion("1.27.1.0")]
  35 +[assembly: AssemblyVersion("1.27.2.0")]
  36 +[assembly: AssemblyFileVersion("1.27.2.0")]
... ...