DatasetVectorEx.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using SuperMap.Data;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. namespace WWPipeLine.MapBasic
  7. {
  8. /// <summary>
  9. /// DatasetVector查询对象
  10. /// </summary>
  11. public class DatasetVectorEx
  12. {
  13. //private readonly string _layerName;
  14. //public DatasetVectorEx(string lyrName)
  15. //{
  16. // _layerName = lyrName;
  17. // resultFields.AddRange(new string[] { "smid" });
  18. //}
  19. public DatasetVectorEx(string lyrName = "")
  20. {
  21. resultFields.AddRange(new string[] { "smid" });
  22. }
  23. private DatasetVector m_DatasetVector;
  24. /// <summary>
  25. /// DatasetVectorEx中的DatasetVector矢量数据集对象
  26. /// </summary>
  27. public DatasetVector DatasetVector { set => m_DatasetVector = value; }// get => m_DatasetVector;
  28. private List<string> resultFields = new List<string>();
  29. /// <summary>
  30. /// 查询后,结果字段集
  31. /// </summary>
  32. public List<string> ResultFields
  33. {
  34. get => resultFields;
  35. set => resultFields = Commons.StringEx.setToLowerList(value);
  36. }
  37. private List<string> orderBy = new List<string>();
  38. /// <summary>
  39. /// 查询时,排序字段集
  40. /// </summary>
  41. public List<string> OrderBy
  42. {
  43. get => orderBy;
  44. set => orderBy = value;
  45. }
  46. private List<string> groupBy = new List<string>();
  47. /// <summary>
  48. /// 查询时,分组字段集
  49. /// </summary>
  50. public List<string> GroupBy
  51. {
  52. get => groupBy;
  53. set => groupBy = value;
  54. }
  55. private bool m_HasGeometry = true;
  56. /// <summary>
  57. /// HasGeometry 几何学 几何结构
  58. /// </summary>
  59. public bool HasGeometry { get => m_HasGeometry; set => m_HasGeometry = value; }
  60. /// <summary>
  61. /// 构造查询参数类,设置查询条件。并返回Recordset数据
  62. /// <para>使用完毕该Recordset后,必须关闭 recordset.Close(); recordset.Dispose(); </para>
  63. /// </summary>
  64. /// <param name="sqlWhere"></param>
  65. /// <param name="spatialQueryObject"></param>
  66. /// <returns></returns>
  67. public Recordset QueryToRecordset(string sqlWhere, object spatialQueryObject = null)
  68. {
  69. QueryParameter queryParameter = new QueryParameter
  70. {
  71. AttributeFilter = sqlWhere,
  72. //ResultFields = resultFields?.ToArray(),
  73. ResultFields = Commons.StringEx.setToLower(resultFields),
  74. OrderBy = orderBy?.ToArray(),
  75. GroupBy = groupBy?.ToArray(),
  76. CursorType = CursorType.Static,
  77. HasGeometry = this.m_HasGeometry
  78. };
  79. if (spatialQueryObject != null)
  80. {
  81. queryParameter.SpatialQueryObject = spatialQueryObject;
  82. queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
  83. }
  84. try
  85. {
  86. Recordset recordset = m_DatasetVector.Query(queryParameter);
  87. return recordset;
  88. }
  89. catch (Exception ex)
  90. {
  91. Commons.LogHelper.Fatal(ex);
  92. }
  93. return null;
  94. }
  95. /// <summary>
  96. /// 构造查询参数类,设置查询条件。并返回DataTable数据
  97. /// </summary>
  98. /// <param name="sqlWhere"></param>
  99. /// <param name="spatialQueryObject"></param>
  100. /// <returns></returns>
  101. public DataTable Query(string sqlWhere, object spatialQueryObject = null)
  102. {
  103. QueryParameter queryParameter = new QueryParameter
  104. {
  105. AttributeFilter = sqlWhere,
  106. //ResultFields = resultFields?.ToArray(),
  107. ResultFields = Commons.StringEx.setToLower(resultFields),
  108. OrderBy = orderBy?.ToArray(),
  109. GroupBy = groupBy?.ToArray(),
  110. CursorType = CursorType.Static,
  111. HasGeometry = this.m_HasGeometry
  112. };
  113. if (spatialQueryObject != null)
  114. {
  115. queryParameter.SpatialQueryObject = spatialQueryObject;
  116. queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
  117. }
  118. Recordset recordset = null;
  119. try
  120. {
  121. recordset = m_DatasetVector.Query(queryParameter);
  122. return ToDataTable(recordset);
  123. }
  124. catch (Exception ex)
  125. {
  126. Commons.LogHelper.Fatal(ex);
  127. }
  128. finally
  129. {
  130. if (recordset != null) { recordset.Close(); recordset.Dispose(); }
  131. }
  132. return null;
  133. }
  134. /// <summary>
  135. /// 根据条件返回一行一列的值。只能是一个字符串。
  136. /// </summary>
  137. /// <param name="sqlWhere"></param>
  138. /// <param name="resultField"></param>
  139. /// <param name="spatialQueryObject"></param>
  140. /// <returns></returns>
  141. public string QueryGetValueByOnlyField(string sqlWhere, string m_resultField, object spatialQueryObject = null)
  142. {
  143. QueryParameter queryParameter = new QueryParameter
  144. {
  145. AttributeFilter = sqlWhere,
  146. ResultFields = new string[] { m_resultField.ToLower() },
  147. CursorType = CursorType.Static,
  148. HasGeometry = this.m_HasGeometry
  149. };
  150. if (spatialQueryObject != null)
  151. {
  152. queryParameter.SpatialQueryObject = spatialQueryObject;
  153. queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
  154. }
  155. string result = string.Empty;
  156. Recordset recordset = null;
  157. try
  158. {
  159. recordset = m_DatasetVector.Query(queryParameter);
  160. if (recordset.RecordCount == 1)
  161. {
  162. recordset.MoveFirst();
  163. result = recordset.GetFieldValue(m_resultField).ToString();
  164. }
  165. }
  166. catch (Exception ex)
  167. {
  168. Commons.LogHelper.Fatal(ex);
  169. }
  170. finally
  171. {
  172. if (recordset != null) { recordset.Close(); recordset.Dispose(); }
  173. }
  174. return result;
  175. }
  176. /// <summary>
  177. /// 将Recordset转为DataTable
  178. /// </summary>
  179. /// <param name="recordset"></param>
  180. /// <returns></returns>
  181. public DataTable ToDataTable(Recordset recordset)
  182. {
  183. resultFields = Commons.StringEx.setToLowerList(resultFields);
  184. DataTable dt = new DataTable(m_DatasetVector.Name);
  185. FieldInfos fieldInfos = recordset.GetFieldInfos();
  186. // 输出字段名 Output the field name
  187. Int32 count = fieldInfos.Count;
  188. DataColumn dataColumn;
  189. for (Int32 i = 0; i < count; i++)
  190. {
  191. String fieldname = fieldInfos[i].Name.ToLower();
  192. dataColumn = new DataColumn { ColumnName = fieldname, Caption = fieldInfos[i].Caption };
  193. if (resultFields != null && resultFields.Count > 0)
  194. {
  195. if (resultFields.Contains(fieldname))
  196. dt.Columns.Add(dataColumn);
  197. }
  198. else
  199. {
  200. dt.Columns.Add(dataColumn);
  201. }
  202. }
  203. if (dt.Columns.Count == 0) return dt;
  204. // 输出字段值 Output the field value
  205. recordset.MoveFirst();
  206. Int32 length = recordset.RecordCount;
  207. for (int i = 0; i < length; i++)
  208. {
  209. DataRow dr = dt.NewRow();
  210. var actIndex = 0;
  211. for (Int32 j = 0; j < count; j++)
  212. {
  213. if (resultFields != null && resultFields.Count > 0)
  214. {
  215. String fieldname = fieldInfos[j].Name.ToLower();
  216. if (resultFields.Contains(fieldname))
  217. {
  218. var objValue = recordset.GetFieldValue(j);
  219. String value = objValue?.ToString();
  220. dr[actIndex++] = value;
  221. }
  222. }
  223. else
  224. {
  225. var objValue = recordset.GetFieldValue(j);
  226. string value = objValue?.ToString();
  227. dr[actIndex++] = value;
  228. }
  229. }
  230. dt.Rows.Add(dr);
  231. recordset.MoveNext();
  232. }
  233. return dt;
  234. }
  235. }
  236. }