クエリ レイヤーの作成

Sub CreateQueryLayer()
    Dim pWorkspaceFactory As IWorkspaceFactory
    Set pWorkspaceFactory = New SqlWorkspaceFactory
    
    Dim pPropertySet As IPropertySet
    Set pPropertySet = New PropertySet
    
    pPropertySet.SetProperty "DBCLIENT", "sqlserver"
    pPropertySet.SetProperty "SERVERINSTANCE", "xxxxxx\sqlexpress"
    pPropertySet.SetProperty "DATABASE", "GeoDatabase"
'    pPropertySet.SetProperty "AUTHENTICATION_MODE", "DBMS"
    pPropertySet.SetProperty "AUTHENTICATION_MODE", "OSA"
'    pPropertySet.SetProperty "USER", "XXX"
'    pPropertySet.SetProperty "PASSWORD", "XXX"

'    'SDEジオデータベースをクエリ レイヤで接続したものから取得したPropertySet
'    pPropertySet.SetProperty "SERVER", "xxxxxx"
'    pPropertySet.SetProperty "INSTANCE", "sde:sqlserver:xxxxxx\sqlexpress"
'    pPropertySet.SetProperty "DBCLIENT", "sqlserver"
'    pPropertySet.SetProperty "DB_CONNECTION_PROPERTIES", "xxxxxx\sqlexpress"
'    pPropertySet.SetProperty "DATABASE", "GeoDatabase"
'    pPropertySet.SetProperty "IS_GEODATABASE", "True"
'    pPropertySet.SetProperty "AUTHENTICATION_MODE", "OSA"
'    pPropertySet.SetProperty "CONNPROP-REV", "Rev1.0"
'    pPropertySet.SetProperty "VERSION", "dbo.DEFAULT"

    Dim pWorkspace As IWorkspace
    Set pWorkspace = pWorkspaceFactory.Open(pPropertySet, 0)
    
    Dim pSqlWorkspace As ISqlWorkspace
    Set pSqlWorkspace = pWorkspace
    
    'クエリの作成
    Dim pQueryDescription As IQueryDescription
    Set pQueryDescription = pSqlWorkspace.GetQueryDescription("select * from GeoDatabase.dbo.JAPAN")
    pQueryDescription.GeometryType = esriGeometryPolygon        'ジオメトリ タイプ
    pQueryDescription.Srid = "4612"                             '座標系WKID
    Set pQueryDescription.SpatialReference = pSpatialRefernece  '座標系
    pQueryDescription.OIDFields = "OBJECTID"                    'ユニークID識別子
    
    Dim pFeatureWorkaspce As IFeatureWorkspace
    Set pFeatureWorkaspce = pWorkspace
    
    Dim pStringArray As IStringArray
    Set pStringArray = pSqlWorkspace.GetTables
    
    Dim pFeatureClass As IFeatureClass
    Set pFeatureClass = pSqlWorkspace.OpenQueryClass("%" & "JAPAN", pQueryDescription)  'クエリ
    
    'フィーチャ レイヤの追加
    Dim pMxDocument As IMxDocument
    Set pMxDocument = ThisDocument
    
    Dim pFeatureLayer As IFeatureLayer
    Set pFeatureLayer = New FeatureLayer
    pFeatureLayer.Name = "Query Layer"
    Set pFeatureLayer.FeatureClass = pFeatureClass
    
    pMxDocument.AddLayer pFeatureLayer

    pMxDocument.ActiveView.Refresh
    pMxDocument.UpdateContents

End Sub