VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C# 常用工具方法之DataTable(一)

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

1、DataTable 转 泛型T的List

复制代码
        /// <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;
        }
复制代码

2、DataTable 转 HashTable

复制代码
        /// <summary>
        /// DataTable转HashTable
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static Hashtable ToHashtableByDataRow(DataTable dt, int key)
        {
            Hashtable ht = new Hashtable();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                ht.Add(dt.Columns[i].ColumnName, dt.Rows[key][i]);
            }
            return ht;
        }
复制代码

3、DataTable 转 HashTable

复制代码
        /// <summary>
        ///  转换哈希表

        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<Hashtable> ToHashtableByDataTable<T>(DataTable dt)
        {
            List<Hashtable> listht = new List<Hashtable>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Hashtable ht = new Hashtable();
                ht.Add(i, dt.Rows[i]);
                listht.Add(ht);
            }
            return listht;
        }
复制代码

4、DataTable 按照某列进行条件拆分

复制代码
        /// <summary>
        /// 拆分Dt,返回相同数据结构的多个Dt
        /// [Excel需要去掉第一行标题]
        /// </summary>
        /// <param name="dt">原始数据Dt</param>
        /// <param name="condition">第几列数据,按照这一列进行拆分</param>
        /// <param name="orderstr">排序条件,DataView的排序条件</param>
        /// <returns></returns>
        public static List<DataTable> SplitDtSameStruct(DataTable dt,int columnnum,string orderstr) {
            //定义返回对象
            List<DataTable> listDt = new List<DataTable>();
            //按照条件进行筛选
            List<string> conditionstr = new List<string>();
            foreach (DataRow dr in dt.Rows)
            {
                //筛选字段的值
                string key = dr["Column"+ columnnum].ToString();
                //不存在新增进条件conditionstr中
                if (!isExit(conditionstr,key)) {
                    conditionstr.Add(key);
                }
            }
            //根据条件conditionstr集合进行拆分dt
            //1、将dt数据源按照字段排序
            dt.Columns.Add();
            DataView view = new DataView(dt);
            //正序排列
            view.Sort = orderstr;//"Column"+ columnnum + " ASC";
            dt = view.ToTable();
            foreach (string str in conditionstr)
            {
                //按照条件进行抽取数据
                DataRow[] dr = dt.Select("Column"+ columnnum + "='"+str+"'");
                //抽出的数据存入临时的tempdt中
                DataTable tempdt = new DataTable();
                tempdt = dt.Clone();//拷贝框架
                for (int i = 0; i < dr.Length; i++)
                {
                    tempdt.ImportRow((DataRow)dr[i]);
                }
                //tempdt存入返回对象listDt中
                listDt.Add(tempdt);
            }
            return listDt;
        }
复制代码

日常工作常遇到的utils中积累的代码,可以优化的地方希望多多指教!

相关教程