Editing with the geodatabase API

Applications can use geodatabase edit sessions and operations to manage database transactions. Edit sessions and operations provide the following benefits:
  • Grouping edits into atomic transactions. If an error occurs before all edits are completed, the transaction can be rolled back.
  • Optional edit operation undo and redo stacks maintained by the geodatabase. After an edit operation is stopped, it is placed on the undo stack. Application developers can traverse the undo/redo stack by calling UndoEditOperation and RedoEditOperation.
  • Edit sessions and edit operations allow batched updates to occur, offering significant performance advantages when editing ArcSDE geodatabases.
  • In geodatabases that allow multiple users to edit concurrently, an application in an edit session will not recognize changes made by other applications until the session is complete.
  • The geodatabase guarantees unique instancing of row objects retrieved from the database within an edit session. Any data access call that retrieves a non-recycled object returns an in-memory instance of the object if the object has been previously instantiated in the edit session.

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值