Exemple #1
0
        public void GetValueOrDefault_NonDbNullInt()
        {
            object target = 123;
            int    actual = DatabaseValueParser.GetValueOrDefault <int>(target);

            Assert.Equal(123, actual);
        }
Exemple #2
0
        public void GetValueOrDefault_DbNullInt()
        {
            object target = DBNull.Value;
            int    actual = DatabaseValueParser.GetValueOrDefault <int>(target);

            Assert.Equal(0, actual);
        }
        /// <summary>
        /// Gets the value of the given field from the data reader or the
        /// default for the object type.
        /// </summary>
        /// <typeparam name="T">Type of object.</typeparam>
        /// <param name="dataRecord">Current IDataRecord object from extension method.</param>
        /// <param name="index">Index of field within IDataRecord.</param>
        /// <param name="defaultValue">Value to use if null field value.</param>
        /// <returns>The given field's value or the given default.</returns>
        public static T GetValueOrDefault <T>(this IDataRecord dataRecord, int index, T defaultValue)
        {
            if (dataRecord == null)
            {
                throw new ArgumentNullException(nameof(dataRecord));
            }

            object value = dataRecord[index];

            return(DatabaseValueParser.GetValueOrDefault(value, defaultValue));
        }
        /// <summary>
        /// Gets the value of the given field from the data reader or the
        /// default for the object type.
        /// </summary>
        /// <typeparam name="T">Type of object.</typeparam>
        /// <param name="dataRecord">Current IDataRecord object from extension method.</param>
        /// <param name="name">Name of field within IDataRecord.</param>
        /// <returns>The given field's value or the default for the object type.</returns>
        public static T GetValueOrDefault <T>(this IDataRecord dataRecord, string name)
        {
            if (dataRecord == null)
            {
                throw new ArgumentNullException(nameof(dataRecord));
            }

            object value = dataRecord[name];

            return(DatabaseValueParser.GetValueOrDefault <T>(value));
        }