ObjDataOperationClass.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Extensometer.Data;
  10. using Extensometer.Model;
  11. using Extensometer.Model.ResultModel;
  12. namespace Extensometer.BLL
  13. {
  14. public class ObjDataOperationClass
  15. {
  16. private static ObjDataOperationClass ObjDataOperation = new ObjDataOperationClass();
  17. private ObjDataOperationClass()
  18. { }
  19. public static ObjDataOperationClass GetObjDataOperationClass()
  20. {
  21. return ObjDataOperation;
  22. }
  23. private string ObjDbPath = string.Empty;
  24. /// <summary>
  25. /// 创建
  26. /// </summary>
  27. /// <param name="DbPath"></param>
  28. public void CreatObjDbFile(string DbPath)
  29. {
  30. using (var Db = new ObjectDBTable(DbPath))
  31. {
  32. ObjDbPath = DbPath;
  33. }
  34. }
  35. /// <summary>
  36. /// 加载
  37. /// </summary>
  38. /// <param name="DbPath"></param>
  39. public void LoadObjDbFile(string DbPath)
  40. {
  41. ObjDbPath = DbPath;
  42. LoadDll();
  43. }
  44. public void LoadDll()
  45. {
  46. Thread thread = new Thread(()=> {
  47. using (var Db = new ObjectDBTable(ObjDbPath))
  48. {
  49. }
  50. });
  51. thread.Start();
  52. }
  53. /// <summary>
  54. /// 全查询
  55. /// </summary>
  56. public List<ObjectData> CheckObjDb()
  57. {
  58. List<ObjectData> result = new List<ObjectData>();
  59. using (var Db = new ObjectDBTable(ObjDbPath))
  60. {
  61. result = Db.ObjectDatas.OrderBy(e => e.GaugeLeng).ToList();
  62. }
  63. return result;
  64. }
  65. /// <summary>
  66. /// 添加一条数据
  67. /// </summary>
  68. /// <param name="objectData"></param>
  69. /// <returns></returns>
  70. public ResultModel InsertObjDb(ObjectData objectData)
  71. {
  72. ResultModel result = new ResultModel();
  73. using (var Db = new ObjectDBTable(ObjDbPath))
  74. {
  75. if(Db.Insert(objectData)>0)
  76. {
  77. result.ErrCode = ErrCodeList.Success;
  78. result.ErrMessage = "添加成功";
  79. }
  80. }
  81. return result;
  82. }
  83. /// <summary>
  84. /// List<ObjectData>转成Exl表格
  85. /// </summary>
  86. /// <param name="list"></param>
  87. /// <param name="FilePath"></param>
  88. /// <returns></returns>
  89. //public ResultModel DataExport(List<ObjectData> list,string FilePath)
  90. //{
  91. // ResultModel resultModel = new ResultModel();
  92. // if (list.Count > 0)
  93. // {
  94. // object misValue = Missing.Value;
  95. // Application xlApp = new Application();
  96. // Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
  97. // Worksheet xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
  98. // PropertyInfo[] props = GetPropertyInfoArray();
  99. // for (int i = 0; i < props.Length; i++)
  100. // {
  101. // xlWorkSheet.Cells[1, i + 1] = props[i].Name; //write the column name
  102. // }
  103. // for (int i = 0; i < list.Count; i++)
  104. // {
  105. // xlWorkSheet.Cells[i + 2, 1] = list[i].GaugeLeng.ToString();
  106. // xlWorkSheet.Cells[i + 2, 2] = list[i].Time.ToString();
  107. // xlWorkSheet.Cells[i + 2, 3] = list[i].Point1x.ToString();
  108. // xlWorkSheet.Cells[i + 2, 4] = list[i].Point1y.ToString();
  109. // xlWorkSheet.Cells[i + 2, 5] = list[i].Point2x.ToString();
  110. // xlWorkSheet.Cells[i + 2, 6] = list[i].Point2y.ToString();
  111. // xlWorkSheet.Cells[i + 2, 7] = list[i].StrainValue.ToString();
  112. // xlWorkSheet.Cells[i + 2, 8] = list[i].Distance.ToString();
  113. // xlWorkSheet.Cells[i + 2, 9] = list[i].Displacement.ToString();
  114. // xlWorkSheet.Cells[i + 2, 10] = list[i].ForceValue.ToString();
  115. // }
  116. // try
  117. // {
  118. // xlWorkBook.SaveAs(FilePath, XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
  119. // xlWorkBook.Close(true, misValue, misValue);
  120. // xlApp.Quit();
  121. // }
  122. // catch
  123. // { }
  124. // }
  125. // return resultModel;
  126. //}
  127. /// <summary>
  128. /// 查询数据库路径是否为空
  129. /// </summary>
  130. /// <returns></returns>
  131. public ResultModel CheckDataPathIsEmpty()
  132. {
  133. ResultModel resultModel = new ResultModel();
  134. try
  135. {
  136. if(string.IsNullOrEmpty(ObjDbPath))
  137. {
  138. resultModel.ErrCode = ErrCodeList.PathEmpty;
  139. resultModel.ErrMessage = "数据路径为空";
  140. }
  141. else
  142. {
  143. resultModel.ErrCode = ErrCodeList.Success;
  144. resultModel.ErrMessage = "";
  145. }
  146. }
  147. catch
  148. {
  149. resultModel.ErrCode = ErrCodeList.OtherErr;
  150. resultModel.ErrMessage = "未知错误";
  151. }
  152. return resultModel;
  153. }
  154. /// <summary>
  155. /// 反射获取类型的所有属性
  156. /// </summary>
  157. /// <returns></returns>
  158. private PropertyInfo[] GetPropertyInfoArray()
  159. {
  160. PropertyInfo[] props = null;
  161. try
  162. {
  163. Type type = typeof(ObjectData);
  164. object obj = Activator.CreateInstance(type);
  165. props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
  166. }
  167. catch
  168. { }
  169. return props;
  170. }
  171. }
  172. }