PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/QuickMon3/QuickMonSQLAgents/Notifiers/SqlDatabaseNotifier/EditConfig/SqlDatabaseNotifierEditConfig.cs

#
C# | 207 lines | 193 code | 14 blank | 0 comment | 13 complexity | cb535ad249580f93e63b95b00d996455 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using QuickMon.Forms;
  11. namespace QuickMon.Notifiers
  12. {
  13. public partial class SqlDatabaseNotifierEditConfig : SimpleNotifierEditConfig
  14. {
  15. public SqlDatabaseNotifierEditConfig()
  16. {
  17. InitializeComponent();
  18. }
  19. public override void LoadEditData()
  20. {
  21. SQLDatabaseNotifierConfig currentConfig;
  22. if (SelectedEntry != null)
  23. currentConfig = (SQLDatabaseNotifierConfig)SelectedEntry;
  24. else
  25. currentConfig = (SQLDatabaseNotifierConfig)SelectedConfig;
  26. if (currentConfig != null)
  27. {
  28. txtServer.Text = currentConfig.SqlServer;
  29. txtDatabase.Text = currentConfig.Database;
  30. chkIntegratedSec.Checked = currentConfig.IntegratedSec;
  31. txtUserName.Text = currentConfig.UserName;
  32. txtPassword.Text = currentConfig.Password;
  33. numericUpDownCmndTimeOut.Value = currentConfig.CmndTimeOut;
  34. chkUseSP.Checked = currentConfig.UseSP;
  35. txtCmndValue.Text = currentConfig.CmndValue;
  36. txtAlertFieldName.Text = currentConfig.AlertFieldName;
  37. txtCollectorType.Text = currentConfig.CollectorTypeFieldName;
  38. txtCategoryFieldName.Text = currentConfig.CategoryFieldName;
  39. txtPreviousStateFieldName.Text = currentConfig.PreviousStateFieldName;
  40. txtCurrentStateFieldName.Text = currentConfig.CurrentStateFieldName;
  41. txtDetailsFieldName.Text = currentConfig.DetailsFieldName;
  42. chkUseSP2.Checked = currentConfig.UseSPForViewer;
  43. txtViewerName.Text = currentConfig.ViewerName;
  44. txtDateTimeFieldName.Text = currentConfig.DateTimeFieldName;
  45. }
  46. }
  47. public override void OkClicked()
  48. {
  49. SQLDatabaseNotifierConfig currentConfig;
  50. if (SelectedEntry != null)
  51. currentConfig = (SQLDatabaseNotifierConfig)SelectedEntry;
  52. else if (SelectedConfig != null)
  53. currentConfig = (SQLDatabaseNotifierConfig)SelectedConfig;
  54. else
  55. {
  56. SelectedConfig = new SQLDatabaseNotifierConfig();
  57. currentConfig = (SQLDatabaseNotifierConfig)SelectedConfig;
  58. }
  59. currentConfig.SqlServer = txtServer.Text;
  60. currentConfig.Database = txtDatabase.Text;
  61. currentConfig.IntegratedSec = chkIntegratedSec.Checked;
  62. currentConfig.UserName = txtUserName.Text;
  63. currentConfig.Password = txtPassword.Text;
  64. currentConfig.CmndTimeOut = (int)numericUpDownCmndTimeOut.Value;
  65. currentConfig.UseSP = chkUseSP.Checked;
  66. currentConfig.CmndValue = txtCmndValue.Text;
  67. currentConfig.AlertFieldName = txtAlertFieldName.Text;
  68. currentConfig.CollectorTypeFieldName = txtCollectorType.Text;
  69. currentConfig.CategoryFieldName = txtCategoryFieldName.Text;
  70. currentConfig.PreviousStateFieldName = txtPreviousStateFieldName.Text;
  71. currentConfig.CurrentStateFieldName = txtCurrentStateFieldName.Text;
  72. currentConfig.DetailsFieldName = txtDetailsFieldName.Text;
  73. currentConfig.UseSPForViewer = chkUseSP2.Checked;
  74. currentConfig.ViewerName = txtViewerName.Text;
  75. currentConfig.DateTimeFieldName = txtDateTimeFieldName.Text;
  76. DialogResult = System.Windows.Forms.DialogResult.OK;
  77. Close();
  78. }
  79. private void CheckOKEnabled()
  80. {
  81. SetOKEnabled(txtServer.Text.Trim().Length > 0 && txtDatabase.Text.Trim().Length > 0);
  82. }
  83. private void cmdTest_Click(object sender, EventArgs e)
  84. {
  85. string connStr = "";
  86. string lastStep = "";
  87. try
  88. {
  89. SQLDatabaseNotifierConfig tmpConfig = new SQLDatabaseNotifierConfig();
  90. lastStep = "Setting up connection string";
  91. tmpConfig.SqlServer = txtServer.Text;
  92. tmpConfig.Database = txtDatabase.Text;
  93. tmpConfig.IntegratedSec = chkIntegratedSec.Checked;
  94. tmpConfig.UserName = txtUserName.Text;
  95. tmpConfig.Password = txtPassword.Text;
  96. connStr = tmpConfig.GetConnectionString();
  97. lastStep = "Opening connection";
  98. using (SqlConnection conn = new SqlConnection(connStr))
  99. {
  100. conn.Open();
  101. lastStep = "Inserting test message into database";
  102. string cmndName = txtCmndValue.Text.Replace("'", "''");
  103. string viewerName = txtViewerName.Text.Replace("'", "''");
  104. string alertParamName = txtAlertFieldName.Text.Replace("'", "''").Replace("@", "");
  105. string collectorTypeParamName = txtCollectorType.Text.Replace("'", "''").Replace("@", "");
  106. string categoryParamName = txtCategoryFieldName.Text.Replace("'", "''").Replace("@", "");
  107. string previousStateParamName = txtPreviousStateFieldName.Text.Replace("'", "''").Replace("@", "");
  108. string currentStateParamName = txtCurrentStateFieldName.Text.Replace("'", "''").Replace("@", "");
  109. string detailsParamName = txtDetailsFieldName.Text.Replace("'", "''").Replace("@", "");
  110. string datetimeParamName = txtDateTimeFieldName.Text.Replace("'", "''").Replace("@", "");
  111. int topCount = 1;
  112. string sql = chkUseSP.Checked ? cmndName :
  113. string.Format("Insert {0} ({1}, {2}, {3}, {4}, {5}, {6}) values(@{1}, @{2}, @{3}, @{4}, @{5}, @{6})",
  114. cmndName,
  115. alertParamName,
  116. collectorTypeParamName,
  117. categoryParamName,
  118. previousStateParamName,
  119. currentStateParamName,
  120. detailsParamName);
  121. byte alertTypeValue = 0;
  122. byte previousState = 0;
  123. byte currentState = 0;
  124. using (SqlCommand cmnd = new SqlCommand(sql, conn))
  125. {
  126. SqlParameter[] paramArr = new SqlParameter[]
  127. {
  128. new SqlParameter("@" + alertParamName, alertTypeValue),
  129. new SqlParameter("@" + collectorTypeParamName, "N/A"),
  130. new SqlParameter("@" + categoryParamName, "Test"),
  131. new SqlParameter("@" + previousStateParamName, previousState),
  132. new SqlParameter("@" + currentStateParamName, currentState),
  133. new SqlParameter("@" + detailsParamName, "Testing QuickMon database notifier insert")
  134. };
  135. cmnd.Parameters.AddRange(paramArr);
  136. if (chkUseSP.Checked)
  137. cmnd.CommandType = CommandType.StoredProcedure;
  138. else
  139. cmnd.CommandType = CommandType.Text;
  140. cmnd.CommandTimeout = (int)numericUpDownCmndTimeOut.Value;
  141. cmnd.ExecuteNonQuery();
  142. }
  143. lastStep = "Retrieve message from database";
  144. sql = chkUseSP2.Checked ? viewerName : GetQuery(viewerName, alertParamName, collectorTypeParamName,
  145. categoryParamName, previousStateParamName, currentStateParamName, detailsParamName, datetimeParamName);
  146. using (SqlCommand cmnd = new SqlCommand(sql, conn))
  147. {
  148. SqlParameter[] paramArr = new SqlParameter[]
  149. {
  150. new SqlParameter("@Top", topCount),
  151. new SqlParameter("@FromDate", DateTime.Now.AddDays(-1)),
  152. new SqlParameter("@ToDate", DateTime.Now.AddMinutes(1)),
  153. new SqlParameter("@" + alertParamName, alertTypeValue),
  154. new SqlParameter("@" + categoryParamName, "Test"),
  155. new SqlParameter("@" + currentStateParamName, currentState),
  156. new SqlParameter("@" + detailsParamName, "Testing QuickMon database notifier insert")
  157. };
  158. cmnd.Parameters.AddRange(paramArr);
  159. if (chkUseSP2.Checked)
  160. cmnd.CommandType = CommandType.StoredProcedure;
  161. else
  162. cmnd.CommandType = CommandType.Text;
  163. cmnd.CommandTimeout = (int)numericUpDownCmndTimeOut.Value;
  164. using (SqlDataReader r = cmnd.ExecuteReader())
  165. {
  166. if (!r.Read())
  167. throw new Exception("No data returned by server");
  168. }
  169. }
  170. }
  171. MessageBox.Show("Test was successful!", "Test connection", MessageBoxButtons.OK, MessageBoxIcon.Information);
  172. }
  173. catch (Exception ex)
  174. {
  175. MessageBox.Show(string.Format("Failed at step: {0}\r\nDetails: {1}", lastStep, ex.Message), "Test connection", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  176. }
  177. }
  178. private string GetQuery(string viewerName, string alertParamName, string collectorTypeParamName, string categoryParamName, string previousStateParamName, string currentStateParamName, string detailsParamName, string datetimeParamName)
  179. {
  180. return string.Format("Select top (@Top) {0}, {1}, {2}, {3}, {4}, {5}, @FromDate, @ToDate, @{0}, @{1}, @{3}, @{4}, @{5} from {6} with (Readpast) order by {6} desc",
  181. alertParamName, collectorTypeParamName, categoryParamName, previousStateParamName, currentStateParamName, detailsParamName,
  182. viewerName, datetimeParamName);
  183. }
  184. private void txtServer_TextChanged(object sender, EventArgs e)
  185. {
  186. CheckOKEnabled();
  187. }
  188. private void llblDBCreateScript_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  189. {
  190. Clipboard.SetText(Properties.Resources.ExampleSqlDatabaseCreateScript);
  191. }
  192. }
  193. }