private void lblColumnNameSequence_DoubleClick(object sender, EventArgs e) { try { WMIConfigEntry tmpWMIConfig = new WMIConfigEntry(); tmpWMIConfig.Name = txtName.Text; tmpWMIConfig.Namespace = txtNamespace.Text; tmpWMIConfig.Machinename = txtMachines.Text; tmpWMIConfig.StateQuery = txtStateQuery.Text; tmpWMIConfig.ReturnValueIsInt = chkIsReturnValueInt.Checked; tmpWMIConfig.ReturnValueInverted = !chkReturnValueNotInverted.Checked; tmpWMIConfig.UseRowCountAsValue = chkUseRowCountAsValue.Checked; tmpWMIConfig.SuccessValue = cboSuccessValue.Text; tmpWMIConfig.WarningValue = cboWarningValue.Text; tmpWMIConfig.ErrorValue = cboErrorValue.Text; tmpWMIConfig.DetailQuery = txtDetailQuery.Text; tmpWMIConfig.ColumnNames = txtColumnNames.Text.ToListFromCSVString(); // tmpWMIConfig.KeyColumn = (int)keyColumnNumericUpDown.Value; List <DataColumn> columns = tmpWMIConfig.GetDetailQueryColumns(); txtColumnNames.Text = ""; columns.ForEach(c => txtColumnNames.Text += c.ColumnName + ", "); txtColumnNames.Text = txtColumnNames.Text.TrimEnd(' ', ','); } catch { } }
private void cmdTestDB_Click(object sender, EventArgs e) { if (DoValidate()) { string lastStep = "Initialize values"; string columnWarningText = ""; try { WMIConfigEntry tmpWMIConfig = new WMIConfigEntry(); tmpWMIConfig.Name = txtName.Text; tmpWMIConfig.Namespace = txtNamespace.Text; tmpWMIConfig.Machinename = txtMachines.Text; tmpWMIConfig.StateQuery = txtStateQuery.Text; tmpWMIConfig.ReturnValueIsInt = chkIsReturnValueInt.Checked; tmpWMIConfig.ReturnValueInverted = !chkReturnValueNotInverted.Checked; tmpWMIConfig.UseRowCountAsValue = chkUseRowCountAsValue.Checked; tmpWMIConfig.SuccessValue = cboSuccessValue.Text; tmpWMIConfig.WarningValue = cboWarningValue.Text; tmpWMIConfig.ErrorValue = cboErrorValue.Text; tmpWMIConfig.DetailQuery = txtDetailQuery.Text; tmpWMIConfig.ColumnNames = txtColumnNames.Text.ToListFromCSVString(); //tmpWMIConfig.KeyColumn = (int)keyColumnNumericUpDown.Value; object returnValue = null; if (tmpWMIConfig.UseRowCountAsValue) { lastStep = "Run summary query (row count as value)"; returnValue = tmpWMIConfig.RunQueryWithCountResult(); } else { lastStep = "Run summary query"; if (returnValue.IsIntegerTypeNumber()) { object currentValue = null; currentValue = tmpWMIConfig.RunQueryWithSingleResult(); if (currentValue.IsNumber()) { returnValue = (decimal)currentValue; } else { throw new Exception(string.Format("Return value is not an integer!\r\nValue returned: {0}", returnValue)); } } else { returnValue = tmpWMIConfig.RunQueryWithSingleResult(); } } if (tmpWMIConfig.ReturnValueIsInt) { lastStep = "Test return value is an Integer"; if (!returnValue.IsIntegerTypeNumber()) { throw new Exception(string.Format("Return value is not an integer!\r\nValue returned: {0}", returnValue)); } } //testing detail query lastStep = "Testing detail query - Getting column names"; List <DataColumn> columns = tmpWMIConfig.GetDetailQueryColumns(); lastStep = "Testing detail query - Custom column name sequence check"; StringBuilder sbColumns = new StringBuilder(); for (int i = 1; i < columns.Count; i++) { sbColumns.AppendLine(columns[i].ColumnName); } foreach (string columnName in tmpWMIConfig.ColumnNames) { if ((from c in columns where c.ColumnName.ToUpper() == columnName.ToUpper() select c).Count() != 1) { columnWarningText += columnName + ", "; } } if (chkCopyColumnNames.Checked) { Clipboard.SetText(sbColumns.ToString()); } lastStep = "Testing detail query"; DataSet ds = tmpWMIConfig.RunDetailQuery(); if (columnWarningText.Length == 0) { MessageBox.Show(string.Format("Success!\r\nSummary value return: {0}\r\nDetail row count: {1}\r\nDetail columns: {2}", returnValue, ds.Tables[0].Rows.Count, columns.ToCSVString()), "Test", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(string.Format("Success (with warning)!\r\nSummary value return: {0}\r\nDetail row count: {1}\r\nDetail columns returned: {2}\r\nColumns not found: {3}", returnValue, ds.Tables[0].Rows.Count, columns.ToCSVString(), columnWarningText), "Test", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception ex) { MessageBox.Show(string.Format("Failed!\r\nLast step: {0}\r\n{1}", lastStep, ex.Message), "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }