DM实体转化

这是一个关于数据服务基类的对象,包含对Oracle数据库的操作方法,如执行SQL获取DataTable,将DataTable转换为实体类集合。代码中实现了将数据库查询结果转换为自定义实体类列表,并处理类型转换的细节,如DBNull值的处理和可空类型的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

namespace Syit.AHSZY.Mobile.DataServices
{
    ///


    /// 数据服务基类对象
    ///

    public  class DataServiceBase
    {
        #region DB 处理
       static USTC.OracleDbc db;
        ///
        /// DB
        ///

        private static USTC.OracleDbc Db
        {
            get
            {
                if (db == null)
                {
                    DM dm = new DM();
                    db = dm.getDataBase();
                }
                return db;
            }
        }
        ///
        /// 执行SQL字符串返回一个DataTable
        ///

        ///
        ///
        protected   static DataTable ExecuteSQL(string sql)
        {
            Db.open();
            DataSet ds = (DataSet)Db.getData(sql, false);
            Db.close();
            if (ds != null && ds.Tables.Count != 0)
                return ds.Tables[0];
            else
                return null;
        }
        #endregion
        #region Tabel 类型转换相关
        ///
        /// 转化Table成一个相对应的实体类集合
        ///

        ///
        ///
        ///
        protected static List ConvertDataTableToEntityCollections (DataTable table)
            where T : class, new()
        {
            if (table != null)
            {
                PropertyInfo[] pInfos = GetTypePropertyInfo(typeof(T));
                List list = new List (table.Rows.Count);

                foreach (DataRow row in table.Rows)
                {
                    T t = new T();
                    foreach (PropertyInfo pInfo in pInfos)
                    {
                        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;
        }
        ///


        /// 得到指定SQL执行的结果并返回结果的实体类集合对象
        /// 本方法综合了 ConvertDataTableToEntityCollections/ExecuteSQL 方法
        ///

        ///
        /// 待执行的SQL
        ///
        protected static List GetEntityConllectionsFromSQL (string sql)
            where T : class, new()
        {
            if (string.IsNullOrEmpty(sql))
                return null;
            else
                return ConvertDataTableToEntityCollections (ExecuteSQL(sql));
        }
        ///
        /// 得到指定类型公开的属性
        ///

        ///
        ///
        private static 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;
        }
        ///
        /// 类型转换
        /// FROM: http://www.cnblogs.com/cnee5/archive/2006/05/21/405403.html
        ///

        ///
        ///
        ///
        public static 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();
        ///


        /// 实体类属性缓存
        ///

        private static Dictionary _PropertyInfoCache = new Dictionary ();
        #endregion
    }
}

===============================================================以上代码,工作的目的是代替

        dataSetXML.DataSet1TableAdapters.Mytool dt = new dataSetXML.DataSet1TableAdapters.Mytool();
         DataSet1._MsgDataTable dt1 = dt.GetData();
         this.dataGridView1.DataSource = dt1;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值