PageRenderTime 4254ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/QuickMon2/QMSQLQuery/QueryInstance.cs

#
C# | 280 lines | 271 code | 9 blank | 0 comment | 60 complexity | 87c387e361286ee5dcad2735a48d1ec4 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. namespace QuickMon
  8. {
  9. public class QueryInstance
  10. {
  11. #region Properties
  12. public string Name { get; set; }
  13. public string SqlServer { get; set; }
  14. public string Database { get; set; }
  15. private bool integratedSecurity = true;
  16. public bool IntegratedSecurity { get { return integratedSecurity; } set { integratedSecurity = value; } }
  17. public string UserName { get; set; }
  18. public string Password { get; set; }
  19. private int cmndTimeOut = 60;
  20. public int CmndTimeOut { get { return cmndTimeOut; } set { cmndTimeOut = value; } }
  21. public bool UseSPForSummary { get; set; }
  22. public bool UseSPForDetail { get; set; }
  23. public string SummaryQuery { get; set; }
  24. public string DetailQuery { get; set; }
  25. private bool returnValueIsNumber = true;
  26. public bool ReturnValueIsNumber { get { return returnValueIsNumber; } set { returnValueIsNumber = value; } }
  27. public bool ReturnValueInverted { get; set; }
  28. private string warningValue = "1";
  29. public string WarningValue { get { return warningValue; } set { warningValue = value; } }
  30. private string errorValue = "2";
  31. public string ErrorValue { get { return errorValue; } set { errorValue = value; } }
  32. private string successValue = "[any]";
  33. public string SuccessValue { get { return successValue; } set { successValue = value; } }
  34. public bool UseRowCountAsValue { get; set; }
  35. public bool UsePersistentConnection { get; set; }
  36. public bool UseExecuteTimeAsValue { get; set; }
  37. private SqlConnection testExecutionConn = null;
  38. public string ApplicationName { get; set; }
  39. #endregion
  40. public override string ToString()
  41. {
  42. return string.Format("{0} - {1}\\{2}\\{3}", Name, SqlServer, Database, SummaryQuery);
  43. }
  44. #region Connection stuff
  45. private string GetConnectionString()
  46. {
  47. SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
  48. sb.ApplicationName = ApplicationName;
  49. sb.DataSource = SqlServer;
  50. sb.InitialCatalog = Database;
  51. sb.IntegratedSecurity = IntegratedSecurity;
  52. if (!IntegratedSecurity)
  53. {
  54. sb.UserID = UserName;
  55. sb.Password = Password;
  56. }
  57. return sb.ConnectionString;
  58. }
  59. private SqlConnection GetConnection()
  60. {
  61. try
  62. {
  63. if (UsePersistentConnection)
  64. {
  65. if (testExecutionConn == null || testExecutionConn.State == ConnectionState.Closed)
  66. {
  67. testExecutionConn = new SqlConnection(GetConnectionString());
  68. testExecutionConn.Open();
  69. }
  70. }
  71. else
  72. {
  73. if (testExecutionConn != null)
  74. {
  75. CloseConnection();
  76. }
  77. testExecutionConn = new SqlConnection(GetConnectionString());
  78. testExecutionConn.Open();
  79. }
  80. }
  81. catch
  82. {
  83. CloseConnection(true);
  84. throw;
  85. }
  86. return testExecutionConn;
  87. }
  88. private void CloseConnection(bool closeNonPersistent = false)
  89. {
  90. try
  91. {
  92. if (closeNonPersistent && UsePersistentConnection)
  93. {
  94. testExecutionConn.Close();
  95. testExecutionConn = null;
  96. }
  97. }
  98. catch { }
  99. }
  100. #endregion
  101. #region RunQuery
  102. internal object RunQuery()
  103. {
  104. object value = null;
  105. if (!ReturnValueIsNumber)
  106. value = RunQueryWithSingleResult();
  107. else if (!UseRowCountAsValue && !UseExecuteTimeAsValue)
  108. value = RunQueryWithSingleResult();
  109. else if (UseRowCountAsValue)
  110. value = RunQueryWithCountResult();
  111. else
  112. value = RunQueryWithExecutionTimeResult();
  113. return value;
  114. }
  115. private int RunQueryWithCountResult()
  116. {
  117. int returnValue = 0;
  118. SqlConnection conn = GetConnection();
  119. try
  120. {
  121. using (SqlCommand cmnd = new SqlCommand(SummaryQuery, conn))
  122. {
  123. cmnd.CommandType = UseSPForSummary ? CommandType.StoredProcedure : CommandType.Text;
  124. cmnd.CommandTimeout = cmndTimeOut;
  125. using (SqlDataReader r = cmnd.ExecuteReader())
  126. {
  127. while (r.Read())
  128. {
  129. returnValue++;
  130. }
  131. }
  132. }
  133. CloseConnection();
  134. }
  135. catch
  136. {
  137. CloseConnection(true);
  138. throw;
  139. }
  140. return returnValue;
  141. }
  142. private object RunQueryWithSingleResult()
  143. {
  144. object returnValue = null;
  145. SqlConnection conn = GetConnection();
  146. try
  147. {
  148. using (SqlCommand cmnd = new SqlCommand(SummaryQuery, conn))
  149. {
  150. cmnd.CommandType = UseSPForSummary ? CommandType.StoredProcedure : CommandType.Text;
  151. cmnd.CommandTimeout = cmndTimeOut;
  152. using (SqlDataReader r = cmnd.ExecuteReader())
  153. {
  154. if (r.Read())
  155. returnValue = r[0];
  156. }
  157. }
  158. CloseConnection();
  159. }
  160. catch
  161. {
  162. CloseConnection(true);
  163. throw;
  164. }
  165. return returnValue;
  166. }
  167. private long RunQueryWithExecutionTimeResult()
  168. {
  169. long returnValue = 0;
  170. System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  171. SqlConnection conn = GetConnection();
  172. try
  173. {
  174. using (SqlCommand cmnd = new SqlCommand(SummaryQuery, conn))
  175. {
  176. cmnd.CommandType = UseSPForSummary ? CommandType.StoredProcedure : CommandType.Text;
  177. cmnd.CommandTimeout = cmndTimeOut;
  178. cmnd.Prepare();
  179. sw.Start();
  180. using (SqlDataReader r = cmnd.ExecuteReader())
  181. {
  182. while (r.Read())
  183. {
  184. }
  185. }
  186. sw.Stop();
  187. returnValue = sw.ElapsedMilliseconds;
  188. }
  189. CloseConnection();
  190. }
  191. catch
  192. {
  193. CloseConnection(true);
  194. throw;
  195. }
  196. return returnValue;
  197. }
  198. #endregion
  199. internal MonitorStates GetState(object value)
  200. {
  201. MonitorStates currentState = MonitorStates.Good;
  202. if (value == DBNull.Value)
  203. {
  204. if (ErrorValue == "[null]")
  205. currentState = MonitorStates.Error;
  206. else if (WarningValue == "[null]")
  207. currentState = MonitorStates.Warning;
  208. }
  209. else //non null value
  210. {
  211. if (!ReturnValueIsNumber)
  212. {
  213. if (value.ToString() == ErrorValue)
  214. currentState = MonitorStates.Error;
  215. else if (value.ToString() == WarningValue)
  216. currentState = MonitorStates.Warning;
  217. else if (value.ToString() == SuccessValue || SuccessValue == "[any]")
  218. currentState = MonitorStates.Good; //just to flag condition
  219. else if (WarningValue == "[any]")
  220. currentState = MonitorStates.Warning;
  221. else if (ErrorValue == "[any]")
  222. currentState = MonitorStates.Error;
  223. }
  224. else //now we know the value is not null and must be in a range
  225. {
  226. if (!value.IsNumber()) //value must be a number!
  227. {
  228. currentState = MonitorStates.Error;
  229. }
  230. else if (ErrorValue != "[any]" && ErrorValue != "[null]" &&
  231. (
  232. (!ReturnValueInverted && double.Parse(value.ToString()) >= double.Parse(ErrorValue)) ||
  233. (ReturnValueInverted && double.Parse(value.ToString()) <= double.Parse(ErrorValue))
  234. )
  235. )
  236. {
  237. currentState = MonitorStates.Error;
  238. }
  239. else if (WarningValue != "[any]" && WarningValue != "[null]" &&
  240. (
  241. (!ReturnValueInverted && double.Parse(value.ToString()) >= double.Parse(WarningValue)) ||
  242. (ReturnValueInverted && double.Parse(value.ToString()) <= double.Parse(WarningValue))
  243. )
  244. )
  245. {
  246. currentState = MonitorStates.Warning;
  247. }
  248. }
  249. }
  250. return currentState;
  251. }
  252. internal DataSet RunDetailQuery()
  253. {
  254. DataSet returnValues = new DataSet();
  255. using (SqlConnection conn = new SqlConnection(GetConnectionString()))
  256. {
  257. conn.Open();
  258. using (SqlCommand cmnd = new SqlCommand(DetailQuery, conn))
  259. {
  260. cmnd.CommandType = UseSPForDetail ? CommandType.StoredProcedure : CommandType.Text;
  261. cmnd.CommandTimeout = cmndTimeOut;
  262. using (SqlDataAdapter da = new SqlDataAdapter(cmnd))
  263. {
  264. da.Fill(returnValues);
  265. }
  266. }
  267. }
  268. return returnValues;
  269. }
  270. }
  271. }