MDataTable.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using Ant.ORM.SQL;
  8. using System.Reflection;
  9. using Ant.ORM.Tool;
  10. namespace Ant.ORM.Table
  11. {
  12. /// <summary>
  13. /// 表格
  14. /// </summary>
  15. public class MDataTable : IDataReader, IEnumerable,System.ComponentModel.IListSource
  16. {
  17. /// <summary>
  18. /// 从DataReader隐式转换成MDataTable
  19. /// </summary>
  20. public static implicit operator MDataTable(System.Data.Common.DbDataReader sdr)
  21. {
  22. MDataTable mTable = new MDataTable("default");
  23. if (sdr != null)
  24. {
  25. CellStruct mStruct;
  26. MDataRow mRecord = new MDataRow();
  27. int columnState = 0;
  28. while (sdr.Read())
  29. {
  30. for (int i = 0; i < sdr.FieldCount; i++)
  31. {
  32. if (columnState == 0)
  33. {
  34. mStruct = new CellStruct(sdr.GetName(i), DataType.GetSqlType(sdr.GetFieldType(i)), false, true, -1, ParameterDirection.InputOutput);
  35. MDataCell mdc = new MDataCell(ref mStruct, sdr.GetValue(i));
  36. mRecord.Add(mdc);
  37. mTable.Columns.Add(mStruct);
  38. }
  39. if (sdr[mRecord[i]._CellStruct.ColumnName] == null || sdr[mRecord[i]._CellStruct.ColumnName].ToString() == string.Empty)
  40. {
  41. mRecord[i].Value = DBNull.Value;
  42. }
  43. else
  44. {
  45. mRecord[i].Value = sdr[mRecord[i]._CellStruct.ColumnName]; //sdr.GetValue(i);
  46. }
  47. }
  48. columnState++;
  49. mTable.Rows.Add(mRecord.Clone());
  50. }
  51. mRecord.Clear();
  52. mRecord = null;
  53. sdr.Close();
  54. sdr.Dispose();
  55. sdr = null;
  56. }
  57. return mTable;
  58. }
  59. #region 属性
  60. private List<MDataRow> _Mdr;
  61. public List<MDataRow> Rows
  62. {
  63. get
  64. {
  65. return _Mdr;
  66. }
  67. }
  68. public MDataTable()
  69. {
  70. Init("default");
  71. }
  72. public MDataTable(string tableName)
  73. {
  74. Init(tableName);
  75. }
  76. private void Init(string tableName)
  77. {
  78. _Mdr = new List<MDataRow>();
  79. _TableName = tableName;
  80. if (_Columns == null)
  81. {
  82. _Columns = new MDataColumn(this);
  83. }
  84. }
  85. private string _TableName = string.Empty;
  86. public string TableName
  87. {
  88. get
  89. {
  90. return _TableName;
  91. }
  92. set
  93. {
  94. _TableName = value;
  95. }
  96. }
  97. private MDataColumn _Columns;
  98. public MDataColumn Columns
  99. {
  100. get
  101. {
  102. //if (_Mdr != null && _Mdr.Count > 0)
  103. //{
  104. // _Columns = _Mdr[0].Columns;
  105. //}
  106. return _Columns;
  107. }
  108. }
  109. /// <summary>
  110. /// 新增一列
  111. /// </summary>
  112. /// <returns></returns>
  113. public MDataRow NewRow()
  114. {
  115. MDataRow mdr = new MDataRow();
  116. mdr.TableName = _TableName;
  117. CellStruct mdcStruct = null;
  118. for (int i = 0; i < _Columns.Count; i++)
  119. {
  120. mdcStruct = _Columns[i];
  121. mdr.Add(new MDataCell(ref mdcStruct));
  122. }
  123. return mdr;
  124. }
  125. #endregion
  126. #region 方法
  127. internal void LoadRow(MDataRow row)
  128. {
  129. if (this.Columns.Count == 0 && row != null && row.Count > 0)
  130. {
  131. foreach (MDataCell cell in row)
  132. {
  133. this.Columns.Add(cell._CellStruct);
  134. }
  135. _TableName = row.TableName;
  136. if (row[0].Value != null && row[0].Value != DBNull.Value)
  137. {
  138. _Mdr.Add(row);
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// 转换成DataTable
  144. /// </summary>
  145. public DataTable ToDataTable()
  146. {
  147. DataTable dt = new DataTable(_TableName);
  148. if (Columns != null && Columns.Count > 0)
  149. {
  150. foreach (CellStruct item in Columns)
  151. {
  152. dt.Columns.Add(item.ColumnName,item.ValueType);
  153. }
  154. foreach (MDataRow row in Rows)
  155. {
  156. DataRow dr = dt.NewRow();
  157. for (int i = 0; i < Columns.Count; i++)
  158. {
  159. dr[i] = row[i].Value;
  160. }
  161. dt.Rows.Add(dr);
  162. }
  163. }
  164. return dt;
  165. }
  166. /// <summary>
  167. /// 从Json字符串反加载成MDataTable
  168. /// </summary>
  169. public static MDataTable LoadFromJson(string json)
  170. {
  171. JsonHelper helper = new JsonHelper();
  172. return helper.Load(json);
  173. }
  174. /// <summary>
  175. /// 输出Json字符串
  176. /// </summary>
  177. public string ToJson()
  178. {
  179. JsonHelper helper = new JsonHelper();
  180. helper.Fill(this);
  181. return helper.ToString();
  182. }
  183. /// <summary>
  184. /// 将数据表绑定到列表控件
  185. /// </summary>
  186. /// <param name="control">列表控件[包括Repeater/DataList/GridView/DataGrid等]</param>
  187. public void Bind(object control)
  188. {
  189. MBindUI.Bind(control, this);
  190. }
  191. /// <summary>
  192. /// 转实体列表
  193. /// </summary>
  194. /// <param name="useEmit">是否使用Emit方式转换[数据越多[大于500条]性能越高],不写默认自适应判断</param>
  195. /// <typeparam name="T"></typeparam>
  196. /// <returns></returns>
  197. public List<T> ToList<T>(params bool[] useEmit)
  198. {
  199. List<T> list = new List<T>();
  200. if (Rows != null && Rows.Count > 0)
  201. {
  202. if ((Rows.Count > 500 && useEmit.Length == 0) || (useEmit.Length > 0 && useEmit[0]))
  203. {
  204. FastToT<T>.EmitHandle emit = FastToT<T>.Create(this);
  205. foreach (MDataRow row in Rows)
  206. {
  207. list.Add(emit(row));
  208. }
  209. }
  210. else
  211. {
  212. foreach (MDataRow row in Rows)
  213. {
  214. T obj = (T)Activator.CreateInstance(typeof(T));
  215. PropertyInfo[] pis = obj.GetType().GetProperties();
  216. MDataCell cell = null;
  217. for (int i = 0; i < pis.Length; i++)
  218. {
  219. cell = row[pis[i].Name];
  220. if (cell == null || cell._CellValue.IsNull)
  221. {
  222. continue;
  223. }
  224. pis[i].SetValue(obj, cell.Value, null);
  225. }
  226. list.Add(obj);
  227. }
  228. }
  229. }
  230. return list;
  231. }
  232. #endregion
  233. private int _Ptr = 0;
  234. #region IDataReader 成员
  235. public void Close()
  236. {
  237. _Mdr.Clear();
  238. }
  239. public int Depth
  240. {
  241. get
  242. {
  243. if (_Mdr != null)
  244. {
  245. return _Mdr.Count;
  246. }
  247. return 0;
  248. }
  249. }
  250. public DataTable GetSchemaTable()
  251. {
  252. return null;
  253. }
  254. public bool IsClosed
  255. {
  256. get
  257. {
  258. return true;
  259. }
  260. }
  261. public bool NextResult()
  262. {
  263. if (_Ptr < _Mdr.Count - 1)
  264. {
  265. return true;
  266. }
  267. else
  268. {
  269. return false;
  270. }
  271. }
  272. public bool Read()
  273. {
  274. if (_Ptr < _Mdr.Count)
  275. {
  276. _Ptr++;
  277. return true;
  278. }
  279. else
  280. {
  281. _Ptr = 0;
  282. return false;
  283. }
  284. }
  285. public int RecordsAffected
  286. {
  287. get
  288. {
  289. return -1;
  290. }
  291. }
  292. #endregion
  293. #region IDisposable 成员
  294. public void Dispose()
  295. {
  296. _Mdr.Clear();
  297. _Mdr = null;
  298. }
  299. #endregion
  300. #region IDataRecord 成员
  301. public int FieldCount
  302. {
  303. get
  304. {
  305. if (this.Columns != null)
  306. {
  307. return this.Columns.Count;
  308. }
  309. return 0;
  310. }
  311. }
  312. public bool GetBoolean(int i)
  313. {
  314. return (bool)_Mdr[_Ptr][i].Value;
  315. }
  316. public byte GetByte(int i)
  317. {
  318. return (byte)_Mdr[_Ptr][i].Value;
  319. }
  320. public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
  321. {
  322. throw new Exception(AppConst.Global_NotImplemented);
  323. }
  324. public char GetChar(int i)
  325. {
  326. return (char)_Mdr[_Ptr][i].Value;
  327. }
  328. public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
  329. {
  330. throw new Exception(AppConst.Global_NotImplemented);
  331. }
  332. public IDataReader GetData(int i)
  333. {
  334. return this;
  335. }
  336. public string GetDataTypeName(int i)
  337. {
  338. return "";
  339. //return _Mdr[_Ptr][i]._CellValue.Value.GetType().Name;
  340. //return DataType.GetDbType(_Mdr[_Ptr][i]._CellStruct.SqlType.ToString()).ToString();
  341. }
  342. public DateTime GetDateTime(int i)
  343. {
  344. return (DateTime)_Mdr[_Ptr][i].Value;
  345. }
  346. public decimal GetDecimal(int i)
  347. {
  348. return (decimal)_Mdr[_Ptr][i].Value;
  349. }
  350. public double GetDouble(int i)
  351. {
  352. return (double)_Mdr[_Ptr][i].Value;
  353. }
  354. public Type GetFieldType(int i)
  355. {
  356. return _Columns[i].ValueType;
  357. //return _Mdr[_Ptr][i]._CellStruct.ValueType;
  358. }
  359. public float GetFloat(int i)
  360. {
  361. return (float)_Mdr[_Ptr][i].Value;
  362. }
  363. public Guid GetGuid(int i)
  364. {
  365. return (Guid)_Mdr[_Ptr][i].Value;
  366. }
  367. public short GetInt16(int i)
  368. {
  369. return (short)_Mdr[_Ptr][i].Value;
  370. }
  371. public int GetInt32(int i)
  372. {
  373. return (int)_Mdr[_Ptr][i].Value;
  374. }
  375. public long GetInt64(int i)
  376. {
  377. return (long)_Mdr[_Ptr][i].Value;
  378. }
  379. public string GetName(int i)
  380. {
  381. return _Columns[i].ColumnName;
  382. //return _Mdr[_Ptr][i]._CellStruct.ColumnName;
  383. }
  384. public int GetOrdinal(string name)
  385. {
  386. return _Columns.GetIndex(name);
  387. }
  388. public string GetString(int i)
  389. {
  390. return Convert.ToString(_Mdr[_Ptr][i].Value);
  391. }
  392. public object GetValue(int i)
  393. {
  394. return _Mdr[_Ptr][i].Value;
  395. }
  396. public int GetValues(object[] values)
  397. {
  398. for (int i = 0; i < values.Length; i++)
  399. {
  400. values[i] = _Mdr[_Ptr - 1][i].Value;
  401. }
  402. return values.Length;
  403. }
  404. public bool IsDBNull(int i)
  405. {
  406. return _Mdr[_Ptr][i]._CellValue.IsNull;
  407. }
  408. public object this[string name]
  409. {
  410. get
  411. {
  412. return null;
  413. }
  414. }
  415. public object this[int i]
  416. {
  417. get
  418. {
  419. return _Mdr[i];
  420. }
  421. }
  422. #endregion
  423. #region IEnumerable 成员
  424. public IEnumerator GetEnumerator()
  425. {
  426. return new System.Data.Common.DbEnumerator(this);
  427. }
  428. #endregion
  429. #region IListSource 成员
  430. public bool ContainsListCollection
  431. {
  432. get
  433. {
  434. return true;
  435. }
  436. }
  437. public IList GetList()
  438. {
  439. return Rows;
  440. }
  441. #endregion
  442. }
  443. }