Connect to a File Geodatabase



Description:

Making a connection to a workspace is the foundatation to accessing data. This sample will show you how to connect to a file geodatabase stored in an .gdb file. The example code is a function that takes the path to the File geodatabase as a string and returns a workspace.
Products:
Engine: VB6, VC++

Platforms: Windows

Minimum ArcGIS Release: 9.2

How to use:
  1. Paste the function in your project.
  2. Call the function from your code.
[VB6]
'' openFileDB:  connect to a File geodatabase
'' connString should be full path to the file database with .gdb extension
'' example Set pWorkspace = openFGDBWorkspace("c:\temp\geodata.gdb")
''
Public Function openFGDBWorkspace(connString As String) As IWorkspace

On Error GoTo EH

    Dim pWorkspace As IWorkspace
    Dim pWorkspaceFactory As IWorkspaceFactory

    Set openFGDBWorkspace = Nothing

    Set pWorkspaceFactory = New FileGDBWorkspaceFactory
    Set pWorkspace = pWorkspaceFactory.OpenFromFile(connString, 0)
    Set openFGDBWorkspace = pWorkspace

    Exit Function
EH:
    MsgBox Err.Description, vbInformation, "openFGDBWorkspace"
End Function


[VC++]
// connString should be the path to a .gdb folder, i.e.:
//  L"C:\\Data\\Landbase.gdb"
HRESULT openFGDBWorkspace(BSTR connString, IWorkspace** outWorkspace)
{
      IWorkspaceFactoryPtr ipWorkspaceFactory(CLSID_FileGDBWorkspaceFactory);
      return ipWorkspaceFactory->OpenFromFile(connString, NULL, outWorkspace);
}