예제 #1
0
        /// <summary>
        /// A variation of DbCommand.ExecuteScalar() that allows you to specify that a decrypted value of the specified type will be returned
        /// </summary>
        /// <typeparam name="T">The type of value to return after decryption</typeparam>
        /// <param name="dbNullHandling">Determines how table column values of NULL are handled</param>
        /// <returns>A decrypted value of the specified type</returns>
        public T ExecuteDecrypt <T>(DbNullHandling dbNullHandling = DbNullHandling.ThrowDbNullException)
        {
            T result = default(T);

            if (_cryptEngine == null)
            {
                throw new Exception(NO_CRYPT_ENGINE);
            }
            using (SqliteDataReader reader = ExecuteReader(CommandBehavior.SingleRow | CommandBehavior.SingleResult)) {
                if (reader.Read())
                {
                    if (reader.IsDBNull(0))
                    {
                        if (dbNullHandling == DbNullHandling.ThrowDbNullException)
                        {
                            throw new DbNullException();
                        }
                    }
                    else
                    {
                        result = _cryptEngine.DecryptObject <T>(reader[0].ToString());
                    }
                }
            }
            return(result);
        }
예제 #2
0
        /// <summary>
        /// A variation of DbCommand.ExecuteScalar() that allows you to specify the Type of the returned value
        /// </summary>
        /// <typeparam name="T">The type of value to return</typeparam>
        /// <param name="dbNullHandling">Determines how table column values of NULL are handled</param>
        /// <returns>A value of the specified type</returns>
        public T ExecuteScalar <T>(DbNullHandling dbNullHandling = DbNullHandling.ThrowDbNullException)
        {
            T result = default(T);

            using (SqliteDataReader reader = ExecuteReader(CommandBehavior.SingleRow | CommandBehavior.SingleResult)) {
                if (reader.Read())
                {
                    if (reader.IsDBNull(0))
                    {
                        if (dbNullHandling == DbNullHandling.ThrowDbNullException)
                        {
                            throw new DbNullException();
                        }
                    }
                    else
                    {
                        result = (T)Convert.ChangeType(reader[0], typeof(T));
                    }
                }
            }
            return(result);
        }