如何增加记录

本例要实现的是如何在FeatureClass中新增一条记录(Feature)。

l   要点

通过IFeatureClass.Insert方法获得可插入记录的游标IFeatureCursor,然后使用IFeatureClass.CreateFeatureBuff方法获得IFeatureBuffer接口实例,使用IFeatureCursor.InsertFeature方法插入记录。

主要用到IFeatureCursor接口。

l   程序说明

函数OpenFeatureClass获得当前激活的Map中第一层的IFeatureClass接口对象。

函数InsertFeature在pFeatureClass中添加一条记录。

l   代码

Private Function InsertFeature(pFeatureClass As IFeatureClass) As Boolean

    Dim pFeatureCursor          As IFeatureCursor

    Dim pFeatureBuffer          As IFeatureBuffer

    Dim nFeatureNumber          As Integer

On Error GoTo ErrorHandler:

    InsertFeature = False

    If (pFeatureClass Is Nothing) Then

        Exit Function

    End If

    Set pFeatureCursor = pFeatureClass.Insert(True)

    Set pFeatureBuffer = pFeatureClass.CreateFeatureBuffer

    nFeatureNumber = -1

    pFeatureBuffer.Value(6) = "Insert Land"

    nFeatureNumber = pFeatureCursor.InsertFeature(pFeatureBuffer)

    If (nFeatureNumber <> -1) Then

        MsgBox ("添加了第" & nFeatureNumber & "条记录")

        InsertFeature = True

    Else

        MsgBox ("添加失败")

        InsertFeature = False

    End If

    Exit Function

ErrorHandler:

    MsgBox Err.Description

End Function

Private Function OpenFeatureClass() As IFeatureClass

    Dim pMxDocument             As IMxDocument

    Dim pMap                    As IMap

    Dim pFeatureLayer           As IFeatureLayer

    Dim pFeatureClass           As IFeatureClass   

On Error GoTo ErrorHandler:

    Set OpenFeatureClass = Nothing

    Set pMxDocument = ThisDocument

    Set pMap = pMxDocument.FocusMap

    If (pMap.LayerCount = 0) Then

        MsgBox ("缺少数据")

        Exit Function

    End If

    Set pFeatureLayer = pMap.Layer(0)

    Set pFeatureClass = pFeatureLayer.FeatureClass

    Set OpenFeatureClass = pFeatureClass

    Exit Function

ErrorHandler:

    MsgBox Err.Description

End Function 

Private Sub UIButtonControl1_Click()

On Error GoTo ErrorHandler:

    Dim pFeatureClass        As IFeatureClass

    Set pFeatureClass = OpenFeatureClass()

    InsertFeature pFeatureClass

    Exit Sub

ErrorHandler:

    MsgBox Err.Description

End Sub

posted on 2006-09-07 13:05  greatbird  阅读(870)  评论(0)    收藏  举报

导航