EntityData.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data;
  7. using System.Runtime.Serialization;
  8. namespace WebAPIBase.NetCore.BusinessCore
  9. {
  10. [Serializable]
  11. public class EntityData : DataSet
  12. {
  13. private string m_EntityTypeName = "";
  14. private string m_ClassName = "";
  15. private string m_MainTableName = "";
  16. private int m_CurrentTableIndex = 0;
  17. private int m_CurrentRowIndex = 0;
  18. public EntityData()
  19. {
  20. }
  21. public EntityData(SerializationInfo info, StreamingContext context)
  22. : base(info, context)
  23. {
  24. }
  25. public EntityData(string className)
  26. {
  27. try
  28. {
  29. this.m_ClassName = className;
  30. this.m_EntityTypeName = "";
  31. EntityDataManager.CloneEntityStruct(this, className);
  32. }
  33. catch (Exception ex)
  34. {
  35. throw ex;
  36. }
  37. }
  38. public string ClassName
  39. {
  40. get => this.m_ClassName;
  41. set => this.m_ClassName = value;
  42. }
  43. public new string MainTableName
  44. {
  45. get => this.m_MainTableName;
  46. set => this.m_MainTableName = value;
  47. }
  48. public int CurrentTableIndex => this.m_CurrentTableIndex;
  49. public int CurrentRowIndex => this.m_CurrentRowIndex;
  50. public string EntityTypeName
  51. {
  52. get => this.m_EntityTypeName;
  53. set => this.m_EntityTypeName = value;
  54. }
  55. public void AddNewRecord(DataRow row) => this.Tables[this.m_CurrentTableIndex].Rows.Add(row);
  56. public void AddNewRecord(DataRow row, string tableName)
  57. {
  58. if (!this.Tables.Contains(tableName))
  59. throw new ApplicationException("不存在该表");
  60. try
  61. {
  62. this.Tables[tableName].Rows.Add(row);
  63. }
  64. catch (Exception ex)
  65. {
  66. throw ex;
  67. }
  68. }
  69. public EntityData CloneData()
  70. {
  71. try
  72. {
  73. EntityData entityData = (EntityData)this.Clone();
  74. entityData.EntityTypeName = this.EntityTypeName;
  75. entityData.ClassName = this.ClassName;
  76. entityData.MainTableName = this.MainTableName;
  77. return entityData;
  78. }
  79. catch (Exception ex)
  80. {
  81. throw ex;
  82. }
  83. }
  84. public void CloneDataStucture(EntityData cloneentitydata)
  85. {
  86. try
  87. {
  88. cloneentitydata.EntityTypeName = this.EntityTypeName;
  89. cloneentitydata.ClassName = this.ClassName;
  90. cloneentitydata.MainTableName = this.MainTableName;
  91. cloneentitydata.Relations.Clear();
  92. cloneentitydata.Tables.Clear();
  93. foreach (DataTable table in (InternalDataCollectionBase)this.Tables)
  94. cloneentitydata.Tables.Add(table.Clone());
  95. foreach (DataRelation relation in (InternalDataCollectionBase)this.Relations)
  96. {
  97. DataTable childTable = relation.ChildTable;
  98. DataTable parentTable = relation.ParentTable;
  99. int length1 = relation.ChildColumns.Length;
  100. int length2 = relation.ParentColumns.Length;
  101. DataColumn[] childColumns = new DataColumn[length1];
  102. DataColumn[] parentColumns = new DataColumn[length2];
  103. for (int index = 0; index < length1; ++index)
  104. childColumns[index] = cloneentitydata.Tables[childTable.TableName].Columns[relation.ChildColumns[index].ColumnName];
  105. for (int index = 0; index < length2; ++index)
  106. parentColumns[index] = cloneentitydata.Tables[parentTable.TableName].Columns[relation.ParentColumns[index].ColumnName];
  107. cloneentitydata.Relations.Add(relation.RelationName, parentColumns, childColumns);
  108. }
  109. }
  110. catch (Exception ex)
  111. {
  112. throw ex;
  113. }
  114. }
  115. public DataRow GetNewRecord(string tableName) => this.Tables.Contains(tableName) ? this.Tables[tableName].NewRow() : throw new ApplicationException("不存在该表");
  116. public DataRow GetNewRecord() => this.Tables[this.m_CurrentTableIndex].NewRow();
  117. public DataRow GetRecord() => this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex];
  118. public DataRow GetRecord(string tableName)
  119. {
  120. if (!this.Tables.Contains(tableName))
  121. throw new ApplicationException("不存在该表");
  122. return this.Tables[tableName].Rows.Count < 1 ? (DataRow)null : this.Tables[tableName].Rows[this.m_CurrentRowIndex];
  123. }
  124. public DataRow GetRecord(int index) => this.Tables.Count < 1 || this.Tables[0].Rows.Count < index + 1 ? (DataRow)null : this.Tables[this.m_CurrentTableIndex].Rows[index];
  125. public DataRow GetRecord(string tableName, int index)
  126. {
  127. if (!this.Tables.Contains(tableName))
  128. throw new ApplicationException("不存在该表");
  129. return this.Tables[tableName].Rows.Count < index + 1 ? (DataRow)null : this.Tables[tableName].Rows[index];
  130. }
  131. public void SetCurrentTable(int tableIndex)
  132. {
  133. if (tableIndex < 0 || tableIndex >= this.Tables.Count)
  134. throw new ApplicationException("数据表索引超出范围");
  135. this.m_CurrentTableIndex = tableIndex;
  136. this.m_CurrentRowIndex = 0;
  137. }
  138. public void SetCurrentTable(string tableName)
  139. {
  140. this.m_CurrentTableIndex = this.Tables.Contains(tableName) ? this.Tables.IndexOf(tableName) : throw new ApplicationException("不存在该表");
  141. this.m_CurrentRowIndex = 0;
  142. }
  143. public void SetCurrentRow(int rowIndex)
  144. {
  145. if (rowIndex < 0 || rowIndex >= this.Tables[this.m_CurrentTableIndex].Rows.Count)
  146. throw new ApplicationException("数据行索引超出范围");
  147. this.m_CurrentRowIndex = rowIndex;
  148. }
  149. public DataTable CurrentTable => this.Tables[this.m_CurrentTableIndex];
  150. public DataRow CurrentRow => this.m_CurrentRowIndex >= this.Tables[this.m_CurrentTableIndex].Rows.Count ? (DataRow)null : this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex];
  151. public bool GetBoolean(string columnName)
  152. {
  153. try
  154. {
  155. return !this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) && (bool)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName];
  156. }
  157. catch (Exception ex)
  158. {
  159. throw ex;
  160. }
  161. }
  162. public byte GetByte(string columnName)
  163. {
  164. try
  165. {
  166. return this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) ? (byte)0 : (byte)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName];
  167. }
  168. catch (Exception ex)
  169. {
  170. throw ex;
  171. }
  172. }
  173. public string GetChar(string columnName)
  174. {
  175. try
  176. {
  177. return this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) ? "" : ((char)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName]).ToString();
  178. }
  179. catch (Exception ex)
  180. {
  181. throw ex;
  182. }
  183. }
  184. public string GetDateTime(string columnName)
  185. {
  186. try
  187. {
  188. return this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) ? "" : ((DateTime)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName]).ToString();
  189. }
  190. catch (Exception ex)
  191. {
  192. throw ex;
  193. }
  194. }
  195. public string GetDateTime(string columnName, string formatString)
  196. {
  197. try
  198. {
  199. return this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) ? "" : ((DateTime)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName]).ToString(formatString);
  200. }
  201. catch (Exception ex)
  202. {
  203. throw ex;
  204. }
  205. }
  206. public string GetDateTimeOnlyDate(string columnName)
  207. {
  208. try
  209. {
  210. return this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) ? "" : ((DateTime)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName]).ToString("yyyy-MM-dd");
  211. }
  212. catch (Exception ex)
  213. {
  214. throw ex;
  215. }
  216. }
  217. public Decimal GetDecimal(string columnName)
  218. {
  219. try
  220. {
  221. return this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) ? 0M : (Decimal)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName];
  222. }
  223. catch (Exception ex)
  224. {
  225. throw ex;
  226. }
  227. }
  228. public string GetDecimalString(string columnName)
  229. {
  230. try
  231. {
  232. string str = "";
  233. if (!this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName))
  234. str = ((Decimal)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName]).ToString("F");
  235. return str;
  236. }
  237. catch (Exception ex)
  238. {
  239. throw ex;
  240. }
  241. }
  242. public double GetDouble(string columnName)
  243. {
  244. try
  245. {
  246. return this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) ? 0.0 : (double)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName];
  247. }
  248. catch (Exception ex)
  249. {
  250. throw ex;
  251. }
  252. }
  253. public string GetDoubleString(string columnName)
  254. {
  255. try
  256. {
  257. string str = "";
  258. if (!this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName))
  259. str = ((double)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName]).ToString("F");
  260. return str;
  261. }
  262. catch (Exception ex)
  263. {
  264. throw ex;
  265. }
  266. }
  267. public float GetFloat(string columnName)
  268. {
  269. try
  270. {
  271. return this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) ? 0.0f : (float)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName];
  272. }
  273. catch (Exception ex)
  274. {
  275. throw ex;
  276. }
  277. }
  278. public string GetFloatString(string columnName)
  279. {
  280. try
  281. {
  282. string str = "";
  283. if (!this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName))
  284. str = ((float)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName]).ToString("F");
  285. return str;
  286. }
  287. catch (Exception ex)
  288. {
  289. throw ex;
  290. }
  291. }
  292. public int GetInt(string columnName)
  293. {
  294. try
  295. {
  296. return this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) ? 0 : (int)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName];
  297. }
  298. catch (Exception ex)
  299. {
  300. throw ex;
  301. }
  302. }
  303. public string GetIntString(string columnName)
  304. {
  305. try
  306. {
  307. string str = "";
  308. if (!this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName))
  309. str = ((int)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName]).ToString();
  310. return str;
  311. }
  312. catch (Exception ex)
  313. {
  314. throw ex;
  315. }
  316. }
  317. public long GetLong(string columnName)
  318. {
  319. try
  320. {
  321. return this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) ? 0L : (long)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName];
  322. }
  323. catch (Exception ex)
  324. {
  325. throw ex;
  326. }
  327. }
  328. public string GetLongString(string columnName)
  329. {
  330. try
  331. {
  332. string str = "";
  333. if (this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName))
  334. str = ((long)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName]).ToString();
  335. return str;
  336. }
  337. catch (Exception ex)
  338. {
  339. throw ex;
  340. }
  341. }
  342. public sbyte GetSByte(string columnName)
  343. {
  344. try
  345. {
  346. return this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) ? (sbyte)0 : (sbyte)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName];
  347. }
  348. catch (Exception ex)
  349. {
  350. throw ex;
  351. }
  352. }
  353. public short GetShort(string columnName)
  354. {
  355. try
  356. {
  357. return this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) ? (short)0 : (short)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName];
  358. }
  359. catch (Exception ex)
  360. {
  361. throw ex;
  362. }
  363. }
  364. public string GetShortString(string columnName)
  365. {
  366. try
  367. {
  368. string str = "";
  369. if (this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName))
  370. str = ((short)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName]).ToString();
  371. return str;
  372. }
  373. catch (Exception ex)
  374. {
  375. throw ex;
  376. }
  377. }
  378. public string GetString(string columnName)
  379. {
  380. try
  381. {
  382. return this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex].IsNull(columnName) ? "" : (string)this.Tables[this.m_CurrentTableIndex].Rows[this.m_CurrentRowIndex][columnName];
  383. }
  384. catch (Exception ex)
  385. {
  386. throw ex;
  387. }
  388. }
  389. public bool HasRecord() => this.Tables[this.m_CurrentTableIndex].Rows.Count > 0;
  390. public void DeleteAllTableRow(string tableName)
  391. {
  392. DataTable dataTable = this.Tables.Contains(tableName) ? this.Tables[tableName] : throw new ApplicationException("不存在该表:" + tableName);
  393. int count = dataTable.Rows.Count;
  394. for (int index = 0; index < count; ++index)
  395. dataTable.Rows[index].Delete();
  396. }
  397. }
  398. }