DatasetVectorEx.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. private DatasetVector m_DatasetVector;
  20. /// <summary>
  21. /// DatasetVectorEx中的DatasetVector矢量数据集对象
  22. /// </summary>
  23. public DatasetVector DatasetVector { set => m_DatasetVector = value; }
  24. private List<string> resultFields = new List<string>();
  25. /// <summary>
  26. /// 查询后,结果字段集
  27. /// </summary>
  28. public List<string> ResultFields { get => resultFields; set => resultFields = value; }
  29. private List<string> orderBy = new List<string>();
  30. /// <summary>
  31. /// 查询时,排序字段集
  32. /// </summary>
  33. public List<string> OrderBy { get => orderBy; set => orderBy = value; }
  34. private List<string> groupBy = new List<string>();
  35. /// <summary>
  36. /// 查询时,分组字段集
  37. /// </summary>
  38. public List<string> GroupBy { get => groupBy; set => groupBy = value; }
  39. private bool m_HasGeometry = true;
  40. /// <summary>
  41. /// HasGeometry 几何学 几何结构
  42. /// </summary>
  43. public bool HasGeometry { get => m_HasGeometry; set => m_HasGeometry = value; }
  44. /// <summary>
  45. /// 构造查询参数类,设置查询条件。并返回Recordset数据
  46. /// <para>使用完毕该Recordset后,必须关闭 recordset.Close(); recordset.Dispose(); </para>
  47. /// </summary>
  48. /// <param name="sqlWhere"></param>
  49. /// <param name="spatialQueryObject"></param>
  50. /// <returns></returns>
  51. public Recordset QueryToRecordset(string sqlWhere, object spatialQueryObject = null)
  52. {
  53. QueryParameter queryParameter = new QueryParameter
  54. {
  55. AttributeFilter = sqlWhere,
  56. ResultFields = resultFields?.ToArray(),
  57. OrderBy = orderBy?.ToArray(),
  58. GroupBy = groupBy?.ToArray(),
  59. CursorType = CursorType.Static,
  60. HasGeometry = this.m_HasGeometry
  61. };
  62. if (spatialQueryObject != null)
  63. {
  64. queryParameter.SpatialQueryObject = spatialQueryObject;
  65. queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
  66. }
  67. try
  68. {
  69. Recordset recordset = m_DatasetVector.Query(queryParameter);
  70. return recordset;
  71. }
  72. catch (Exception ex)
  73. {
  74. Console.WriteLine(ex.Message);
  75. }
  76. return null;
  77. }
  78. /// <summary>
  79. /// 构造查询参数类,设置查询条件。并返回DataTable数据
  80. /// </summary>
  81. /// <param name="sqlWhere"></param>
  82. /// <param name="spatialQueryObject"></param>
  83. /// <returns></returns>
  84. public DataTable Query(string sqlWhere, object spatialQueryObject = null)
  85. {
  86. QueryParameter queryParameter = new QueryParameter
  87. {
  88. AttributeFilter = sqlWhere,
  89. ResultFields = resultFields?.ToArray(),
  90. OrderBy = orderBy?.ToArray(),
  91. GroupBy = groupBy?.ToArray(),
  92. CursorType = CursorType.Static,
  93. HasGeometry = this.m_HasGeometry
  94. };
  95. if (spatialQueryObject != null)
  96. {
  97. queryParameter.SpatialQueryObject = spatialQueryObject;
  98. queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
  99. }
  100. Recordset recordset = null;
  101. try
  102. {
  103. recordset = m_DatasetVector.Query(queryParameter);
  104. return ToDataTable(recordset);
  105. }
  106. catch (Exception ex)
  107. {
  108. Console.WriteLine(ex.Message);
  109. }
  110. finally
  111. {
  112. if (recordset != null) { recordset.Close(); recordset.Dispose(); }
  113. }
  114. return null;
  115. }
  116. public DataTable QueryCaption(string sqlWhere, object spatialQueryObject = null)
  117. {
  118. QueryParameter queryParameter = new QueryParameter
  119. {
  120. AttributeFilter = sqlWhere,
  121. ResultFields = resultFields?.ToArray(),
  122. OrderBy = orderBy?.ToArray(),
  123. GroupBy = groupBy?.ToArray(),
  124. CursorType = CursorType.Static,
  125. HasGeometry = this.m_HasGeometry
  126. };
  127. if (spatialQueryObject != null)
  128. {
  129. queryParameter.SpatialQueryObject = spatialQueryObject;
  130. queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
  131. }
  132. Recordset recordset = null;
  133. try
  134. {
  135. recordset = m_DatasetVector.Query(queryParameter);
  136. return ToDataTableCaption(recordset);
  137. }
  138. catch (Exception ex)
  139. {
  140. Console.WriteLine(ex.Message);
  141. }
  142. finally
  143. {
  144. if (recordset != null) { recordset.Close(); recordset.Dispose(); }
  145. }
  146. return null;
  147. }
  148. /// <summary>
  149. /// 构造查询参数类,设置查询条件。并返回查询结果。
  150. /// <para>判断记录集中是否有记录。true 表示该记录集中无数据,此时,RecordCount 的值应为-1。 </para>
  151. /// </summary>
  152. /// <param name="sqlWhere"></param>
  153. /// <param name="spatialQueryObject"></param>
  154. /// <returns></returns>
  155. public bool QueryIsEmpty(string sqlWhere, object spatialQueryObject = null)
  156. {
  157. QueryParameter queryParameter = new QueryParameter
  158. {
  159. AttributeFilter = sqlWhere,
  160. ResultFields = resultFields?.ToArray(),
  161. OrderBy = orderBy?.ToArray(),
  162. GroupBy = groupBy?.ToArray(),
  163. CursorType = CursorType.Static,
  164. HasGeometry = this.m_HasGeometry
  165. };
  166. if (spatialQueryObject != null)
  167. {
  168. queryParameter.SpatialQueryObject = spatialQueryObject;
  169. queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
  170. }
  171. Recordset recordset = null;
  172. try
  173. {
  174. recordset = m_DatasetVector.Query(queryParameter);
  175. return recordset.IsEmpty;
  176. }
  177. catch (Exception ex)
  178. {
  179. Console.WriteLine(ex.Message);
  180. }
  181. finally
  182. {
  183. if (recordset != null) { recordset.Close(); recordset.Dispose(); }
  184. }
  185. return false;
  186. }
  187. /// <summary>
  188. /// 根据查询条件,查询一个唯一的SMID值。
  189. /// <para>如果没有查询到SMID,则返回-1</para>
  190. /// </summary>
  191. /// <param name="sqlWhere"></param>
  192. /// <param name="spatialQueryObject"></param>
  193. /// <returns></returns>
  194. public Int32 QueryGetSMID(string sqlWhere, object spatialQueryObject = null)
  195. {
  196. QueryParameter queryParameter = new QueryParameter
  197. {
  198. AttributeFilter = sqlWhere,
  199. //ResultFields = resultFields?.ToArray(),
  200. ResultFields = new string[] { "smid" },
  201. CursorType = CursorType.Static,
  202. HasGeometry = this.m_HasGeometry
  203. };
  204. if (spatialQueryObject != null)
  205. {
  206. queryParameter.SpatialQueryObject = spatialQueryObject;
  207. queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
  208. }
  209. int result = -1;
  210. Recordset recordset = null;
  211. try
  212. {
  213. recordset = m_DatasetVector.Query(queryParameter);
  214. Int32.TryParse(recordset.GetFieldValue("smid").ToString(), out result);
  215. return result;
  216. }
  217. catch (Exception ex)
  218. {
  219. Console.WriteLine(ex.Message);
  220. }
  221. finally
  222. {
  223. if (recordset != null) { recordset.Close(); recordset.Dispose(); }
  224. }
  225. return result;
  226. }
  227. /// <summary>
  228. /// 根据条件返回一行一列的值。只能是一个字符串。
  229. /// </summary>
  230. /// <param name="sqlWhere"></param>
  231. /// <param name="resultField"></param>
  232. /// <param name="spatialQueryObject"></param>
  233. /// <returns></returns>
  234. public string QueryGetValueByOnlyField(string sqlWhere, string resultField, object spatialQueryObject = null)
  235. {
  236. QueryParameter queryParameter = new QueryParameter
  237. {
  238. AttributeFilter = sqlWhere,
  239. ResultFields = new string[] { resultField },
  240. CursorType = CursorType.Static,
  241. HasGeometry = this.m_HasGeometry
  242. };
  243. if (spatialQueryObject != null)
  244. {
  245. queryParameter.SpatialQueryObject = spatialQueryObject;
  246. queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
  247. }
  248. string result = string.Empty;
  249. Recordset recordset = null;
  250. try
  251. {
  252. recordset = m_DatasetVector.Query(queryParameter);
  253. if (recordset.RecordCount == 1)
  254. {
  255. recordset.MoveFirst();
  256. result = recordset.GetFieldValue(resultField).ToString();
  257. }
  258. }
  259. catch (Exception ex)
  260. {
  261. Console.WriteLine(ex.Message);
  262. }
  263. finally
  264. {
  265. if (recordset != null) { recordset.Close(); recordset.Dispose(); }
  266. }
  267. return result;
  268. }
  269. /// <summary>
  270. /// 将Recordset转为DataTable
  271. /// </summary>
  272. /// <param name="recordset"></param>
  273. /// <returns></returns>
  274. public DataTable ToDataTable(Recordset recordset)
  275. {
  276. DataTable dt = new DataTable(_layerName);
  277. try
  278. {
  279. FieldInfos fieldInfos = recordset.GetFieldInfos();
  280. // 输出字段名 Output the field name
  281. Int32 count = fieldInfos.Count;
  282. for (Int32 i = 0; i < count; i++)
  283. {
  284. String fieldname = fieldInfos[i].Name;
  285. if (resultFields != null && resultFields.Count > 0)
  286. {
  287. if (resultFields.Contains(fieldname))
  288. dt.Columns.Add(fieldname);
  289. // dt.Columns.Add(fieldInfos[i].Caption);
  290. }
  291. else
  292. {
  293. dt.Columns.Add(fieldname);
  294. // dt.Columns.Add(fieldInfos[i].Caption);
  295. }
  296. }
  297. // 输出字段值 Output the field value
  298. recordset.MoveFirst();
  299. Int32 length = recordset.RecordCount;
  300. for (int i = 0; i < length; i++)
  301. {
  302. DataRow dr = dt.NewRow();
  303. var actIndex = 0;
  304. for (Int32 j = 0; j < count; j++)
  305. {
  306. if (resultFields != null && resultFields.Count > 0)
  307. {
  308. if (resultFields.Contains(fieldInfos[j].Name))
  309. {
  310. var objValue = recordset.GetFieldValue(j);
  311. String value = objValue?.ToString();
  312. dr[actIndex++] = value;
  313. }
  314. }
  315. else
  316. {
  317. var objValue = recordset.GetFieldValue(j);
  318. string value = objValue?.ToString();
  319. dr[actIndex++] = value;
  320. }
  321. }
  322. dt.Rows.Add(dr);
  323. recordset.MoveNext();
  324. }
  325. }
  326. catch (Exception ex)
  327. {
  328. Console.WriteLine(ex.Message);
  329. }
  330. return dt;
  331. }
  332. public DataTable ToDataTableCaption(Recordset recordset)
  333. {
  334. DataTable dt = new DataTable(_layerName);
  335. try
  336. {
  337. FieldInfos fieldInfos = recordset.GetFieldInfos();
  338. // 输出字段名 Output the field name
  339. Int32 count = fieldInfos.Count;
  340. for (Int32 i = 0; i < count; i++)
  341. {
  342. String fieldname = fieldInfos[i].Name;
  343. String fieldCaption = fieldInfos[i].Caption;
  344. if (resultFields != null && resultFields.Count > 0)
  345. {
  346. if (resultFields.Contains(fieldname))
  347. dt.Columns.Add(fieldCaption);
  348. }
  349. else
  350. {
  351. dt.Columns.Add(fieldCaption);
  352. }
  353. }
  354. // 输出字段值 Output the field value
  355. recordset.MoveFirst();
  356. Int32 length = recordset.RecordCount;
  357. for (int i = 0; i < length; i++)
  358. {
  359. DataRow dr = dt.NewRow();
  360. int actIndex = 0;
  361. for (Int32 j = 0; j < count; j++)
  362. {
  363. if (resultFields != null && resultFields.Count > 0)
  364. {
  365. if (resultFields.Contains(fieldInfos[j].Name))
  366. {
  367. var objValue = recordset.GetFieldValue(j);
  368. String value = objValue?.ToString();
  369. dr[actIndex++] = value;
  370. }
  371. }
  372. else
  373. {
  374. var objValue = recordset.GetFieldValue(j);
  375. string value = objValue?.ToString();
  376. dr[actIndex++] = value;
  377. }
  378. }
  379. dt.Rows.Add(dr);
  380. recordset.MoveNext();
  381. }
  382. }
  383. catch (Exception ex)
  384. {
  385. Console.WriteLine(ex.Message);
  386. }
  387. return dt;
  388. }
  389. #region 在当前的DatasetVector中,获取指定列名的最大值、最小值
  390. /// <summary>
  391. /// 在当前的DatasetVector中,获取指定列名的最大值
  392. /// </summary>
  393. /// <param name="fieldName"></param>
  394. /// <returns></returns>
  395. public double GetMaxValue(string fieldName)
  396. {
  397. var val = MinMaxValue(fieldName, 1);
  398. if (val == null) return double.NaN;
  399. else return double.Parse(val.ToString());
  400. }
  401. /// <summary>
  402. /// 在当前的DatasetVector中,获取指定列名的最小值
  403. /// </summary>
  404. /// <param name="fieldName"></param>
  405. /// <returns></returns>
  406. public double GetMinValue(string fieldName)
  407. {
  408. var val = MinMaxValue(fieldName, -1);
  409. if (val == null) return double.NaN;
  410. else return double.Parse(val.ToString());
  411. }
  412. /// <summary>
  413. /// 在当前的DatasetVector中,获取指定列名的最大值和最小值
  414. /// </summary>
  415. /// <param name="fieldName"></param>
  416. /// <returns></returns>
  417. public double[] GetMinMaxValue(string fieldName)
  418. {
  419. var val = MinMaxValue(fieldName, 0);
  420. if (val == null) return new double[] { };
  421. else return (double[])val;
  422. }
  423. /// <summary>
  424. /// 在当前的DatasetVector中,获取指定列名的最大值、最小值
  425. /// </summary>
  426. /// <param name="fieldName">列名</param>
  427. /// <param name="min">min = 0,获取最小值和最大值;-1时获取最小值,1时获取最大值</param>
  428. /// <returns></returns>
  429. private object MinMaxValue(string fieldName, int minMax = 0)
  430. {
  431. this.m_HasGeometry = false;
  432. resultFields.Clear();
  433. resultFields.Add(fieldName);
  434. if (minMax == 1)
  435. orderBy.Add(fieldName + " desc");
  436. else
  437. orderBy.Add(fieldName + " asc");
  438. var dt = this.Query(fieldName + " is not null");
  439. if (dt.Rows.Count > 0)
  440. {
  441. if (minMax != 0)
  442. return double.Parse(dt.Rows[0][0].ToString());
  443. else
  444. {
  445. return new double[]
  446. {
  447. double.Parse(dt.Rows[0][0].ToString()),
  448. double.Parse(dt.Rows[dt.Rows.Count-1][0].ToString())
  449. };
  450. }
  451. }
  452. return null;
  453. }
  454. #endregion
  455. /// <summary>
  456. /// 判断当前DatasetVector中,是否包含指定的列名。包含则返回true
  457. /// </summary>
  458. /// <param name="_field"></param>
  459. /// <returns></returns>
  460. public bool HasField(string _field)
  461. {
  462. bool hasField = false;
  463. for (int i = 0; i < m_DatasetVector.FieldInfos.Count; i++)
  464. {
  465. if (m_DatasetVector.FieldInfos[i].Name == _field)
  466. {
  467. hasField = true;
  468. break;
  469. }
  470. }
  471. return hasField;
  472. }
  473. /// <summary>
  474. /// 二维地图集 按照表名、列名返回该列名的字段值及其加总数量的数据表,并按照filed排序
  475. /// 里面调用UniqueValOrderSum方法。返回列名的字段值的加总数量
  476. /// </summary>
  477. /// <param name="_filed"></param>
  478. /// <param name="_filedName"></param>
  479. /// <param name="_sumName"></param>
  480. /// <returns></returns>
  481. public DataTable GetTableByFieldOnUniqueValSumOrder(string _filed, string _filedName, string _sumName)
  482. {
  483. Dictionary<int, double> result = UniqueValOrderSum(_filedName, _sumName);
  484. DataTable dt = new DataTable()
  485. {
  486. TableName = m_DatasetVector.Name
  487. }; ;
  488. dt.Columns.Add(_filed);
  489. foreach (var key in result.Keys)
  490. {
  491. var tmpKey = key.ToString();
  492. if (key == 0)
  493. tmpKey = "未知";
  494. if (!dt.Columns.Contains(tmpKey.ToString()))
  495. dt.Columns.Add(tmpKey.ToString());
  496. }
  497. var rows = dt.NewRow();
  498. rows[0] = m_DatasetVector.Name;
  499. dt.Rows.Add(rows);
  500. var rowIndex = 0;
  501. var drs = dt.Rows[rowIndex++];
  502. foreach (var key in result.Keys)
  503. {
  504. var tmpKey = key.ToString();
  505. if (key == 0)
  506. tmpKey = "未知";
  507. drs[tmpKey] = result[key];
  508. }
  509. return dt;
  510. }
  511. /// <summary>
  512. /// 二维地图集 按照表名、列名返回该列名的字段值及其加总数量的数据表
  513. /// 里面调用UniqueValOrderSum方法。返回列名的字段值的加总数量
  514. /// </summary>
  515. /// <param name="_filed"></param>
  516. /// <param name="_filedName"></param>
  517. /// <param name="_sumName"></param>
  518. /// <returns></returns>
  519. public DataTable GetTableByFieldOnUniqueValSum(string _filed, string _filedName, string _sumName)
  520. {
  521. Dictionary<string, double> result = UniqueValSum(_filedName, _sumName);
  522. DataTable dt = new DataTable()
  523. {
  524. TableName = m_DatasetVector.Name
  525. }; ;
  526. dt.Columns.Add(_filed);
  527. foreach (string key in result.Keys)
  528. {
  529. string tmpKey = string.IsNullOrEmpty(key) ? "未知" : key;
  530. if (!dt.Columns.Contains(tmpKey))
  531. dt.Columns.Add(tmpKey);
  532. }
  533. var rows = dt.NewRow();
  534. rows[0] = m_DatasetVector.Name;
  535. dt.Rows.Add(rows);
  536. var rowIndex = 0;
  537. var drs = dt.Rows[rowIndex++];
  538. foreach (string key in result.Keys)
  539. {
  540. string tmpKey = string.IsNullOrEmpty(key) ? "未知" : key;
  541. drs[tmpKey] = result[key].ToString().Split('.')[0];
  542. }
  543. return dt;
  544. }
  545. public DataTable GetValCountSumByField(string _filedCount, string _filedSum, string[] _rowTitle)
  546. {
  547. List<Dictionary<string, int>> results = new List<Dictionary<string, int>>();
  548. //1 查询该字段的所有值和每个值的数量汇总
  549. var resultUniqueValCount = UniqueValCount(_filedCount);
  550. var resultUniqueValSum = UniqueValSumInt(_filedCount, _filedSum);
  551. results.Add(resultUniqueValCount);
  552. results.Add(resultUniqueValSum);
  553. //2 根据查询到的Dictionary开始转换为DataTable
  554. DataTable dt = new DataTable()
  555. {
  556. TableName = m_DatasetVector.Name
  557. };
  558. //2.1 DataTable增加标题行,显示所有的字段
  559. dt.Columns.Add(_rowTitle[0]);
  560. foreach (var key in results[0].Keys)
  561. {
  562. string tmpKey = string.IsNullOrEmpty(key) ? "未知" : key.ToString();
  563. if (!dt.Columns.Contains(tmpKey))
  564. dt.Columns.Add(tmpKey);
  565. }
  566. //2.2 DataTable增加数据行,显示所有的字段对应的数量汇总
  567. DataRow dr;
  568. int rowIndex = 0;
  569. foreach (var result in results)
  570. {
  571. dr = dt.NewRow();
  572. dt.Rows.Add(dr);
  573. dt.Rows[rowIndex][0] = _rowTitle[rowIndex + 1];
  574. foreach (var key in result.Keys)
  575. {
  576. string tmpKey = string.IsNullOrEmpty(key) ? "未知" : key.ToString();
  577. dt.Rows[rowIndex][tmpKey] = result[key];
  578. }
  579. rowIndex++;
  580. }
  581. return dt;
  582. }
  583. public DataTable GetValCountSumByFieldOrder(string _filedCount, string _filedSum, string[] _rowTitle)
  584. {
  585. List<Dictionary<string, int>> results = new List<Dictionary<string, int>>();
  586. //1 查询该字段的所有值和每个值的数量汇总
  587. Dictionary<int, int> resultValOrderCount = UniqueValOrderCount(_filedCount);
  588. Dictionary<string, int> resultUniqueValCount = resultValOrderCount.ToDictionary(o => o.Key.ToString(), p => p.Value);
  589. Dictionary<string, int> resultUniqueValSum = UniqueValSumInt(_filedCount, _filedSum);
  590. results.Add(resultUniqueValCount);
  591. results.Add(resultUniqueValSum);
  592. //2 根据查询到的Dictionary开始转换为DataTable
  593. DataTable dt = new DataTable() { TableName = m_DatasetVector.Name };
  594. //2.1 DataTable增加标题行,显示所有的字段
  595. dt.Columns.Add(_rowTitle[0]);
  596. foreach (var key in results[0].Keys)
  597. {
  598. string a = key.ToString();
  599. string tmpKey = a == "-1" ? "未知" : a;
  600. if (!dt.Columns.Contains(tmpKey))
  601. {
  602. dt.Columns.Add(tmpKey);
  603. }
  604. }
  605. //2.2 DataTable增加数据行,显示所有的字段对应的数量汇总
  606. DataRow dr;
  607. int rowIndex = 0;
  608. foreach (var result in results)
  609. {
  610. dr = dt.NewRow();
  611. dt.Rows.Add(dr);
  612. dt.Rows[rowIndex][0] = _rowTitle[rowIndex + 1];
  613. foreach (var key in result.Keys)
  614. {
  615. string a = key.ToString();
  616. string tmpKey = a == "-1" ? "未知" : a;
  617. dt.Rows[rowIndex][tmpKey] = result[key];
  618. }
  619. rowIndex++;
  620. }
  621. return dt;
  622. }
  623. /// <summary>
  624. /// 二维地图集 按照表名、列名返回该列名的字段值及其汇总数量的数据表
  625. /// 里面调用UniqueValCount方法。返回列名的字段值的汇总数量
  626. /// </summary>
  627. /// <param name="_file"></param>
  628. /// <param name="_fileName"></param>
  629. /// <returns></returns>
  630. public DataTable GetTableByFieldOnUniqueValCount(string _filed, string _filedName)
  631. {
  632. //1 查询该字段的所有值和每个值的数量汇总
  633. Dictionary<string, int> result = UniqueValCount(_filedName);
  634. //2 根据查询到的Dictionary开始转换为DataTable
  635. DataTable dt = new DataTable()
  636. {
  637. TableName = m_DatasetVector.Name
  638. };
  639. //2.1 DataTable增加第一行,显示所有的字段
  640. dt.Columns.Add(_filed);
  641. foreach (var key in result.Keys)
  642. {
  643. string tmpKey = string.IsNullOrEmpty(key) ? "未知" : key.ToString();
  644. if (!dt.Columns.Contains(tmpKey))
  645. dt.Columns.Add(tmpKey);
  646. }
  647. //2.2 DataTable增加第二行,显示所有的字段对应的数量汇总
  648. var dr = dt.NewRow();
  649. dr[0] = m_DatasetVector.Name;
  650. dt.Rows.Add(dr);
  651. var rowIndex = 0;
  652. var drs = dt.Rows[rowIndex++];
  653. foreach (var key in result.Keys)
  654. {
  655. string tmpKey = string.IsNullOrEmpty(key) ? "未知" : key.ToString();
  656. drs[tmpKey] = result[key];
  657. }
  658. return dt;
  659. }
  660. /// <summary>
  661. /// 二维地图集 按照表名、列名返回该列名的字段值及其汇总数量的数据表,并按照列名排序。
  662. /// 里面调用UniqueValOrderCount方法。返回列名的字段值的汇总数量
  663. /// </summary>
  664. /// <param name="_filed"></param>
  665. /// <param name="_filedName"></param>
  666. /// <returns></returns>
  667. public DataTable GetTableByFieldOnUniqueValCountOrder(string _filed, string _filedName)
  668. {
  669. Dictionary<int, int> result = UniqueValOrderCount(_filedName);
  670. DataTable dt = new DataTable()
  671. {
  672. TableName = m_DatasetVector.Name
  673. };
  674. dt.Columns.Add(_filed);
  675. foreach (var key in result.Keys)
  676. {
  677. var tmpKey = key;
  678. if (!dt.Columns.Contains(tmpKey.ToString()))
  679. dt.Columns.Add(tmpKey.ToString());
  680. }
  681. var dr = dt.NewRow();
  682. dr[0] = m_DatasetVector.Name;
  683. dt.Rows.Add(dr);
  684. var rowIndex = 0;
  685. var drs = dt.Rows[rowIndex++];
  686. foreach (var key in result.Keys)
  687. {
  688. string tmpKey = key.ToString();
  689. drs[tmpKey] = result[key];
  690. }
  691. return dt;
  692. }
  693. /// <summary>
  694. /// 二维地图集 按照表字段名称,统计字段值DISTINCT及其数量COUNT
  695. /// </summary>
  696. /// <param name="fieldName"></param>
  697. /// <returns></returns>
  698. public Dictionary<string, int> UniqueValCount(string fieldName)
  699. {
  700. Dictionary<string, int> result = new Dictionary<string, int>();
  701. resultFields.Add(fieldName);
  702. m_HasGeometry = true;
  703. Recordset recordset = QueryToRecordset("1=1");
  704. try
  705. {
  706. recordset.MoveFirst();
  707. Int32 length = recordset.RecordCount;
  708. for (int i = 0; i < length; i++)
  709. {
  710. var val = recordset.GetString(fieldName);
  711. if (string.IsNullOrEmpty(val))
  712. val = "未知";
  713. if (!result.ContainsKey(val))
  714. {
  715. result.Add(val, 1);
  716. }
  717. else
  718. {
  719. result[val] += 1;
  720. }
  721. recordset.MoveNext();
  722. }
  723. result = result.OrderBy(o => o.Value).ToDictionary(o => o.Key, p => p.Value);
  724. }
  725. catch (Exception ex)
  726. {
  727. Console.WriteLine(ex.Message);
  728. }
  729. finally
  730. {
  731. if (recordset != null) { recordset.Close(); recordset.Dispose(); }
  732. }
  733. return result;
  734. }
  735. /// <summary>
  736. /// 二维地图集 按照表字段名称,统计字段值DISTINCT及其数量COUNT,并按照字段值转为int排序
  737. /// </summary>
  738. /// <param name="fieldName"></param>
  739. /// <returns></returns>
  740. public Dictionary<int, int> UniqueValOrderCount(string fieldName)
  741. {
  742. Dictionary<int, int> result = new Dictionary<int, int>();
  743. resultFields.Add(fieldName);
  744. m_HasGeometry = true;
  745. Recordset recordset = QueryToRecordset("1=1");// 查询得到结果记录集
  746. try
  747. {
  748. recordset.MoveFirst();
  749. Int32 length = recordset.RecordCount;
  750. for (int i = 0; i < length; i++)
  751. {
  752. var val = recordset.GetString(fieldName);
  753. if (string.IsNullOrEmpty(val))
  754. val = "-1";
  755. int orderField = 0;
  756. int.TryParse(val, out orderField);
  757. if (!result.ContainsKey(orderField))
  758. {
  759. result.Add(orderField, 0);
  760. }
  761. result[orderField] += 1;
  762. recordset.MoveNext();
  763. }
  764. result = result.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
  765. }
  766. catch (Exception ex)
  767. {
  768. Console.WriteLine(ex.Message);
  769. }
  770. finally
  771. {
  772. if (recordset != null) { recordset.Close(); recordset.Dispose(); }
  773. }
  774. return result;
  775. }
  776. /// <summary>
  777. /// 二维地图集 按照表字段名称,统计字段值DISTINCT及其数量SUM
  778. /// </summary>
  779. /// <param name="fieldName"></param>
  780. /// <returns></returns>
  781. public Dictionary<string, double> UniqueValSum(string fieldName, string fieldSum)
  782. {
  783. Dictionary<string, double> result = new Dictionary<string, double>();
  784. resultFields.Add(fieldName);
  785. resultFields.Add(fieldSum);
  786. m_HasGeometry = true;
  787. Recordset recordset = QueryToRecordset("1=1");// 查询得到结果记录集
  788. try
  789. {
  790. recordset.MoveFirst();
  791. Int32 length = recordset.RecordCount;
  792. for (int i = 0; i < length; i++)
  793. {
  794. var val = recordset.GetString(fieldName);
  795. if (string.IsNullOrEmpty(val))
  796. val = "未知";
  797. if (!result.ContainsKey(val))
  798. {
  799. result.Add(val, 0);
  800. }
  801. double valDouble = 0.00d;
  802. double.TryParse(recordset.GetString(fieldSum).ToString(), out valDouble);
  803. result[val] += valDouble;
  804. recordset.MoveNext();
  805. }
  806. result = result.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
  807. }
  808. catch (Exception ex)
  809. {
  810. Console.WriteLine(ex.Message);
  811. }
  812. finally
  813. {
  814. if (recordset != null) { recordset.Close(); recordset.Dispose(); }
  815. }
  816. return result;
  817. }
  818. /// <summary>
  819. /// 二维地图集 按照表字段名称,统计字段值DISTINCT及其数量SUM
  820. /// </summary>
  821. /// <param name="fieldName"></param>
  822. /// <param name="fieldSum"></param>
  823. /// <returns></returns>
  824. public Dictionary<string, int> UniqueValSumInt(string fieldName, string fieldSum)
  825. {
  826. Dictionary<string, int> result = new Dictionary<string, int>();
  827. resultFields.Add(fieldName);
  828. resultFields.Add(fieldSum);
  829. m_HasGeometry = true;
  830. Recordset recordset = QueryToRecordset("1=1");// 查询得到结果记录集
  831. try
  832. {
  833. recordset.MoveFirst();
  834. Int32 length = recordset.RecordCount;
  835. for (int i = 0; i < length; i++)
  836. {
  837. var val = recordset.GetString(fieldName);
  838. if (string.IsNullOrEmpty(val))
  839. val = "未知";
  840. if (!result.ContainsKey(val))
  841. {
  842. result.Add(val, 0);
  843. }
  844. int valInt = 0;
  845. int.TryParse(recordset.GetString(fieldSum).Split('.')[0], out valInt);
  846. result[val] += valInt;
  847. recordset.MoveNext();
  848. }
  849. //result = result.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
  850. }
  851. catch (Exception ex)
  852. {
  853. Console.WriteLine(ex.Message);
  854. }
  855. finally
  856. {
  857. if (recordset != null) { recordset.Close(); recordset.Dispose(); }
  858. }
  859. return result;
  860. }
  861. /// <summary>
  862. /// 二维地图集 按照表字段名称,统计字段值DISTINCT及其数量SUM,并按照字段值转为int排序
  863. /// </summary>
  864. /// <param name="fieldName"></param>
  865. /// <returns></returns>
  866. public Dictionary<int, double> UniqueValOrderSum(string fieldName, string fieldSum)
  867. {
  868. Dictionary<int, double> result = new Dictionary<int, double>();
  869. resultFields.Add(fieldName);
  870. resultFields.Add(fieldSum);
  871. m_HasGeometry = true;
  872. Recordset recordset = QueryToRecordset("1=1");// 查询得到结果记录集
  873. try
  874. {
  875. recordset.MoveFirst();
  876. Int32 length = recordset.RecordCount;
  877. for (int i = 0; i < length; i++)
  878. {
  879. double valDouble = 0.00d;
  880. string val = recordset.GetString(fieldName);
  881. if (string.IsNullOrEmpty(val))
  882. val = "未知";
  883. int orderField = 0;
  884. int.TryParse(val, out orderField);
  885. if (!result.ContainsKey(orderField))
  886. {
  887. result.Add(orderField, 0);
  888. }
  889. double.TryParse(recordset.GetString(fieldSum).ToString(), out valDouble);
  890. result[orderField] += valDouble;
  891. recordset.MoveNext();
  892. }
  893. result = result.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
  894. }
  895. catch (Exception ex)
  896. {
  897. Console.WriteLine(ex.Message);
  898. }
  899. finally
  900. {
  901. if (recordset != null) { recordset.Close(); recordset.Dispose(); }
  902. }
  903. return result;
  904. }
  905. /// <summary>
  906. /// 判断该字段的数据是否存在NULL值。不存在返回true;存在Nul值返回false。
  907. /// </summary>
  908. /// <param name="fieldName"></param>
  909. /// <returns></returns>
  910. public bool NullValHas(string fieldName)
  911. {
  912. resultFields.Add(fieldName);
  913. m_HasGeometry = false;
  914. var dt = this.Query(fieldName + " is null");
  915. if (dt.Rows.Count > 0)
  916. return true;
  917. return false;
  918. }
  919. /// <summary>
  920. /// 该字段存在的Null值的数量
  921. /// </summary>
  922. /// <param name="fieldName"></param>
  923. /// <returns></returns>
  924. public int NullValCount(string fieldName)
  925. {
  926. resultFields.Add(fieldName);
  927. m_HasGeometry = false;
  928. var dt = this.Query(fieldName + " is null");
  929. return dt.Rows.Count;
  930. }
  931. }
  932. }