123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- using SuperMap.Data;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- namespace WWPipeLine.MapBasic
- {
- /// <summary>
- /// DatasetVector查询对象
- /// </summary>
- public class DatasetVectorEx
- {
- //private readonly string _layerName;
- //public DatasetVectorEx(string lyrName)
- //{
- // _layerName = lyrName;
- // resultFields.AddRange(new string[] { "smid" });
- //}
- public DatasetVectorEx(string lyrName = "")
- {
- resultFields.AddRange(new string[] { "smid" });
- }
- private DatasetVector m_DatasetVector;
- /// <summary>
- /// DatasetVectorEx中的DatasetVector矢量数据集对象
- /// </summary>
- public DatasetVector DatasetVector { set => m_DatasetVector = value; }// get => m_DatasetVector;
- private List<string> resultFields = new List<string>();
- /// <summary>
- /// 查询后,结果字段集
- /// </summary>
- public List<string> ResultFields
- {
- get => resultFields;
- set => resultFields = Commons.StringEx.setToLowerList(value);
- }
- private List<string> orderBy = new List<string>();
- /// <summary>
- /// 查询时,排序字段集
- /// </summary>
- public List<string> OrderBy
- {
- get => orderBy;
- set => orderBy = value;
- }
- private List<string> groupBy = new List<string>();
- /// <summary>
- /// 查询时,分组字段集
- /// </summary>
- public List<string> GroupBy
- {
- get => groupBy;
- set => groupBy = value;
- }
- private bool m_HasGeometry = true;
- /// <summary>
- /// HasGeometry 几何学 几何结构
- /// </summary>
- public bool HasGeometry { get => m_HasGeometry; set => m_HasGeometry = value; }
- /// <summary>
- /// 构造查询参数类,设置查询条件。并返回Recordset数据
- /// <para>使用完毕该Recordset后,必须关闭 recordset.Close(); recordset.Dispose(); </para>
- /// </summary>
- /// <param name="sqlWhere"></param>
- /// <param name="spatialQueryObject"></param>
- /// <returns></returns>
- public Recordset QueryToRecordset(string sqlWhere, object spatialQueryObject = null)
- {
- QueryParameter queryParameter = new QueryParameter
- {
- AttributeFilter = sqlWhere,
- //ResultFields = resultFields?.ToArray(),
- ResultFields = Commons.StringEx.setToLower(resultFields),
- OrderBy = orderBy?.ToArray(),
- GroupBy = groupBy?.ToArray(),
- CursorType = CursorType.Static,
- HasGeometry = this.m_HasGeometry
- };
- if (spatialQueryObject != null)
- {
- queryParameter.SpatialQueryObject = spatialQueryObject;
- queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
- }
- try
- {
- Recordset recordset = m_DatasetVector.Query(queryParameter);
- return recordset;
- }
- catch (Exception ex)
- {
- Commons.LogHelper.Fatal(ex);
- }
- return null;
- }
- /// <summary>
- /// 构造查询参数类,设置查询条件。并返回DataTable数据
- /// </summary>
- /// <param name="sqlWhere"></param>
- /// <param name="spatialQueryObject"></param>
- /// <returns></returns>
- public DataTable Query(string sqlWhere, object spatialQueryObject = null)
- {
- QueryParameter queryParameter = new QueryParameter
- {
- AttributeFilter = sqlWhere,
- //ResultFields = resultFields?.ToArray(),
- ResultFields = Commons.StringEx.setToLower(resultFields),
- OrderBy = orderBy?.ToArray(),
- GroupBy = groupBy?.ToArray(),
- CursorType = CursorType.Static,
- HasGeometry = this.m_HasGeometry
- };
- if (spatialQueryObject != null)
- {
- queryParameter.SpatialQueryObject = spatialQueryObject;
- queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
- }
- Recordset recordset = null;
- try
- {
- recordset = m_DatasetVector.Query(queryParameter);
- return ToDataTable(recordset);
- }
- catch (Exception ex)
- {
- Commons.LogHelper.Fatal(ex);
- }
- finally
- {
- if (recordset != null) { recordset.Close(); recordset.Dispose(); }
- }
- return null;
- }
- /// <summary>
- /// 根据条件返回一行一列的值。只能是一个字符串。
- /// </summary>
- /// <param name="sqlWhere"></param>
- /// <param name="resultField"></param>
- /// <param name="spatialQueryObject"></param>
- /// <returns></returns>
- public string QueryGetValueByOnlyField(string sqlWhere, string m_resultField, object spatialQueryObject = null)
- {
- QueryParameter queryParameter = new QueryParameter
- {
- AttributeFilter = sqlWhere,
- ResultFields = new string[] { m_resultField.ToLower() },
- CursorType = CursorType.Static,
- HasGeometry = this.m_HasGeometry
- };
- if (spatialQueryObject != null)
- {
- queryParameter.SpatialQueryObject = spatialQueryObject;
- queryParameter.SpatialQueryMode = SpatialQueryMode.Intersect;
- }
- string result = string.Empty;
- Recordset recordset = null;
- try
- {
- recordset = m_DatasetVector.Query(queryParameter);
- if (recordset.RecordCount == 1)
- {
- recordset.MoveFirst();
- result = recordset.GetFieldValue(m_resultField).ToString();
- }
- }
- catch (Exception ex)
- {
- Commons.LogHelper.Fatal(ex);
- }
- finally
- {
- if (recordset != null) { recordset.Close(); recordset.Dispose(); }
- }
- return result;
- }
- /// <summary>
- /// 将Recordset转为DataTable
- /// </summary>
- /// <param name="recordset"></param>
- /// <returns></returns>
- public DataTable ToDataTable(Recordset recordset)
- {
- resultFields = Commons.StringEx.setToLowerList(resultFields);
- DataTable dt = new DataTable(m_DatasetVector.Name);
- FieldInfos fieldInfos = recordset.GetFieldInfos();
- // 输出字段名 Output the field name
- Int32 count = fieldInfos.Count;
- DataColumn dataColumn;
- for (Int32 i = 0; i < count; i++)
- {
- String fieldname = fieldInfos[i].Name.ToLower();
- dataColumn = new DataColumn { ColumnName = fieldname, Caption = fieldInfos[i].Caption };
- if (resultFields != null && resultFields.Count > 0)
- {
- if (resultFields.Contains(fieldname))
- dt.Columns.Add(dataColumn);
- }
- else
- {
- dt.Columns.Add(dataColumn);
- }
- }
- if (dt.Columns.Count == 0) return dt;
- // 输出字段值 Output the field value
- recordset.MoveFirst();
- Int32 length = recordset.RecordCount;
- for (int i = 0; i < length; i++)
- {
- DataRow dr = dt.NewRow();
- var actIndex = 0;
- for (Int32 j = 0; j < count; j++)
- {
- if (resultFields != null && resultFields.Count > 0)
- {
- String fieldname = fieldInfos[j].Name.ToLower();
- if (resultFields.Contains(fieldname))
- {
- var objValue = recordset.GetFieldValue(j);
- String value = objValue?.ToString();
- dr[actIndex++] = value;
- }
- }
- else
- {
- var objValue = recordset.GetFieldValue(j);
- string value = objValue?.ToString();
- dr[actIndex++] = value;
- }
- }
- dt.Rows.Add(dr);
- recordset.MoveNext();
- }
- return dt;
- }
- }
- }
|