/// <summary> /// Retrieves a Parameter Value from the Parameter Table indicated by the Parameter Manager Object that was /// passed to the Method. If the Value cannot be determined, a NULL String is returned. /// </summary> /// <param name="ParameterName"> /// The Name of the Parameter that is t be looked up in the Parameter Table. /// </param> /// <param name="ParameterManager"> /// A Parameter Manager Object that provides the connection information for the Parameter Table. /// </param> /// <returns> /// String - The value for the Parameter named in the "ParameterName" input parameter. A NULL String is returned /// if the Parameter Value cannot be determined. /// </returns> public string RetrieveParameterValue(string ParameterName, PDX.BTS.DataMaintenance.MaintTools.ParameterManager ParameterManager, bool DecryptValue = true) { PDX.BTS.DataMaintenance.MaintTools.EncryptionManager encryptionManager = null; try { // If the Parameter Manager Object is not initialized, return an empty string to indicate that the Parameter Value could not // be determined. if (!ParameterManager.IsInitialized()) { // Return an empty string to indicate that the value could not determined. return ""; } // Determine if the Parameter exists in the Parameter Table. If it does, return // the value. Otherwise, return an empty string to indicate that the Parameter // Value could not be found in the Table. string parameterValue = null; if (ParameterManager.ParameterExists(ParameterName)) { // Retrieve the Paarameter Value from the Parameter Table. parameterValue = ParameterManager.ReadParameter(ParameterName); // If the Parameter Value is encrypted, decrypt it. if (ParameterManager.ParameterEncrypted(ParameterName)) { // Instantiate an Encryption Manager Object. encryptionManager = new PDX.BTS.DataMaintenance.MaintTools.EncryptionManager(); // Decrypt the encrypted parameter value. parameterValue = encryptionManager.DecryptString(parameterValue); } } else { // Set the return value to an empty string to indicate that the value could not // be found in the Parameter Table. parameterValue = ""; } // Return the Parameter Value to the calling method. return parameterValue; } catch (System.Exception caught) { // Determine the Line Number from which the exception was thrown. System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true); System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1); int lineNumber = stackFrame.GetFileLineNumber(); // Let the User know that this method failed. if (ErrorMessage != null) { ErrorMessage("The RetrieveParameterValue() Method failed with error message - " + caught.Message + " (Line: " + lineNumber.ToString() + ")!"); } // Return a NULL String to the calling Method to indicate that this Method failed. return null; } finally { // If the Encryption Manager was instantiated and not released, release it before exiting. if (encryptionManager != null) { encryptionManager = null; } } }
/// <summary> /// Retrieves the Parameter Value associated with the passed Key from the Parameter Table and returns /// it as a string to the calling routine. /// </summary> /// <param name="parameterName"> /// ParameterName - The name (key) of the parameter whose value is being sought. /// /// DecryptValue - Indicates whether a value that is encrypted in the Parameter Table should be decrypted before /// it is returned. /// </param> /// <returns> /// The Parameter Value if found or an empty string to indicate that the value could not be obtained /// from the database. /// </returns> public string ReadParameter(string parameterName, bool DecryptValue) { string sqlStatement = null; SqlCommand parameterValueCommand = null; SqlDataReader parameterValueReader = null; string parameterValue = null; PDX.BTS.DataMaintenance.MaintTools.EncryptionManager encryptionManager = null; try { // Make sure there is a valid connection to the database before moving on. if (!ValidConnection()) { // Return an empty string "" to the calling routine to indicate that the Parameter Value could not // be retrieved from the Parameter Table. return ""; } // Make sure the Parameter Exists before trying to pull it from the Database. if (!ParameterExists(parameterName)) { // Return an empty string "" to the calling routine to indicate that the Parameter Value could not // be retrieved from the Parameter Table. return ""; } // Build the SQL Statement that will be used to retrieve the Parameter Value from the Parameter Table. sqlStatement = "SELECT Parameter_Value " + "FROM " + _parameterTableName + " " + "WHERE Parameter_Name = '" + parameterName + "'"; // Create the command object and that will be used to retrieve the Parameter Value from the Parameter // Table. parameterValueCommand = new SqlCommand(); parameterValueCommand.Connection = _parameterDatabaseConnection; parameterValueCommand.CommandText = sqlStatement; // Use the command that was just created to populate a data reader. parameterValueReader = parameterValueCommand.ExecuteReader(); // If the Data Reader contains rows, the Parameter Value was retrieved so return the value that was // obtained from the Parameter Table to the calling routine. Otherwise, return an empty string to // indicate that the value was not obtained. if (parameterValueReader.HasRows) { // Retrieve the data from the Table. parameterValueReader.Read(); // Get the Parameter Value from the reader. parameterValue = (string)parameterValueReader["Parameter_Value"]; } else { // Set the return value to an empty string. parameterValue = ""; } // Close the Parameter Value SQL Reader. parameterValueReader.Close(); // If the User wanted the value decrypted and it is encrypted in the Parameters Table, decrypt the value. if ((DecryptValue) && (ParameterEncrypted(parameterName))) { // Instantiate an Encryption Manager Object. encryptionManager = new PDX.BTS.DataMaintenance.MaintTools.EncryptionManager(); // Decrypt the Parameter Value. parameterValue = encryptionManager.DecryptString(parameterValue, "sombra1"); // Close the Parameter Manager Object since it is no longer needed. encryptionManager = null; } // Return the Parameter Value or the empty string to the calling routine. return parameterValue; } catch { // Return a NULL string to indicate that this process failed. return null; } }
} // ReadParameter /// <summary> /// Retrieves the Parameter Value associated with the passed Key from the Parameter Table and returns /// it as a string to the calling routine. /// </summary> /// <param name="parameterName"> /// ParameterName - The name (key) of the parameter whose value is being sought. /// /// DecryptValue - Indicates whether a value that is encrypted in the Parameter Table should be decrypted before /// it is returned. /// </param> /// <returns> /// The Parameter Value if found or an empty string to indicate that the value could not be obtained /// from the database. /// </returns> public string ReadParameter(string parameterName, bool DecryptValue) { string sqlStatement = null; SqlCommand parameterValueCommand = null; SqlDataReader parameterValueReader = null; string parameterValue = null; PDX.BTS.DataMaintenance.MaintTools.EncryptionManager encryptionManager = null; try { // Make sure there is a valid connection to the database before moving on. if (!ValidConnection()) { // Return an empty string "" to the calling routine to indicate that the Parameter Value could not // be retrieved from the Parameter Table. return(""); } // Make sure the Parameter Exists before trying to pull it from the Database. if (!ParameterExists(parameterName)) { // Return an empty string "" to the calling routine to indicate that the Parameter Value could not // be retrieved from the Parameter Table. return(""); } // Build the SQL Statement that will be used to retrieve the Parameter Value from the Parameter Table. sqlStatement = "SELECT Parameter_Value " + "FROM " + _parameterTableName + " " + "WHERE Parameter_Name = '" + parameterName + "'"; // Create the command object and that will be used to retrieve the Parameter Value from the Parameter // Table. parameterValueCommand = new SqlCommand(); parameterValueCommand.Connection = _parameterDatabaseConnection; parameterValueCommand.CommandText = sqlStatement; // Use the command that was just created to populate a data reader. parameterValueReader = parameterValueCommand.ExecuteReader(); // If the Data Reader contains rows, the Parameter Value was retrieved so return the value that was // obtained from the Parameter Table to the calling routine. Otherwise, return an empty string to // indicate that the value was not obtained. if (parameterValueReader.HasRows) { // Retrieve the data from the Table. parameterValueReader.Read(); // Get the Parameter Value from the reader. parameterValue = (string)parameterValueReader["Parameter_Value"]; } else { // Set the return value to an empty string. parameterValue = ""; } // Close the Parameter Value SQL Reader. parameterValueReader.Close(); // If the User wanted the value decrypted and it is encrypted in the Parameters Table, decrypt the value. if ((DecryptValue) && (ParameterEncrypted(parameterName))) { // Instantiate an Encryption Manager Object. encryptionManager = new PDX.BTS.DataMaintenance.MaintTools.EncryptionManager(); // Decrypt the Parameter Value. parameterValue = encryptionManager.DecryptString(parameterValue, "sombra1"); // Close the Parameter Manager Object since it is no longer needed. encryptionManager = null; } // Return the Parameter Value or the empty string to the calling routine. return(parameterValue); } catch { // Return a NULL string to indicate that this process failed. return(null); } } // ReadParameter