把IFeatureClass\ ITable转换成DataTable,效率高。

1.用IFeatureClass属性查询的方式较慢,这样速度可提高几十倍。

2.避免了hresult 0x80040952错误

        /// <summary>
        /// 将FeatClass属性表高效率转换成DataTable  
///gisrsman.cnblogs.com
/// </summary> /// <param name="featCls">输入的要素类</param> /// <param name="pQueryFilter">查询器,无则为Null</param> /// <returns></returns> public static DataTable FeatClass2DataTable(IFeatureClass featCls, IQueryFilter pQueryFilter) { DataTable pAttDT = null; string pFieldName; string pFieldValue; DataRow pDataRow; if (featCls != null) { //根据IFeatureClass字段结构初始化一个表结构 pAttDT = InitTableByFeaCls(featCls); ITable pFeatTable = featCls as ITable; int pFieldCout = pFeatTable.Fields.FieldCount; ICursor pCursor = pFeatTable.Search(pQueryFilter, false); IRow pRow = pCursor.NextRow(); while (pRow != null) { pDataRow = pAttDT.NewRow(); for (int j = 0; j < pFieldCout; j++) { pFieldValue = pRow.get_Value(j).ToString(); pFieldName = pFeatTable.Fields.get_Field(j).Name; pDataRow[pFieldName] = pFieldValue; } pAttDT.Rows.Add(pDataRow); pRow = pCursor.NextRow(); } } return pAttDT; }

注意: 不要使用pTable.GetRow(i).get_Value(j)这种方式,容易出hresult 0x80040952错误

方法二 IFeatureClass遍历属性

遍历要素法查询,效率低

        /// <summary>
        /// 将FeatClass属性表转换成DataTable
        /// </summary>
        /// <param name="featCls"></param>
        /// <param name="pQueryFilter">查询器,无则为Null</param>
        /// <returns></returns>
        public static DataTable FeatClass2DataTable(IFeatureClass featCls, IQueryFilter pQueryFilter)
        {
            DataTable pAttDT = null;
            IFeature pFeature = null;
            IFeatureCursor pFeatCur = null;
            string pFieldName;
            string pFieldValue;
            DataRow pDataRow;

            if (featCls != null)
            {
                pAttDT = InitTableByFeaCls(featCls);
                pFeatCur = featCls.Search(pQueryFilter, false);
                if (pFeatCur != null) pFeature = pFeatCur.NextFeature();

                while (pFeature != null)
                {
                    pDataRow = pAttDT.NewRow();

                    IFields pFields = pFeature.Fields;
                    int pNum = pFields.FieldCount;
                    for (int i = 0; i < pNum; i++)
                    {
                        pFieldName = pFields.get_Field(i).Name;
                        pFieldValue = pFeature.get_Value(i).ToString();
                        pDataRow[pFieldName] = pFieldValue;
                    }
                    pAttDT.Rows.Add(pDataRow);

                    pFeature = pFeatCur.NextFeature();
                }
            }
            return pAttDT;
        }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-23
  • 2021-10-01
  • 2021-09-23
  • 2021-06-20
  • 2022-12-23
猜你喜欢
  • 2021-12-14
  • 2021-08-23
  • 2022-12-23
  • 2021-10-16
  • 2022-12-23
  • 2022-12-23
  • 2022-02-12
相关资源
相似解决方案