ArcEngine连接表join
本例实现的是如何将地图中的一个FeatureLayer的属性表与另一个数据文件建立连接。
l 要点
首先需要定义两个ITable接口对象,分别用来获得地图中的属性表和需要连接的数据文件,再通过IMemoryRelationshipClassFactory.Open方法将两个ITable接口对象根据某个关键字段建立连接,
最后使用IDisplayRelationshipClass.DisplayRelationshipClass方法将显示该连接
主要用到IMemoryRelationshipClassFactory接口,IRelationshipClass接口和IDisplayRelationshipClass接口。
l 程序说明
函数Join是将当前激活的地图中名称为sLayerName的图层和路径为sFilePath、文件名为sFileName的文件按字段名为sFieldName的字段进行连接。
l 代码
Private Function Join(ByVal sLayerName As String, ByVal sFilePath As String, _ByVal sFileName As String, ByVal sFieldName As String) As Boolean
Dim pMxDocument As IMxDocument
Dim pMap As IMap
Dim pWorkspaceFactory As IWorkspaceFactory
Dim pWorkspace As IWorkspace
Dim pFeatureWorkspace As IFeatureWorkspace
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureClass As IFeatureClass
Dim pPrimaryTable As ITable
Dim pForeignTable As ITable
Dim pDisplayTable As IDisplayTable
Dim pMemoryRelationshipCF As IMemoryRelationshipClassFactory
Dim pRelationshipClass As IRelationshipClass
Dim pDisplayRelationshipC As IDisplayRelationshipClass
Dim nNumber As Integer
Dim sForeignFile As String
On Error GoTo ErrorHandler:
Join = False
sForeignFile = Dir(sFilePath & """ & sFileName)
If (sForeignFile = "") Then
MsgBox "The ForeignFile is not exist."
Exit Function
End If
Set pWorkspaceFactory = New ShapefileWorkspaceFactory
Set pWorkspace = pWorkspaceFactory.OpenFromFile(sFilePath, 0)
Set pFeatureWorkspace = pWorkspace
Set pForeignTable = pFeatureWorkspace.OpenTable(sFileName)
Set pMxDocument = ThisDocument
Set pMap = pMxDocument.FocusMap
For nNumber = 0 To pMap.LayerCount - 1
If pMap.Layer(nNumber).Name = sLayerName Then
Set pFeatureLayer = pMap.Layer(nNumber)
Exit For
End If
Next
If pFeatureLayer Is Nothing Then
MsgBox "No Layer's Name is " & sLayerName
Exit Function
End If
Set pDisplayTable = pFeatureLayer
Set pFeatureClass = pDisplayTable.DisplayTable
Set pPrimaryTable = pFeatureClass
Set pMemoryRelationshipCF = New MemoryRelationshipClassFactory
Set pRelationshipClass = pMemoryRelationshipCF.Open("TabletoLayer", pPrimaryTable, sFieldName, _
pForeignTable, sFieldName, "forward", "backward", esriRelCardinalityOneToOne)
Set pDisplayRelationshipC = pFeatureLayer
pDisplayRelationshipC.DisplayRelationshipClass pRelationshipClass, esriLeftOuterJoin
Join = True
Exit Function
ErrorHandler:
MsgBox Err.Description
End Function
Private Sub UIButtonControl1_Click()
Dim pVBProject As VBProject
On Error GoTo ErrorHandler:
Set pVBProject = ThisDocument.VBProject
Join "WorldCountries", pVBProject.FileName & ""..".."..".." & ""data", "Continents.dbf", "FID"
Exit Sub
ErrorHandler:
MsgBox Err.Description
End Sub