C#+AE要素的操作,添加、更新和删除要素
开发环境:VS2010+ArcObject10.1
功能介绍:C#+AE对要素的操作,包括添加要素,查找特定的要素并更新要素、删除要素。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using ESRI.ArcGIS.Carto; 10 using ESRI.ArcGIS.Geodatabase; 11 using ESRI.ArcGIS.Geometry; 12 using ESRI.ArcGIS.Display; 13 14 namespace Test_FieldsQuery 15 { 16 public partial class Form1 : Form 17 { 18 ILayer pLayer1; 19 IFeatureLayer pFeatureLayer1; 20 IFeatureClass pFeatureClass1; 21 22 ILayer pLayer; 23 IFeatureLayer pFeatureLayer; 24 IFeatureClass pFeatureClass; 25 26 public Form1() 27 { 28 InitializeComponent(); 29 } 30 31 private void Form1_Load(object sender, EventArgs e) 32 { 33 axMapControl1.AddShapeFile("数据\\", "AsiaCities_new.shp"); 34 axMapControl1.AddShapeFile("数据\\", "AsiaCities_old.shp"); 35 36 pLayer1 = axMapControl1.get_Layer(1); 37 pFeatureLayer1 = pLayer1 as IFeatureLayer; 38 pFeatureClass1 = pFeatureLayer1.FeatureClass; 39 40 pLayer = axMapControl1.get_Layer(0); 41 pFeatureLayer = pLayer as IFeatureLayer; 42 pFeatureClass = pFeatureLayer.FeatureClass; 43 } 44 45 #region 添加、更新和删除要素对象的操作 46 private void button4_Click(object sender, EventArgs e) 47 { 48 AddRow(pFeatureClass1); 49 } 50 51 private void button2_Click(object sender, EventArgs e) 52 { 53 IQueryFilter pQueryFilter = new QueryFilterClass(); 54 pQueryFilter.WhereClause = "\"FIRST_CONT\" = 'esri'"; 55 IFeatureCursor pFeatureCursor = pFeatureClass1.Search(pQueryFilter, false); 56 IFeature pFeature = pFeatureCursor.NextFeature(); 57 58 UpdateRow(pFeatureClass1, pFeature); 59 } 60 61 private void button3_Click(object sender, EventArgs e) 62 { 63 IQueryFilter pQueryFilter = new QueryFilterClass(); 64 pQueryFilter.WhereClause = "\"FIRST_CONT\" = 'esriChina'"; 65 IFeatureCursor pFeatureCursor = pFeatureClass1.Search(pQueryFilter, false); 66 IFeature pFeature = pFeatureCursor.NextFeature(); 67 68 DeleteRow(pFeatureClass1, pFeature); 69 } 70 /// <summary> 71 /// 添加要素 72 /// </summary> 73 /// <param name="pFeatureClass">要素类</param> 74 private void AddRow(IFeatureClass pFeatureClass) 75 { 76 ITable pTable = pFeatureClass as ITable; 77 IRow pRow; 78 int i = pTable.FindField("FIRST_CONT"); 79 //添加记录 80 pRow = pTable.CreateRow(); 81 pRow.set_Value(i, "esri");//设置属性值 82 pRow.Store(); 83 } 84 /// <summary> 85 /// 更新要素 86 /// </summary> 87 /// <param name="pFeatureClass">要素类</param> 88 /// <param name="pFeature">要更新的要素</param> 89 private void UpdateRow(IFeatureClass pFeatureClass, IFeature pFeature) 90 { 91 ITable pTable = pFeatureClass as ITable; 92 IRow pRow; 93 int i = pTable.FindField("FIRST_CONT"); 94 //更新记录 95 pRow = pTable.GetRow(pFeature.OID); 96 pRow.set_Value(i, "esriChina"); 97 pRow.Store(); 98 } 99 /// <summary> 100 /// 删除要素 101 /// </summary> 102 /// <param name="pFeatureClass">要素类</param> 103 /// <param name="pFeature">要删除的要素</param> 104 private void DeleteRow(IFeatureClass pFeatureClass, IFeature pFeature) 105 { 106 ITable pTable = pFeatureClass as ITable; 107 IRow pRow; 108 //删除记录 109 pRow = pTable.GetRow(pFeature.OID); 110 pRow.Delete(); 111 } 112 #endregion 113 } 114 }