ArcEngine開發_新增欄位,資料刪除,插入,更新細節
阿新 • • 發佈:2019-01-26
一、AE 向已存在的要素類中新增欄位
在向已存在的要素類中新增欄位的時候,需要用到ICLASS介面。於是,進一步的調整程式碼如下,問題得以解決
static void AddFiled(IFeatureClass pFC,string name) { DeleteFile(pFC, name); IFields pFields = pFC.Fields; IClass pClass = pFC as IClass; IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = name; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; pClass.AddField(pField); }
直接新增到Ifields是不成功的
static void AddFiled(IFeatureClass pFC,string name) { IFields pFields = pFC.Fields; IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = name; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; pFields.AddField(pField); }
二、AE高效處理
2.1屬性高效處理
連結:屬性的更新
運用IFeature和IRow的Store方法更新速度都很慢,用ICursor 的UpdateRow方法速度很快,分別是前兩者效率的184倍、159倍!!
ICursor pCursor =pTable.Update(null, false); pRow = pCursor.NextRow(); for (int i = 0; i < pTable.RowCount(null); i++) { pRow.set_Value(2, i + 6); pCursor.UpdateRow(pRow); pRow = pCursor.NextRow(); }
大量資料時不建議使用以下方式
//方法一:
feature.set_Value(2, i);
feature.Store();
//方法二:
ITable pTable = pLayer.FeatureClass as ITable;
IRow pRow = pTable.GetRow(i);
pRow.set_Value(2, i + 6);
2.2批量刪除要素
///<summary>
///快速刪除某featurelayer中所有feature
///</summary>
///<param name="pLayer">操作的塗層</param>
///<remarks>該方法可以給一個queryfilter,進行刪除符合條件的features</remarks>
public static void DeleteAllFeatures(IFeatureClass featCls, IQueryFilter queryFilter)
{
ITable pTable = featCls as ITable;
pTable.DeleteSearchedRows(queryFilter);
}
2.3快速批量插入要素
使用IFeatureBuffer
public static void InsertFeaturesUsingCursor(IFeatureClass featureClass, List<
IGeometry> geometryList)
{
//using (ComReleaser comReleaser = new ComReleaser())
//{
// Create a feature buffer.
IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
//comReleaser.ManageLifetime(featureBuffer);
// Create an insert cursor.
IFeatureCursor insertCursor = featureClass.Insert(true);
//comReleaser.ManageLifetime(insertCursor);
// All of the features to be created are classified as Primary Highways.
int typeFieldIndex = featureClass.FindField("TYPE");
featureBuffer.set_Value(typeFieldIndex, "Primary Highway");
foreach (IGeometry geometry in geometryList)
{
// Set the feature buffer's shape and insert it.
featureBuffer.Shape = geometry;
insertCursor.InsertFeature(featureBuffer);
}
// Flush the buffer to the geodatabase.
insertCursor.Flush();
// }
}
三、AE ArcEngine 嘗試讀取或寫入受保護的記憶體。這通常指示其他記憶體已損壞。
今天在開發時遇到一個問題:" 未處理 System.AccessViolationException Message="嘗試讀取或寫入受保護的記憶體。這通常指示其他記憶體已損壞。"
這通常指示其他記憶體已損壞。不知大家遇到過沒有,這種錯誤是由於使用者反覆寫入或開啟檔案,
最終使得記憶體資源沒有及時釋放導致此錯誤。這種錯誤是無法跟蹤的,必須強制釋放!
沒有強制的釋放方法,底層的com元件根據你程序呼叫元件的次數來控制物件的增加和釋放(物件釋放會延遲,
com自 身的問題)。一般是呼叫強制垃圾回收或ao自帶的回收物件的方法,效果不明顯。解決方法如下:
1.將IIS重新啟動。(我是按此方法解決的。)
2.關閉VS程序,退出VS2005.
3.上述方法若不行的話,可以自己在程式裡強制釋放COM資源,呼叫Marshal.ReleaseComObject()方法將不再使用的物件釋放掉。