This topic discusses how to use edit sessions and edit operations to manage the creation, modification, and deletion of data in a geodatabase.
public void CreateRowInEditSession(IWorkspace workspace, ITable table) { // Cast the workspace to the IWorkspaceEdit interface. IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace; // Start an edit session. An undo/redo stack isn't necessary in this case. workspaceEdit.StartEditing(false); // Start an edit operation. workspaceEdit.StartEditOperation(); // Create a row. The row's attribute values should be set here and if // a feature is being created, the shape should be set as well. IRow row = table.CreateRow(); row.Store(); // Save the edit operation. To cancel an edit operation, the AbortEditOperation // method can be used. workspaceEdit.StopEditOperation(); // Stop the edit session. The saveEdits parameter indicates the edit session // will be committed. workspaceEdit.StopEditing(true); }
public void EditWithReconcile(IWorkspace workspace, ITable table) { // Cast the workspace to the IMultiuserWorkspaceEdit and IWorkspaceEdit2 interfaces. IMultiuserWorkspaceEdit muWorkspaceEdit = (IMultiuserWorkspaceEdit)workspace; IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace; // Start a versioned edit session and an edit operation. muWorkspaceEdit.StartMultiuserEditing (esriMultiuserEditSessionMode.esriMESMVersioned); workspaceEdit.StartEditOperation(); // Perform edits here... IRow row = table.CreateRow(); row.Store(); // Save the edit operation. To cancel an edit operation, the AbortEditOperation // method can be used. workspaceEdit.StopEditOperation(); try { // Stop the edit session. The saveEdits parameter indicates the edit session // will be committed. workspaceEdit.StopEditing(true); } catch (COMException comExc) { if (comExc.ErrorCode == (int)fdoError.FDO_E_VERSION_REDEFINED) { // Get the version name. IVersion version = (IVersion)workspace; String versionName = version.VersionName; // Reconcile the version. Modify this code to reconcile and handle conflicts // appropriately for the specific application. IVersionEdit4 versionEdit4 = (IVersionEdit4)workspace; versionEdit4.Reconcile4(versionName, true, false, true, true); // Stop the edit session. workspaceEdit.StopEditing(true); } else { // A different error has occurred. Handle appropriately for the application. workspaceEdit.StopEditing(false); } } }
public void CreateRowInNonVersionedEditSession(IWorkspace workspace, ITable table) { // Cast the workspace to the IWorkspaceEdit2 and IMultiuserWorkspaceEdit interfaces. IWorkspaceEdit2 workspaceEdit2 = (IWorkspaceEdit2)workspace; IMultiuserWorkspaceEdit muWorkspaceEdit = (IMultiuserWorkspaceEdit)workspace; // Make sure that non-versioned editing is supported. If not, throw an exception. if (!muWorkspaceEdit.SupportsMultiuserEditSessionMode (esriMultiuserEditSessionMode.esriMESMNonVersioned)) { throw new ArgumentException( "The workspace does not support non-versioned editing."); } // Start a non-versioned edit session. muWorkspaceEdit.StartMultiuserEditing (esriMultiuserEditSessionMode.esriMESMNonVersioned); // Create a row. The row's attribute values should be set here, and if // a feature is being created, the shape should be set as well. IRow row = table.CreateRow(); row.Store(); // Stop the edit session. The saveEdits parameter indicates the edit session // will be committed. workspaceEdit2.StopEditing(true); }
public void SafelyCreateRowInEditSession(IWorkspace workspace, ITable table) { // Cast the workspace to the IWorkspaceEdit2 interface. IWorkspaceEdit2 workspaceEdit2 = (IWorkspaceEdit2)workspace; try { // Start an edit session and operation. workspaceEdit2.StartEditing(false); workspaceEdit2.StartEditOperation(); // Create a row. The row's attribute values should be set here, and if // a feature is being created, the shape should be set as well. IRow row = table.CreateRow(); row.Store(); // Save the edit operation and session. workspaceEdit2.StopEditOperation(); workspaceEdit2.StopEditing(true); } catch (COMException comExc) { // To handle the error depending on its severity, use a switch statement // to check its error code. switch (comExc.ErrorCode) { case ((int)fdoError.FDO_E_NOT_EDITABLE_EDITSESSIONMODE): // For example... break; default: // Handle appropriately. break; } } catch (Exception exc) { // Handle exceptions other than Component Object Model (COM) exceptions. // For example, if there is a possibility of a NotImplementedException being thrown. } finally { // If an exception was raised, make sure the edit operation and // edit session are discarded. try { if (workspaceEdit2.IsInEditOperation) { workspaceEdit2.AbortEditOperation(); } if (workspaceEdit2.IsBeingEdited()) { workspaceEdit2.StopEditing(false); } } catch (Exception exc) { // Log or ignore errors that occur at this point. } } }
public void EditDifferentVersion(IWorkspace workspace) { // Cast the workspace to the IVersionedWorkspace interface and get // the QA version. IVersionedWorkspace versionedWorkspace = (IVersionedWorkspace)workspace; IVersion qaVersion = versionedWorkspace.FindVersion("QA"); // Start an edit session on the QA version. IMultiuserWorkspaceEdit muWorkspaceEdit = (IMultiuserWorkspaceEdit)qaVersion; IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)qaVersion; muWorkspaceEdit.StartMultiuserEditing (esriMultiuserEditSessionMode.esriMESMVersioned); // Perform any edits in edit operations here... // Stop the edit session. workspaceEdit.StopEditing(true); }