CSharp examples for System.Data:DataReader
Try Get Value from IDataReader
using System.Data; using System.ComponentModel; using System;/*w w w . j a v a 2 s . co m*/ public class Main{ private static T TryGetValue<T>(this IDataReader dataReader, int ordinal) { if (ordinal < 0 || ordinal >= dataReader.FieldCount) return default(T); if (dataReader.IsDBNull(ordinal)) return default(T); else { TypeConverter tc = TypeDescriptor.GetConverter(typeof(T)); try { if (typeof(IConvertible).IsAssignableFrom(typeof(T))) return (T)tc.ConvertFrom(dataReader[ordinal].ToString()); else if (tc.CanConvertTo(typeof(string))) return (T)tc.ConvertFrom(dataReader[ordinal].ToString()); else return (T)dataReader[ordinal]; } catch (FormatException ex) { throw ex; } catch (Exception ex) { throw ex; } } } public static T TryGetValue<T>(this IDataReader dataReader, string keyName) { try { int ordinal = dataReader.GetOrdinal(keyName); return dataReader.TryGetValue<T>(ordinal); } catch (IndexOutOfRangeException ex) { throw ex; } } }