Exemplo n.º 1
0
        /// <summary>
        /// 转Int类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int ToInt(object obj)
        {
            int num;

            try
            {
                if (obj == null)
                {
                    return(-1);
                }
                num = Nall.ToInt(obj.ToString());
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                num = -1;
            }
            return(num);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 数据集DataTable转换成List集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dtTemp"></param>
        /// <returns></returns>
        public static List <T> ToListByDataTable <T>(DataTable dtTemp)
        {
            if (dtTemp == null || dtTemp.Rows.Count == 0)
            {
                return(null);
            }
            List <T> lstResult = new List <T>();

            for (int j = 0, l = dtTemp.Rows.Count; j < l; j++)
            {
                T _t = (T)Activator.CreateInstance(typeof(T));
                PropertyInfo[] propertys = _t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    for (int i = 0, k = dtTemp.Columns.Count; i < k; i++)
                    {
                        // 属性与字段名称一致的进行赋值
                        if (pi.Name.ToLower().Equals(dtTemp.Columns[i].ColumnName.ToLower()))
                        {
                            if (dtTemp.Rows[j][i] != DBNull.Value)
                            {
                                switch (pi.PropertyType.ToString())
                                {
                                case "System.Int32":
                                    pi.SetValue(_t, Nall.ToInt(dtTemp.Rows[j][i].ToString()), null);
                                    break;

                                case "System.Int64":
                                    pi.SetValue(_t, Nall.ToLong(dtTemp.Rows[j][i].ToString()), null);
                                    break;

                                case "System.DateTime":
                                    pi.SetValue(_t, Nall.ToDateTime(dtTemp.Rows[j][i].ToString()), null);
                                    break;

                                case "System.String":
                                    pi.SetValue(_t, dtTemp.Rows[j][i].ToString(), null);
                                    break;

                                case "System.Boolean":
                                    pi.SetValue(_t, Nall.ToBoolean(dtTemp.Rows[j][i].ToString()), null);
                                    break;

                                case "System.Guid":
                                    pi.SetValue(_t, Nall.ToGuid(dtTemp.Rows[j][i].ToString()), null);
                                    break;

                                case "System.Single":
                                    pi.SetValue(_t, Convert.ToSingle(dtTemp.Rows[j][i].ToString()), null);
                                    break;

                                case "System.Double":
                                    pi.SetValue(_t, Convert.ToDouble(dtTemp.Rows[j][i].ToString()), null);
                                    break;

                                case "System.Object":
                                    pi.SetValue(_t, dtTemp.Rows[j][i], null);
                                    break;
                                }
                            }
                            else
                            {
                                switch (pi.PropertyType.ToString())
                                {
                                case "System.Int32":
                                    pi.SetValue(_t, -1, null);
                                    break;

                                case "System.Int64":
                                    pi.SetValue(_t, -1, null);
                                    break;

                                case "System.DateTime":
                                    pi.SetValue(_t, new DateTime(0x76c, 1, 1), null);
                                    break;

                                case "System.Boolean":
                                    pi.SetValue(_t, false, null);
                                    break;

                                case "System.Guid":
                                    pi.SetValue(_t, Guid.Empty, null);
                                    break;

                                case "System.Single":
                                    pi.SetValue(_t, 0.0f, null);
                                    break;

                                case "System.Double":
                                    pi.SetValue(_t, 0.0, null);
                                    break;

                                case "System.String":
                                    pi.SetValue(_t, string.Empty, null);
                                    break;

                                default:
                                    pi.SetValue(_t, null, null);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }

                lstResult.Add(_t);
            }

            return(lstResult);
        }