1. 程式人生 > >修改Feature的屬性表的值

修改Feature的屬性表的值

首先要獲取Featureclass,利用Featureclass獲取IWorkspace和IWorkspaceEdit:

                IWorkspace workspace = ((IDataset)pFeatureClass).Workspace;
                IWorkspaceEdit workspaceEdit = workspace as IWorkspaceEdit;

然後開啟編輯狀態:

                bool startEdit = workspaceEdit.IsBeingEdited();
                if (!startEdit)
                {
                    workspaceEdit.StartEditing

(false);
                }
                workspaceEdit.StartEditOperation();

然後獲取IFeatureCursor和IFeature:

                pFeatureCursor = pFeatureClass.Search(pQueryFilter, false);

                //注意這裡一定要為false,

                //如果為true可能會產生0x80040958的錯誤,遍歷每一個Feature
                pFeature = pFeatureCursor.NextFeature();

然後用IFeature.set_Value()方法對Feature進行屬性表值的修改:

                 pFeature.set_Value(i, valueToSet);

修改完成,要用IFeature.store()方法儲存:

                pFeature.Store();

然後最好將Feature釋放掉,再進行接下來的修改:

                System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeature);
                pFeature = null;
                pFeature = pFeatureCursor.NextFeature();

接下來用IFeatureCursor.Flush():

                pFeatureCursor.Flush();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);

最後停止編輯狀態:
                workspaceEdit.StopEditOperation();
                startEdit = workspaceEdit.IsBeingEdited();
                if (!startEdit)
                {
                    workspaceEdit.StopEditing(true);
                }