码迷,mamicode.com
首页 > Windows程序 > 详细

关于C#的数据源的转化

时间:2019-04-09 12:38:42      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:tox   iter   row   转换   span   info   public   dataset   except   

  1     public class DataHelper
  2     {
  3         #region IList如何转成List<T>
  4         /// <summary>
  5         /// IList如何转成List<T>
  6         /// </summary>
  7         /// <typeparam name="T"></typeparam>
  8         /// <param name="list"></param>
  9         /// <returns></returns>
 10         public static List<T> IListToList<T>(IList list)
 11         {
 12             T[] array = new T[list.Count];
 13             list.CopyTo(array, 0);
 14             return new List<T>(array);
 15         }
 16         #endregion
 17 
 18         #region DataTable根据条件过滤表的内容
 19         /// <summary>
 20         /// 根据条件过滤表的内容
 21         /// </summary>
 22         /// <param name="dt"></param>
 23         /// <param name="condition"></param>
 24         /// <returns></returns>
 25         public static DataTable DataFilter(DataTable dt, string condition)
 26         {
 27             if (DataHelper.IsExistRows(dt))
 28             {
 29                 if (condition.Trim() == "")
 30                 {
 31                     return dt;
 32                 }
 33                 else
 34                 {
 35                     DataTable newdt = new DataTable();
 36                     newdt = dt.Clone();
 37                     DataRow[] dr = dt.Select(condition);
 38                     for (int i = 0; i < dr.Length; i++)
 39                     {
 40                         newdt.ImportRow((DataRow)dr[i]);
 41                     }
 42                     return newdt;
 43                 }
 44             }
 45             else
 46             {
 47                 return null;
 48             }
 49         }
 50         public static DataTable DataFilter(DataTable dt, string condition, string sort)
 51         {
 52             if (DataHelper.IsExistRows(dt))
 53             {
 54                 DataTable newdt = new DataTable();
 55                 newdt = dt.Clone();
 56                 DataRow[] dr = dt.Select(condition, sort);
 57                 for (int i = 0; i < dr.Length; i++)
 58                 {
 59                     newdt.ImportRow((DataRow)dr[i]);
 60                 }
 61                 return newdt;
 62             }
 63             else
 64             {
 65                 return null;
 66             }
 67         }
 68         #endregion
 69 
 70         #region 检查DataTable 是否有数据行
 71         /// <summary>
 72         /// 检查DataTable 是否有数据行
 73         /// </summary>
 74         /// <param name="dt">DataTable</param>
 75         /// <returns></returns>
 76         public static bool IsExistRows(DataTable dt)
 77         {
 78             if (dt != null && dt.Rows.Count > 0)
 79                 return true;
 80 
 81             return false;
 82         }
 83         #endregion
 84 
 85         #region DataTable 转 DataTableToHashtable
 86         /// <summary>
 87         /// DataTable 转 DataTableToHashtable
 88         /// </summary>
 89         /// <param name="dt"></param>
 90         /// <returns></returns>
 91         public static Hashtable DataTableToHashtable(DataTable dt)
 92         {
 93             Hashtable ht = new Hashtable();
 94             foreach (DataRow dr in dt.Rows)
 95             {
 96                 for (int i = 0; i < dt.Columns.Count; i++)
 97                 {
 98                     string key = dt.Columns[i].ColumnName;
 99                     ht[key] = dr[key];
100                 }
101             }
102             return ht;
103         }
104         #endregion
105 
106         #region List转换DataTable
107         /// <summary>
108         /// 将泛类型集合List类转换成DataTable
109         /// </summary>
110         /// <param name="list">泛类型集合</param>
111         /// <returns></returns>
112         public static DataTable ListToDataTable<T>(List<T> entitys)
113         {
114             //检查实体集合不能为空
115             if (entitys == null || entitys.Count < 1)
116             {
117                 throw new Exception("需转换的集合为空");
118             }
119             //取出第一个实体的所有Propertie
120             Type entityType = entitys[0].GetType();
121             PropertyInfo[] entityProperties = entityType.GetProperties();
122 
123             //生成DataTable的structure
124             //生产代码中,应将生成的DataTable结构Cache起来,此处略
125             DataTable dt = new DataTable();
126             for (int i = 0; i < entityProperties.Length; i++)
127             {
128                 //dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType);
129                 dt.Columns.Add(entityProperties[i].Name);
130             }
131             //将所有entity添加到DataTable中
132             foreach (object entity in entitys)
133             {
134                 //检查所有的的实体都为同一类型
135                 if (entity.GetType() != entityType)
136                 {
137                     throw new Exception("要转换的集合元素类型不一致");
138                 }
139                 object[] entityValues = new object[entityProperties.Length];
140                 for (int i = 0; i < entityProperties.Length; i++)
141                 {
142                     entityValues[i] = entityProperties[i].GetValue(entity, null);
143                 }
144                 dt.Rows.Add(entityValues);
145             }
146             return dt;
147         }
148         #endregion
149 
150         #region DataTable/DataSet 转 XML
151         /// <summary>
152         /// DataTable 转 XML
153         /// </summary>
154         /// <param name="dt"></param>
155         /// <returns></returns>
156         public static string DataTableToXML(DataTable dt)
157         {
158             if (dt != null)
159             {
160                 if (dt.Rows.Count > 0)
161                 {
162                     System.IO.StringWriter writer = new System.IO.StringWriter();
163                     dt.WriteXml(writer);
164                     return writer.ToString();
165                 }
166             }
167             return String.Empty;
168         }
169         /// <summary>
170         /// DataSet 转 XML
171         /// </summary>
172         /// <param name="ds"></param>
173         /// <returns></returns>
174         public static string DataSetToXML(DataSet ds)
175         {
176             if (ds != null)
177             {
178                 System.IO.StringWriter writer = new System.IO.StringWriter();
179                 ds.WriteXml(writer);
180                 return writer.ToString();
181             }
182             return String.Empty;
183         }
184         #endregion
185 
186         #region DataRow  转  HashTable
187         /// <summary>
188         /// DataRow  转  HashTable
189         /// </summary>
190         /// <param name="dr"></param>
191         /// <returns></returns>
192         public static Hashtable DataRowToHashTable(DataRow dr)
193         {
194             Hashtable htReturn = new Hashtable(dr.ItemArray.Length);
195             foreach (DataColumn dc in dr.Table.Columns)
196                 htReturn.Add(dc.ColumnName, dr[dc.ColumnName]);
197             return htReturn;
198         }
199         #endregion
200     }

 

关于C#的数据源的转化

标签:tox   iter   row   转换   span   info   public   dataset   except   

原文地址:https://www.cnblogs.com/taigasweet/p/10676117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!