编辑数据库表 Editing with the geodatabase API

This topic discusses how to use edit sessions and edit operations to manage the creation, modification, and deletion of data in a geodatabase.

Starting and stopping edit sessions and operations

The  IWorkspaceEdit and IWorkspaceEdit2 interfaces can be used to start and stop edit sessions and edit operations. These interfaces are implemented by file, personal, and ArcSDE geodatabases. The IMultiuserWorkspaceEdit interface is implemented only by ArcSDE geodatabases, and can be used to start an edit session in either versioned or non-versioned editing mode (however, stopping the edit session and controlling operations must still be performed through IWorkspaceEdit).
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);
}



Versioned editing in ArcSDE geodatabases

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);
        }
    }
}

Non-versioned editing in ArcSDE geodatabases

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);
}


Best practices

The following are best practices when editing in the geodatabase.
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.
        }
    }
}


Connecting to one version and editing another

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);
}



你可能感兴趣的:(编辑数据库表 Editing with the geodatabase API)