Ejemplo n.º 1
0
        public string GetSetting(string key, bool decrypt)
        {
            if (IsDebugEnabled)
            {
                Logger.Debug(System.Reflection.MethodBase.GetCurrentMethod().Name + " in");
            }

            string returnSetting = String.Empty;

            try
            {
                const string commandText = "SELECT _VALUE FROM SETTINGS WHERE _KEY = @key";

                if (_compactDatabaseType)
                {
                    using (SqlCeConnection conn = DatabaseConnection.CreateOpenCEConnection())
                    {
                        using (SqlCeCommand command = new SqlCeCommand(commandText, conn))
                        {
                            command.Parameters.AddWithValue("@key", key);

                            object result = command.ExecuteScalar();
                            if ((result != null) && (result.GetType() != typeof(DBNull)))
                            {
                                string resultValue = Convert.ToString(result);
                                returnSetting = (decrypt) ? AES.Decrypt(resultValue) : resultValue;
                            }
                        }
                    }
                }
                else
                {
                    using (SqlConnection conn = DatabaseConnection.CreateOpenStandardConnection())
                    {
                        using (SqlCommand command = new SqlCommand(commandText, conn))
                        {
                            command.Parameters.AddWithValue("@key", key);

                            object result = command.ExecuteScalar();
                            if ((result != null) && (result.GetType() != typeof(DBNull)))
                            {
                                string resultValue = Convert.ToString(result);
                                returnSetting = (decrypt) ? AES.Decrypt(resultValue) : resultValue;
                            }
                        }
                    }
                }
            }
            catch (SqlException ex)
            {
                Utility.DisplayErrorMessage("A database error has occurred in AuditWizard." + Environment.NewLine + Environment.NewLine +
                                            "Please see the log file for further details.");
                Logger.Error("Exception in " + System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
            }
            catch (SqlCeException ex)
            {
                Utility.DisplayErrorMessage("A database error has occurred in AuditWizard." + Environment.NewLine + Environment.NewLine +
                                            "Please see the log file for further details.");
                Logger.Error("Exception in " + System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
            }
            catch (Exception ex)
            {
                Utility.DisplayErrorMessage("A database error has occurred in AuditWizard." + Environment.NewLine + Environment.NewLine +
                                            "Please see the log file for further details.");

                Logger.Error("Exception in " + System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
            }

            if (IsDebugEnabled)
            {
                Logger.Debug(System.Reflection.MethodBase.GetCurrentMethod().Name + " out with setting : " + returnSetting);
            }
            return(returnSetting);
        }
Ejemplo n.º 2
0
        public void SetSetting(string key, string value, bool encrypt)
        {
            if (IsDebugEnabled)
            {
                Logger.Debug(System.Reflection.MethodBase.GetCurrentMethod().Name + " in");
            }

            if (value == null)
            {
                value = String.Empty;
            }

            int lCount = 0;

            try
            {
                string commandText = "SELECT COUNT(*) FROM SETTINGS WHERE _KEY = @key";

                if (_compactDatabaseType)
                {
                    using (SqlCeConnection conn = DatabaseConnection.CreateOpenCEConnection())
                    {
                        using (SqlCeCommand command = new SqlCeCommand(commandText, conn))
                        {
                            command.Parameters.AddWithValue("@key", key);
                            lCount = Convert.ToInt32(command.ExecuteScalar());
                        }

                        commandText = (lCount == 0)
                                          ? "INSERT INTO SETTINGS (_KEY,_VALUE) VALUES (@key ,@value)"
                                          : "UPDATE SETTINGS SET _VALUE = @value WHERE _KEY = @key";

                        using (SqlCeCommand command = new SqlCeCommand(commandText, conn))
                        {
                            command.Parameters.AddWithValue("@key", key);
                            command.Parameters.AddWithValue("@value", encrypt ? AES.Encrypt(value) : value);
                            command.ExecuteNonQuery();
                        }
                    }
                }
                else
                {
                    using (SqlConnection conn = DatabaseConnection.CreateOpenStandardConnection())
                    {
                        using (SqlCommand command = new SqlCommand(commandText, conn))
                        {
                            command.Parameters.AddWithValue("@key", key);
                            lCount = Convert.ToInt32(command.ExecuteScalar());
                        }

                        commandText = (lCount == 0)
                                          ? "INSERT INTO SETTINGS (_KEY,_VALUE) VALUES (@key ,@value)"
                                          : "UPDATE SETTINGS SET _VALUE = @value WHERE _KEY = @key";

                        using (SqlCommand command = new SqlCommand(commandText, conn))
                        {
                            command.Parameters.AddWithValue("@key", key);
                            command.Parameters.AddWithValue("@value", encrypt ? AES.Encrypt(value) : value);
                            command.ExecuteNonQuery();
                        }
                    }
                }
            }
            catch (SqlException ex)
            {
                Utility.DisplayErrorMessage("A database error has occurred in AuditWizard." + Environment.NewLine + Environment.NewLine +
                                            "Please see the log file for further details.");
                Logger.Error("Exception in " + System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
            }
            catch (SqlCeException ex)
            {
                Utility.DisplayErrorMessage("A database error has occurred in AuditWizard." + Environment.NewLine + Environment.NewLine +
                                            "Please see the log file for further details.");
                Logger.Error("Exception in " + System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
            }
            catch (Exception ex)
            {
                Utility.DisplayErrorMessage("A database error has occurred in AuditWizard." + Environment.NewLine + Environment.NewLine +
                                            "Please see the log file for further details.");

                Logger.Error("Exception in " + System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
            }


            if (IsDebugEnabled)
            {
                Logger.Debug(System.Reflection.MethodBase.GetCurrentMethod().Name + " out");
            }
        }