关于C#的数据源的转化
标签:tox iter row 转换 span info public dataset except
1 public class DataHelper
2 {
3 #region IList如何转成List 4 ///
5 /// IList如何转成List
6 ///
7 ///
8 ///
9 ///
10 public static List IListToList(IList list)
11 {
12 T[] array = new T[list.Count];
13 list.CopyTo(array, 0);
14 return new List(array);
15 }
16 #endregion
17
18 #region DataTable根据条件过滤表的内容
19 ///
20 /// 根据条件过滤表的内容
21 ///
22 ///
23 ///
24 ///
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 )
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 )
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 ///
72 /// 检查DataTable 是否有数据行
73 ///
74 /// DataTable
75 ///
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 ///
87 /// DataTable 转 DataTableToHashtable
88 ///
89 ///
90 ///
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 )
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 ///
108 /// 将泛类型集合List类转换成DataTable
109 ///
110 /// 泛类型集合
111 ///
112 public static DataTable ListToDataTable(List 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 )
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 )
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 ///
152 /// DataTable 转 XML
153 ///
154 ///
155 ///
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 ///
170 /// DataSet 转 XML
171 ///
172 ///
173 ///
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 ///
188 /// DataRow 转 HashTable
189 ///
190 ///
191 ///
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
评论