从DataTable中获取行列的值

/// <summary>
        /// 获取DataRow 中的值
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="dt">数据集</param>
        /// <param name="columnIndex">字段序列号,第一列为0</param>
        /// <returns></returns>
        public static T DataTable<T>(DataRow dr, int columnIndex)
        {
            if (dr == null)
                return default(T);
            try
            {
                if (columnIndex < 0)
                {
                    throw(new Exception("DataTable don't have this column"));
                }
                object val = dr[columnIndex];
                return GetTypeDate<T>(val);
            }
            catch
            {
                return default(T);
            }
        }
        /// <summary>
        /// 获取DataTable 中的值
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="dt">数据集</param>
        /// <param name="rowcount">行号,第一行为0</param>
        /// <param name="columnIndex">字段序列号,第一列为0</param>
        /// <returns></returns>
        public static T DataTable<T>(DataTable dt, int rowcount, int columnIndex)
        {
            if (dt == null || dt.Rows.Count<=0)
                return default(T);
            try
            {
                return DataTable<T>(dt.Rows[rowcount], columnIndex);
            }
            catch
            {
                return default(T);
            }
        }
        public static T DataTable<T>(DataTable dt, int rowcount, string columnName)
        {
            return DataTable<T>(dt, rowcount, dt.Columns.IndexOf(columnName));
        }
        /// <summary>
        /// 获取DataTable 中的值
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="dt">数据集</param>
        /// <param name="columnIndex">字段序列号,第一列为0</param>
        /// <returns></returns>
        public static T DataTable<T>(DataTable dt, int columnIndex)
        {
            return DataTable<T>(dt, 0, columnIndex);
        }
        /// <summary>
        /// 获取DataTable 的值
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="dt">数据集</param>
        /// <param name="columnName">字段名</param>
        /// <returns></returns>
        public static T DataTable<T>(DataTable dt, string columnName)
        {
            return DataTable<T>(dt, dt.Columns.IndexOf(columnName));
        }
        /// <summary>
        /// object 转换成 某种类型的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="val"></param>
        /// <returns></returns>
        public static T GetMyDate<T>(object val,string format="yyyy-MM-dd")
 {
            T cobj;
            Type ctype = typeof(T);
            try
            {
               //cobj = (T)Activator.CreateInstance(ctype);//动态实力化反射回来的指定空间下的指定类
               //cobj = (T)val;
               if(ctype.Name.toLower().equals("datetime"))
              {
                   cobj = (T)(object)DateTime.ParseExact(val,format,null);
              }else
              {
                   cobj = (T)Convert.ChangeType(val,ctype);
              }
            }
            catch
            {
                try
                {
                    cobj = default(T);
                }
                catch
                {
                    throw;
                }
            }
            return cobj;

调用:

int lastcopy = DataTable<int>(dt, "LastCopy");

你可能感兴趣的:(exception,object,String,null)