データベース読み込みデータ支援クラスDataHelperIoT


データベースを整理してデータを読み込みます。
1.データコードを元に読み込む:  
	int tempInt;
	if (int.TryParse(dataReader[this.DBInt].ToString(), out tempInt))
	{
		model.objInt= tempInt;
	}
intタイプを例にとると、他のタイプは類似しています。このように書くと毎回多くの変換コードが重複します。
2.DataHelperIoT補助類コードを使用する:
            
	DataHelperIoT DataHelper = DataHelperIoT.CreateInstance(dataReader);
	model.objInt= DataHelper.ReadInt32(this.DBInt);
         
	model.objInt= DataHelperIoT.ReadInt32(dataReader,this.DBInt);
以上は二つの呼び出し方法を選択できます。
 
DataHelperIoTソースコード:
    /// <summary>
    ///           
    /// </summary>
    public class DataHelperIoT
    {
        private IDataReader _iDataReader;
        private static DataHelperIoT _dataHelperIoT;
        public DataHelperIoT() { }
        public DataHelperIoT(IDataReader reader)
        {
            this._iDataReader = reader;
        }
        public IDataReader ThisDataReader {
            get { return this._iDataReader; }
            set { this._iDataReader = value; }
        }

        public static DataHelperIoT CreateInstance(IDataReader reader) {
            _dataHelperIoT = new DataHelperIoT(reader);
            return _dataHelperIoT;
        }
        #region     reader
        /// <summary>
        ///  reader   guid
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public Guid ReadGuid(string columnName)
        {
            return ReadGuid(columnName, Guid.Empty);
        }
        /// <summary>
        ///  reader   guid
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public Guid ReadGuid(string columnName, Guid defaultValue)
        {
            try
            {
                if (ThisDataReader[columnName] != DBNull.Value)
                    return (Guid)ThisDataReader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }


        /// <summary>
        /// reader    Int32    
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <returns></returns>
        public int ReadInt32(string columnName)
        {
            return ReadInt32(columnName, 0);
        }

        /// <summary>
        ///  DataReader       ,     ,      
        /// </summary>
        /// <param name="reader">DataReader</param>
        /// <param name="columnName">  </param>
        /// <param name="defaultValue">   </param>
        /// <returns>  </returns>
        public int ReadInt32(string columnName, int defaultValue)
        {
            try
            {
                if (ThisDataReader[columnName] != DBNull.Value)
                    return int.Parse(ThisDataReader[columnName].ToString());
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// DataReader        ,     ,      
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <returns>  </returns>
        public long ReadInt64(string columnName)
        {
            return ReadInt64(columnName, 0L);
        }

        /// <summary>
        ///  DataReader        ,     ,              
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <param name="defaultValue">   </param>
        /// <returns></returns>
        public long ReadInt64( string columnName, long defaultValue)
        {
            try
            {
                if (ThisDataReader[columnName] != DBNull.Value)
                    return (long)ThisDataReader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }
        /// <summary>
        ///  DataReader   decimal  ,     ,      
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public decimal ReadDecimal( string columnName)
        {
            return ReadDecimal(columnName, 0);
        }
        /// <summary>
        ///  DataReader   decimal  ,     ,          
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public  decimal ReadDecimal(string columnName, decimal defaultValue)
        {
            try
            {
                if (ThisDataReader[columnName] != DBNull.Value)
                    return (decimal)ThisDataReader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }
        /// <summary>
        /// DataReader       ,     ,              
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <returns>  </returns>
        public bool ReadBoolean(string columnName)
        {
            return ReadBoolean(columnName, false);
        }

        /// <summary>
        /// DataReader       ,     ,      
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public bool ReadBoolean(string columnName, bool defaultValue)
        {
            try
            {
                if (ThisDataReader[columnName] != DBNull.Value)
                    return (bool)ThisDataReader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }

        /// <summary>
        ///  DataReader     int  ,     ,             
        /// </summary>
        /// <param name="reader">reader.</param>
        /// <param name="columnName">  </param>
        /// <returns></returns>
        public int? ReadNullableInt(string columnName)
        {
            try
            {
                if (ThisDataReader[columnName] != DBNull.Value)
                    return int.Parse(ThisDataReader[columnName].ToString());
                else
                    return null;
            }
            catch (IndexOutOfRangeException)
            {
                return null;
            }
        }

        /// <summary>
        ///      
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public string ReadString(string columnName)
        {
            return ReadString(columnName, string.Empty);
        }

        /// <summary>
        ///      
        /// </summary>
        /// <param name="reader">reader</param>
        /// <param name="columnName">  </param>
        /// <param name="defaultValue">   </param>
        /// <returns></returns>
        public string ReadString(string columnName, string defaultValue)
        {
            try
            {
                if (ThisDataReader[columnName] != DBNull.Value)
                    return (string)ThisDataReader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }

        /// <summary>
        ///   IP    
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <returns></returns>
        public IPAddress ReadIpAddress(string columnName)
        {
            try
            {
                if (ThisDataReader[columnName] != DBNull.Value)
                    return IPAddress.Parse((string)ThisDataReader[columnName]);
                else
                    return IPAddress.None;
            }
            catch (System.FormatException)
            {
                return IPAddress.None;
            }
            catch (IndexOutOfRangeException)
            {
                return IPAddress.None;
            }
        }

        /// <summary>
        ///  DataReader   URI  ,     ,      
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public Uri ReadUri(string columnName)
        {
            try
            {
                if (ThisDataReader[columnName] != DBNull.Value)
                    return new Uri((string)ThisDataReader[columnName]);
                else
                    return null;
            }
            catch (System.IndexOutOfRangeException)
            {
                return null;
            }
            catch (System.FormatException)
            {
                return null;
            }
        }

        /// <summary>
        ///  DataReader   DateTime  ,     ,           
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <returns></returns>
        public DateTime ReadDateTime(string columnName)
        {
            return ReadDate(columnName, DateTime.MinValue);
        }

        /// <summary>
        /// DataReader   DateTime  ,     ,      
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <param name="defaultValue">   </param>
        /// <returns></returns>
        public DateTime ReadDate(string columnName, DateTime defaultValue)
        {
            try
            {
                if (ThisDataReader[columnName] != DBNull.Value)
                    return (DateTime)ThisDataReader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }


        /// <summary>
        /// DataReader     DateTime  ,     ,      
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <returns></returns>
        public DateTime? ReadNullableDate(string columnName)
        {
            return ReadNullableDate(columnName, null);
        }

        /// <summary>
        ///  DataReader     DateTime  ,     ,      
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <param name="defaultValue">   </param>
        /// <returns></returns>
        public DateTime? ReadNullableDate(string columnName, DateTime? defaultValue)
        {
            try
            {
                if (ThisDataReader[columnName] != DBNull.Value)
                    return (DateTime)ThisDataReader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }
        #endregion

        # region    reader
        /// <summary>
        ///  reader   guid
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public static Guid ReadGuid(IDataReader reader, string columnName)
        {
            return ReadGuid(reader, columnName, Guid.Empty);
        }
        /// <summary>
        ///  reader   guid
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static Guid ReadGuid(IDataReader reader, string columnName, Guid defaultValue)
        {
            try
            {
                if (reader[columnName] != DBNull.Value)
                    return (Guid)reader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }


        /// <summary>
        /// reader    Int32    
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <returns></returns>
        public static int ReadInt32(IDataReader reader, string columnName)
        {
            return ReadInt32(reader, columnName, 0);
        }

        /// <summary>
        ///  DataReader       ,     ,      
        /// </summary>
        /// <param name="reader">DataReader</param>
        /// <param name="columnName">  </param>
        /// <param name="defaultValue">   </param>
        /// <returns>  </returns>
        public static int ReadInt32(IDataReader reader, string columnName, int defaultValue)
        {
            try
            {
                if (reader[columnName] != DBNull.Value)
                    return int.Parse(reader[columnName].ToString());
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// DataReader        ,     ,      
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <returns>  </returns>
        public static long ReadInt64(IDataReader reader, string columnName)
        {
            return ReadInt64(reader, columnName, 0L);
        }

        /// <summary>
        ///  DataReader        ,     ,              
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <param name="defaultValue">   </param>
        /// <returns></returns>
        public static long ReadInt64(IDataReader reader, string columnName, long defaultValue)
        {
            try
            {
                if (reader[columnName] != DBNull.Value)
                    return (long)reader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }
        /// <summary>
        ///  DataReader   decimal  ,     ,      
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public static decimal ReadDecimal(IDataReader reader, string columnName)
        {
            return ReadDecimal(reader, columnName, 0);
        }
        /// <summary>
        ///  DataReader   decimal  ,     ,          
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static decimal ReadDecimal(IDataReader reader, string columnName, decimal defaultValue)
        {
            try
            {
                if (reader[columnName] != DBNull.Value)
                    return (decimal)reader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }
        /// <summary>
        /// DataReader       ,     ,              
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <returns>  </returns>
        public static bool ReadBoolean(IDataReader reader, string columnName)
        {
            return ReadBoolean(reader, columnName, false);
        }

        /// <summary>
        /// DataReader       ,     ,      
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool ReadBoolean(IDataReader reader, string columnName, bool defaultValue)
        {
            try
            {
                if (reader[columnName] != DBNull.Value)
                    return (bool)reader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }

        /// <summary>
        ///  DataReader     int  ,     ,             
        /// </summary>
        /// <param name="reader">reader.</param>
        /// <param name="columnName">  </param>
        /// <returns></returns>
        public static int? ReadNullableInt(IDataReader reader, string columnName)
        {
            try
            {
                if (reader[columnName] != DBNull.Value)
                    return int.Parse(reader[columnName].ToString());
                else
                    return null;
            }
            catch (IndexOutOfRangeException)
            {
                return null;
            }
        }

        /// <summary>
        ///      
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public static string ReadString(IDataReader reader, string columnName)
        {
            return ReadString(reader, columnName, string.Empty);
        }

        /// <summary>
        ///      
        /// </summary>
        /// <param name="reader">reader</param>
        /// <param name="columnName">  </param>
        /// <param name="defaultValue">   </param>
        /// <returns></returns>
        public static string ReadString(IDataReader reader, string columnName, string defaultValue)
        {
            try
            {
                if (reader[columnName] != DBNull.Value)
                    return (string)reader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }

        /// <summary>
        ///   IP    
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <returns></returns>
        public static IPAddress ReadIpAddress(IDataReader reader, string columnName)
        {
            try
            {
                if (reader[columnName] != DBNull.Value)
                    return IPAddress.Parse((string)reader[columnName]);
                else
                    return IPAddress.None;
            }
            catch (System.FormatException)
            {
                return IPAddress.None;
            }
            catch (IndexOutOfRangeException)
            {
                return IPAddress.None;
            }
        }

        /// <summary>
        ///  DataReader   URI  ,     ,      
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public static Uri ReadUri(IDataReader reader, string columnName)
        {
            try
            {
                if (reader[columnName] != DBNull.Value)
                    return new Uri((string)reader[columnName]);
                else
                    return null;
            }
            catch (System.IndexOutOfRangeException)
            {
                return null;
            }
            catch (System.FormatException)
            {
                return null;
            }
        }

        /// <summary>
        ///  DataReader   DateTime  ,     ,           
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <returns></returns>
        public static DateTime ReadDateTime(IDataReader reader, string columnName)
        {
            return ReadDate(reader, columnName, DateTime.MinValue);
        }

        /// <summary>
        /// DataReader   DateTime  ,     ,      
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <param name="defaultValue">   </param>
        /// <returns></returns>
        public static DateTime ReadDate(IDataReader reader, string columnName, DateTime defaultValue)
        {
            try
            {
                if (reader[columnName] != DBNull.Value)
                    return (DateTime)reader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }


        /// <summary>
        /// DataReader     DateTime  ,     ,      
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <returns></returns>
        public static DateTime? ReadNullableDate(IDataReader reader, string columnName)
        {
            return ReadNullableDate(reader, columnName, null);
        }

        /// <summary>
        ///  DataReader     DateTime  ,     ,      
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">  </param>
        /// <param name="defaultValue">   </param>
        /// <returns></returns>
        public static DateTime? ReadNullableDate(IDataReader reader, string columnName, DateTime? defaultValue)
        {
            try
            {
                if (reader[columnName] != DBNull.Value)
                    return (DateTime)reader[columnName];
                else
                    return defaultValue;
            }
            catch (IndexOutOfRangeException)
            {
                return defaultValue;
            }
        }
        #endregion

        /// <summary>
        ///       sql  
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string GetSafeSqlDateTimeFormat(DateTime date)
        {
            return date.ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.SortableDateTimePattern);
        }

        /// <summary>
        ///       SQL  
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime GetSafeSqlDateTime(DateTime date)
        {

            if (date < DateTime.MinValue)
                return (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
            else if (date > DateTime.MaxValue)
                return (DateTime)System.Data.SqlTypes.SqlDateTime.MaxValue;
            return date;

        }

        /// <summary>
        ///       SQL  
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static int GetSafeSqlInt(int i)
        {

            if (i <= (int)System.Data.SqlTypes.SqlInt32.MinValue)
                return (int)System.Data.SqlTypes.SqlInt32.MinValue + 1;
            else if (i >= (int)System.Data.SqlTypes.SqlInt32.MaxValue)
                return (int)System.Data.SqlTypes.SqlInt32.MaxValue - 1;
            return i;

        }
        
    }
DataHelper IoTDaHelperIoT