一个实例化的DATATABLE的类

来源:互联网 发布:网络的发展 编辑:程序博客网 时间:2024/04/28 09:36

silverlight调用webservice,关键是调用类的实体。而原先编程基于dataSet的模式。因此必要提出了dataset与实体之间的转化方法:

       List<灌溉计划> jh = new List<灌溉计划>();
        string sql = "select * from [灌溉计划]";
        DataSet ds = dm.getsql(sql);
        jh=USTC.Func.ConvertDataTableToEntityCollections<灌溉计划>(ds.Tables[0]);//调用下面方法即可
        return jh.ToArray();

其中的灌溉计划是数据库表的实体类,可以通过直接建立linq的表实体,拖曳而成型

 

using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

namespace USTC
{
    /// <summary>
    /// 数据服务基类对象
    /// </summary>
    public class DataServiceBase
    {
        DM dm = new DM();
        #region Tabel 类型转换相关
        /// <summary>
        /// 转化Table成一个相对应的实体类集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="table"></param>
        /// <returns></returns>
        public  List<T> ConvertDataTableToEntityCollections<T>(DataTable table)
            where T : class, new()
        {
            if (table != null)
            {
                Type tc =  typeof(T);
                List<T> list = new List<T>(table.Rows.Count);

                foreach (DataRow row in table.Rows)
                {
                    T t = new T();
                    foreach (PropertyInfo pInfo in tc.GetProperties())
                    {
                        if (table.Columns.Contains(pInfo.Name))
                        {
                            //HACK: 对象转换成指定的类型使用 Convert.ChangeType
                            // Convert.ChangeType(row[pInfo.Name], pInfo.PropertyType);
                            try
                            {
                                pInfo.SetValue(t, ChangeType(row[pInfo.Name], pInfo.PropertyType), null);
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                    }
                    list.Add(t);
                }
                return list;
            }
            return null;
        }
        /// <summary>
        /// 得到指定SQL执行的结果并返回结果的实体类集合对象
        /// 本方法综合了 ConvertDataTableToEntityCollections/ExecuteSQL 方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">待执行的SQL</param>
        /// <returns></returns>
        public  List<T> GetEntityConllectionsFromSQL<T>(string sql)
            where T : class, new()
        {
            if (string.IsNullOrEmpty(sql))
                return null;
            else
                return ConvertDataTableToEntityCollections<T>(dm.getsql(sql).Tables[0]);
        }
        /// <summary>
        /// 得到指定类型公开的属性
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private  PropertyInfo[] GetTypePropertyInfo(Type type)
        {
            PropertyInfo[] infos = null;
            //锁定防止 多次 ADD 进缓存
            lock (_PROPERTYINFO_READ_LOCK)
            {
                _PropertyInfoCache.TryGetValue(type, out infos);
                if (infos == null)
                {
                    infos = type.GetProperties();
                    _PropertyInfoCache.Add(type, infos);
                }
            }
            return infos;
        }
        /// <summary>
        /// 类型转换
        /// FROM: http://www.cnblogs.com/cnee5/archive/2006/05/21/405403.html
        /// </summary>
        /// <param name="value"></param>
        /// <param name="conversionType"></param>
        /// <returns></returns>
        protected   object ChangeType(object value, Type conversionType)
        {
            //INFO 对DBNull类型特殊处理
            if (Convert.IsDBNull(value))
            {
                //非可空类型的值类型处理
                if (!(conversionType.IsGenericType &&
                    conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))))
                {
                    if (!conversionType.IsValueType)
                        return null;
                    else
                        return Activator.CreateInstance(conversionType);
                }
                else
                    return null;
            }
            //可空类型类型处理
            if (conversionType.IsGenericType &&
                    conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
            {
                if (value == null)
                    return null;

                System.ComponentModel.NullableConverter nullableConverter
                    = new System.ComponentModel.NullableConverter(conversionType);

                conversionType = nullableConverter.UnderlyingType;
            }

            return Convert.ChangeType(value, conversionType);
        }
        private static object _PROPERTYINFO_READ_LOCK = new object();
        /// <summary>
        /// 实体类属性缓存
        /// </summary>
        private static Dictionary<Type, PropertyInfo[]> _PropertyInfoCache = new Dictionary<Type, PropertyInfo[]>();
        #endregion
    }
}

原创粉丝点击