Reading spatial data using the ESRI OLE DB provider

Description:

The ESRI OLE DB provider now supports two methods to access the spatial data in a supported data source. The spatial data may be returned in either OGIS (Open GIS Consortium) 'WKB' (Well Known Binary) format or as an ESRI geometry 'OBJECT'. The following VB code sample demonstrates how to implement both options, using Microsoft®'s ActiveX® Data Objects (ADO), when connected to a personal Geodatabase (Access) .mdb file.

How to use:
  1. WKB BINARY. The data is passed into a variable declared as a variant and the variant is then passed to the CreateGeometryFromWKBVariant method of the IGeometryFactory interface. The new geometry object may be added to a geometry bag (collection).

  2. ESRI OBJECT. The geometry object is passed directly to a variable declared as a geometry object. As before, the new geometry object may be added to a geometry bag.

  3. Uncomment which ever connection string (and the associated Do...Loop) which is appropriate for your application to read the data and call the procedure from within your application.
Private sub Get_Geometry()
  '++ Create geometry from WKB or ESRI Object
  Dim pGeomCol As IGeometryCollection
  Set pGeomCol = New GeometryBag

  Dim pGeoEnv As GeometryEnvironment
  Dim pGFact As IGeometryFactory
  
  Set pGeoEnv = New GeometryEnvironment
  Set pGFact = pGeoEnv

  Dim pEnv As IEnvelope
  Set pEnv = New Envelope

  Dim pGeom As IGeometry

  Dim sStr As String, sConString As String
  Dim WKBData as variant

  '++ Create a new ADO connection and recordset objects
  Dim Adocon as ADODB.connection
  Set AdoCon = New ADODB.Connection

  Dim Adors as adodb.recordset
  Set Adors = New ADODB.Recordset

  '++ Connection string parameters
    '++ EITHER
    '++ WKB 
  'sConString = "Provider=ESRI.GeoDB.OLEDB.1;" & _
  '"Data Source=C:\ArcGIS\arcexe81\ArcObjects Developer Kit\Samples\Data\Usa\usa.mdb;" & _
  '"Extended Properties=workspacetype=esriCore.AccessWorkspaceFactory.1;Geometry=WKB"
    '++ OR
    '++ ESRI OBJECT 
  'sConString = "Provider=ESRI.GeoDB.OLEDB.1;" & _
  '"Data Source=C:\ArcGIS\arcexe81\ArcObjects Developer Kit\Samples\Data\Usa\usa.mdb;" & _
  '"Extended Properties=workspacetype=esriCore.AccessWorkspaceFactory.1;Geometry=OBJECT"

  adocon.open sConstring

  '++ Open the recordset
  sStr = "Select * from states"
  Adors.Open sStr, Adocon, adOpenForwardOnly, adLockOptimistic

   '++ EITHER
  '++ WKB 
    'Dim lBytesRead As Long
   
    'Do Until Adors.EOF
    '   If Not IsNull(Adors.Fields.Item(1).Value) Then
    '     WKBData = Adors.Fields.Item(1).Value
    '     pGFact.CreateGeometryFromWkbVariant WKBData, pGeom, lBytesRead
    '     pEnv.Union pGeom.Envelope            '++ Aggregate the geom envelopes
    '     pGeomCol.AddGeometry pGeom
    '   End if
    '   Adors.MoveNext
    'Loop

  '++ OR
  '++ ESRI OBJECT 
    'Do Until Adors.EOF
    '   If Not IsNull(Adors.Fields.Item(1).Value) Then
    '     Set pGeom = Adors.Fields.Item(1).Value
    '     pEnv.Union pGeom.Envelope            '++ Aggregate the geom envelopes
    '     pGeomCol.AddGeometry pGeom
    '   End if
    '   Adors.MoveNext
    'Loop 
  Exit sub

  ErrorHandler:
   MsgBox "Get_Geometry" & Err.Number & Err.Description
  End sub