How to connect to a geodatabase


This document was published with and applies to ArcGIS 9.3.
A 10 version also exists. A 9.2 version also exists.
Summary This topic explains how to connect to a personal geodatabase, file geodatabase, ArcSDE geodatabase, and shapefile workspaces. It also shows how to connect to a transactional or historical version of an ArcSDE geodatabase.

Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime

To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):

In this topic


About workspaces

A workspace is a container of spatial and nonspatial datasets, such as feature classes, raster datasets, and tables. Workspaces provide methods to access existing datasets and create new datasets.
 
The following are the three types of workspaces (represented by values in the esriWorkspaceType enumeration):
 
There are several methods that can be used to open a geodatabase workspace, each with its own purpose. This document reviews these methods for different types of workspaces and discusses when one can offer advantages over another.
 
To open a workspace, an appropriate workspace factory must be created. Each workspace type has its own workspace factory.
 
A workspace factory is a dispenser of workspaces and allows a client to connect to a workspace specified by a set of connection properties. A workspace factory is a cocreatable singleton object. A singleton object can only be instantiated once in a process. The workspace factory classes for geodatabase workspaces are found in the DataSourcesGDB library, while those for file-based workspaces (i.e. shapefile workspaces) can are in the DataSourcesFile library.
 
Methods for connecting to a workspace
The following are the three different ways to open a workspace after creating the appropriate workspace factory:
 
Connecting to active workspaces
Each workspace factory maintains a pool of currently connected, active workspaces that are referenced by the application. When any of the Open* methods listed above are called, the workspace factory first checks to see if a workspace has previously been opened with a matching set of properties. If so, a reference to the existing instance is returned.
 
In the case of ArcSDE workspaces, when an application connects to a workspace but omits some connection properties (such as username, password, or version), the workspace factory will check a set of "core" properties - SERVER, INSTANCE, and AUTHENTICATION_MODE - to see they match any open workspaces. If so, the workspace factory will return a reference to the matching workspace.

Connecting to a personal geodatabase workspace stored in Access

The workspace factory required to connect to a personal geodatabase is an AccessWorkspaceFactory. For local database workspaces, the IWorkspaceFactory.Open method usually requires a property set with a single property named DATABASE, the value of which should be the path to the workspace (including the filename).
 
The following code example accepts a string that is used to populate the required property to open and return a personal geodatabase workspace with the Open method:
 

[C#]
// For example, database = @"C:\myData\mypGDB.mdb".
public static IWorkspace AccessWorkspaceFromPropertySet(String database)
{
  IPropertySet propertySet = new PropertySetClass();
  propertySet.SetProperty("DATABASE", database);
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.AccessWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  return workspaceFactory.Open(propertySet, 0);
}

[VB.NET]
' For example, database = "C:\myData\mypGDB.mdb".
Public Shared Function AccessWorkspaceFromPropertySet(ByVal database As String) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()

propertySet.SetProperty("DATABASE", database)
    Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory")
    Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
    Return workspaceFactory.Open(propertySet, 0)
End Function
The OpenFromFile method for personal geodatabases requires the path name of the .mdb file that stores the Access database.
 
The following code example accepts the same string as the Open function, except it passes it directly into the OpenFromFile method instead of using it to create a property set. OpenFromFile is the most commonly used method to open personal geodatabases.
 

[C#]
// For example, path = @"C:\myData\mypGDB.mdb".
public static IWorkspace AccessWorkspaceFromPath(String path)
{
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.AccessWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  return workspaceFactory.OpenFromFile(path, 0);
}

[VB.NET]
' For example, path = "C:\myData\mypGDB.mdb".
Public Shared Function AccessWorkspaceFromPath(ByVal Path As String) As IWorkspace
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
Return workspaceFactory.OpenFromFile(Path, 0)
End Function
The last way to open a personal geodatabase is to utilize the OpenFromString method on IWorkspaceFactory2. The connection string used in this case is a collection of the name value pairs. For personal geodatabases, the collection only requires a name of DATABASE and a value of the path name to the workspace. See the following code example:
 

[C#]
// For example, connectionString = @"DATABASE=C:\myData\mypGDB.mdb".
public static IWorkspace AccessWorkspaceFromString(String connectionString)
{
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.AccessWorkspaceFactory");
  IWorkspaceFactory2 workspaceFactory2 = (IWorkspaceFactory2)
    Activator.CreateInstance(factoryType);
  return workspaceFactory2.OpenFromString(connectionString, 0);
}

[VB.NET]
' For example, connectionString = "DATABASE=C:\myData\mypGDB.mdb".
Public Shared Function AccessWorkspaceFromString(ByVal connectionString As String) As IWorkspace
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory")
Dim workspaceFactory2 As IWorkspaceFactory2 = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory2)
Return workspaceFactory2.OpenFromString(connectionString, 0)
End Function

Connecting to a file geodatabase workspace

The required workspace factory to connect to a file geodatabase is FileGDBWorkspaceFactory.
 
The required code to connect to a file geodatabase is almost identical to the code for connecting to a personal geodatabase. Both are local database workspaces, but as stated previously, a different type of workspace factory is required. Additionally, the extension used on the database will be different. For a file geodatabase, the extension is .gdb.
 
The following code example uses the Open method to return a file geodatabase workspace:
 
The only differences between file and personal geodatabase connections are the workspace factory and the extension of the database.

[C#]
// For example, database = @"C:\myData\myfGDB.gdb".
public static IWorkspace FileGdbWorkspaceFromPropertySet(String database)
{
  IPropertySet propertySet = new PropertySetClass();
  propertySet.SetProperty("DATABASE", database);
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.FileGDBWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  return workspaceFactory.Open(propertySet, 0);
}

[VB.NET]
' For example, database = "C:\myData\myfGDB.gdb".
Public Shared Function FileGdbWorkspaceFromPropertySet(ByVal database As String) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()

propertySet.SetProperty("DATABASE", database)
    Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")
    Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
    Return workspaceFactory.Open(propertySet, 0)
End Function
The OpenFromFile method can also be used to open a file geodatabase workspace. As with the personal geodatabase workspace, OpenFromFile is the most commonly used method to open a file geodatabase workspace. See the following code example:
 

[C#]
// For example, path = @"C:\myData\myfGDB.gdb".
public static IWorkspace FileGdbWorkspaceFromPath(String path)
{
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.FileGDBWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  return workspaceFactory.OpenFromFile(path, 0);
}

[VB.NET]
' For example, path = "C:\myData\myfGDB.gdb".
Public Shared Function FileGdbWorkspaceFromPath(ByVal Path As String) As IWorkspace
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
Return workspaceFactory.OpenFromFile(Path, 0)
End Function
File geodatabases can also be opened using the IWorkspaceFactory.OpenFromString method as shown in the following code example. This method is often more convenient than building the property set used with Open.
 

[C#]
// For example, connectionString = @"DATABASE=C:\myData\myfGDB.gdb".
public static IWorkspace FileGdbWorkspaceFromString(String connectionString)
{
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.FileGDBWorkspaceFactory");
  IWorkspaceFactory2 workspaceFactory2 = (IWorkspaceFactory2)
    Activator.CreateInstance(factoryType);
  return workspaceFactory2.OpenFromString(connectionString, 0);
}

[VB.NET]
' For example, connectionString = "DATABASE=C:\myData\myfGDB.gdb".
Public Shared Function FileGdbWorkspaceFromString(ByVal connectionString As String) As IWorkspace
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")
Dim workspaceFactory2 As IWorkspaceFactory2 = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory2)
Return workspaceFactory2.OpenFromString(connectionString, 0)
End Function

Connecting to an enterprise ArcSDE geodatabase workspace

SdeWorkspaceFactory is the required workspace factory to connect to an ArcSDE geodatabase. The connection properties of an esriRemoteDatabaseWorkspace specify the server and instance to connect to and can be saved in a connection file (.sde) on the file system.
 
In the case of remote database workspaces accessed via ArcSDE, the properties can include the following connection properties of the database being connected to:
Since the workspace connection can only represent one version, only one of the three version properties (VERSION, HISTORICAL_NAME, or HISTORICAL_TIMESTAMP) should be used. If no version is supplied, a connection to the default transactional version will be returned.
If any of the Open methods for a workspace are called with insufficient properties, a connection dialog box opens to request the required properties. This can be used with ArcSDE workspaces to allow the user to connect with specific credentials (for example, enter a specific user name or password).
 
Connecting to a transactional version
In the following code example, a property set is populated from supplied strings to make a connection to an ArcSDE geodatabase workspace with a transactional version. The property set is then used by the Open method to return a workspace. This approach involves the most code; however, it can be customized into a general function for connecting to ArcSDE databases based on supplied parameters from the user.
 

[C#]
// For example, server = "Kona".
// Database = "SDE" or "" if Oracle.
// Instance = "5151".
// User = "vtest".
// Password = "go".
// Version = "SDE.DEFAULT".
public static IWorkspace ConnectToTransactionalVersion(String server, String
  instance, String user, String password, String database, String version)
{
  IPropertySet propertySet = new PropertySetClass();
  propertySet.SetProperty("SERVER", server);
  propertySet.SetProperty("INSTANCE", instance);
  propertySet.SetProperty("DATABASE", database);
  propertySet.SetProperty("USER", user);
  propertySet.SetProperty("PASSWORD", password);
  propertySet.SetProperty("VERSION", version);

  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.SdeWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  return workspaceFactory.Open(propertySet, 0);
}

[VB.NET]
' For example, server = "Kona".
' Database = "SDE" or "" if Oracle.
' Instance = "5151".
' User = "vtest".
' Password = "go".
' Version = "SDE.DEFAULT".
Public Shared Function ConnectToTransactionalVersion(ByVal server As String, ByVal instance As String, _
                                                     ByVal user As String, ByVal password As String, ByVal database As String, ByVal Version As String) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()

propertySet.SetProperty("SERVER", server)

    propertySet.SetProperty("INSTANCE", instance)

        propertySet.SetProperty("DATABASE", database)

            propertySet.SetProperty("USER", user)

                propertySet.SetProperty("PASSWORD", password)

                    propertySet.SetProperty("VERSION", Version)
                        
                        Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory")
                        Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
                        Return workspaceFactory.Open(PropertySet, 0)
                    End Function
Connecting to a historical marker name
If archiving is enabled on data in an ArcSDE geodatabase, a historical version can be specified when connecting to the geodatabase. It is possible to create a named marker at some point. These markers are created with IHistoricalWorkspace and can be used to make a connection to this specific time.
 
To connect to a historical marker, augment the property set and replace the VERSION property with a HISTORICAL_NAME property. In the following code example, the name of the historical marker is Year End 2006:
 

[C#]
// For example, server = "Kona".
// database = "sde" or "" if Oracle.
// instance = "5151".
// user = "vtest".
// password = "go".
// historicalName = "Year End 2006".
public static IWorkspace ConnectoToHistoricalVersion(String server, String
  instance, String user, String password, String database, String
  historicalName)
{
  IPropertySet propertySet = new PropertySetClass();
  propertySet.SetProperty("SERVER", server);
  propertySet.SetProperty("INSTANCE", instance);
  propertySet.SetProperty("DATABASE", database);
  propertySet.SetProperty("USER", user);
  propertySet.SetProperty("PASSWORD", password);
  propertySet.SetProperty("HISTORICAL_NAME", historicalName);

  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.SdeWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  return workspaceFactory.Open(propertySet, 0);
}

[VB.NET]
' For example, server = "Kona".
' database = "sde" or "" if Oracle.
' instance = "5151".
' user = "vtest".
' password = "go".
' historicalName = "Year End 2006".
Public Shared Function ConnectoToHistoricalVersion(ByVal server As String, ByVal instance As String, _
                                                   ByVal user As String, ByVal password As String, ByVal database As String, ByVal historicalName As String) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()

propertySet.SetProperty("SERVER", server)

    propertySet.SetProperty("INSTANCE", instance)

        propertySet.SetProperty("DATABASE", database)

            propertySet.SetProperty("USER", user)

                propertySet.SetProperty("PASSWORD", password)

                    propertySet.SetProperty("HISTORICAL_NAME", historicalName)
                        
                        Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory")
                        Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
                        Return workspaceFactory.Open(propertySet, 0)
                    End Function
Connecting to a historical time stamp
It is also possible to connect at any time with the historical time stamp property. The following code example replaces the Historical_Name property with the Historical_Timestamp to connect to an ArcSDE workspace on SQL Server. It is good practice to convert a string representing a date to a date object when using it throughout the archiving API. This allows any date conversion errors to be handled before they are used to connect to a workspace.
 

[C#]
// For example, server = "Kona".
// database = "sde".
// instance = "5151".
// user = "vtest".
// password = "go".
// timestamp = "1/1/2006 12:00:01 AM".
public static IWorkspace ConnectToHistoricalTimestamp(String server, String
  instance, String user, String password, String database, String timestamp)
{
  IPropertySet propertySet = new PropertySetClass();
  propertySet.SetProperty("SERVER", server);
  propertySet.SetProperty("INSTANCE", instance);
  propertySet.SetProperty("DATABASE", database);
  propertySet.SetProperty("USER", user);
  propertySet.SetProperty("PASSWORD", password);

  // It is a good practice to convert a string representing a date to a date object when using it 
  // throughout the archiving API.
  DateTime dateTime = Convert.ToDateTime(timestamp);
  propertySet.SetProperty("HISTORICAL_TIMESTAMP", dateTime);

  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.SdeWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  return workspaceFactory.Open(propertySet, 0);
}

[VB.NET]
' For example, server = "Kona".
' database = "sde".
' instance = "5151".
' user = "vtest".
' password = "go".
' timestamp = "1/1/2006 12:00:01 AM".
Public Shared Function ConnectToHistoricalTimestamp(ByVal server As String, ByVal instance As String, _
                                                    ByVal user As String, ByVal password As String, ByVal database As String, ByVal timestamp As String) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()

propertySet.SetProperty("SERVER", server)

    propertySet.SetProperty("INSTANCE", instance)

        propertySet.SetProperty("DATABASE", database)

            propertySet.SetProperty("USER", user)

                propertySet.SetProperty("PASSWORD", password)
                    
                    ' It is a good practice to convert a string representing a date to a date object when using it
                    ' throughout the archiving API.
                    Dim dateTime As DateTime = Convert.ToDateTime(timestamp)

                    propertySet.SetProperty("HISTORICAL_TIMESTAMP", dateTime)
                        
                        Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory")
                        Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
                        Return workspaceFactory.Open(propertySet, 0)
                    End Function
If an ArcSDE connection file created in ArcCatalog (or created programmatically with IWorkspaceFactory.Create) is used to organize and distribute the connection information for an ArcSDE geodatabase, the OpenFromFile method can be used to connect to the workspace.
 
In the following code example, the path to the .sde connection file stored on disk is used to create the connection with the OpenFromFile method:
 

[C#]
// For example, connectionFile = @"C:\myData\Connection to Kona.sde".
public static IWorkspace ArcSdeWorkspaceFromFile(String connectionFile)
{
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.SdeWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  return workspaceFactory.OpenFromFile(connectionFile, 0);
}

[VB.NET]
' For example, connectionFile = "C:\myData\Connection to Kona.sde".
Public Shared Function ArcSdeWorkspaceFromFile(ByVal connectionFile As String) As IWorkspace
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
Return workspaceFactory.OpenFromFile(connectionFile, 0)
End Function
The OpenFromString method is most commonly used for convenience. An example of this is when a specific application accesses an ArcSDE geodatabase with well known connection properties. In this case, creating a string of the name-value pairs may be easier then creating a property set for the same parameters.
 
In the following code example, connection is made to the same ArcSDE instance as in the Open method example, except as a different user:
 

[C#]
// For example, connectionString = "SERVER=Kona;DATABASE=sde;INSTANCE=5151;USER=Editor;PASSWORD=Editor;VERSION=sde.DEFAULT".
public static IWorkspace ArcSdeWorkspaceFromString(String connectionString)
{
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.SdeWorkspaceFactory");
  IWorkspaceFactory2 workspaceFactory2 = (IWorkspaceFactory2)
    Activator.CreateInstance(factoryType);
  return workspaceFactory2.OpenFromString(connectionString, 0);
}

[VB.NET]
' For example, connectionString = "SERVER=Kona;DATABASE=sde;INSTANCE=5151;USER=Editor;PASSWORD=Editor;VERSION=sde.DEFAULT".
Public Shared Function ArcSdeWorkspaceFromString(ByVal connectionString As String) As IWorkspace
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory")
Dim workspaceFactory2 As IWorkspaceFactory2 = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory2)
Return workspaceFactory2.OpenFromString(connectionString, 0)
End Function

Connecting to a personal or workgroup ArcSDE geodatabase workspace

Personal and workgroup ArcSDE geodatabases are remote database workspaces and use the SdeWorkspaceFactory to make a connection to the workspace.
 
Using the workspace factory
Personal and workgroup ArcSDE geodatabases can be connected to using the same code. The code for both is also very similar to that of an enterprise ArcSDE geodatabase. Personal and workgroup ArcSDE geodatabases only support connections using direct connect and do not support the three-tier connections that an enterprise ArcSDE does.
 
Additionally, connections can only be made using OSA, where the user's operating system credentials are used for the user name and password. The geodatabases in a personal and workgroup ArcSDE are always owned by the database owner (dbo) user; the version names are prefixed by dbo.
 
The following code example contains minor differences from the code used to connect to an enterprise geodatabase. The direct connection is set up in the instance property. In this code example, the AUTHENTICATION_MODE property is also introduced. This property defaults to DBMS, which means that the DBMS will be used to authenticate the user's credentials. The authentication mode has been set to OSA instead of passing in the user name and password.
 

[C#]
// For example, for direct connect with OSA authentication.
// Server = "tivo_sqlexpress".
// Instance = "sde:sqlserver:tivo\\sqlexpress"
// Database = "sewer".
// Authentication_mode = "OSA".
// Version = "dbo.DEFAULT".
public static IWorkspace WorkgroupArcSdeWorkspaceFromPropertySet(String server,
  String instance, String authenticationMode, String database, String version)
{
  IPropertySet propertySet = new PropertySetClass();
  propertySet.SetProperty("SERVER", server);
  propertySet.SetProperty("INSTANCE", instance);
  propertySet.SetProperty("DATABASE", database);
  propertySet.SetProperty("AUTHENTICATION_MODE", authenticationMode);
  propertySet.SetProperty("VERSION", version);

  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.SdeWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  return workspaceFactory.Open(propertySet, 0);
}

[VB.NET]
' For example, for direct connect with OSA authentication.
' Server = "tivo_sqlexpress".
' Instance = "sde:sqlserver:tivo\sqlexpress"
' Database = "sewer".
' Authentication_mode = "OSA".
' Version = "dbo.DEFAULT".
Public Shared Function WorkgroupArcSdeWorkspaceFromPropertySet(ByVal server As String, ByVal instance As String, _
                                                               ByVal authenticationMode As String, ByVal database As String, ByVal Version As String) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()

propertySet.SetProperty("SERVER", server)

    propertySet.SetProperty("INSTANCE", instance)

        propertySet.SetProperty("DATABASE", database)

            propertySet.SetProperty("AUTHENTICATION_MODE", authenticationMode)

                propertySet.SetProperty("VERSION", Version)
                    
                    Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory")
                    Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
                    Return workspaceFactory.Open(propertySet, 0)
                End Function
The OpenFromFile method can also be used to connect to a personal or workgroup ArcSDE geodatabase. These files can be created manually in ArcCatalog or by using the Save Connection command after right-clicking a database server. This will create a .sde connection file in the Database Connections folder.
 
The connection files are then used in the OpenFromFile method as shown in the following code example:
 

[C#]
// For example, connectionFile = @"C:\myData\Connection to tivo.sde".
public static IWorkspace WorkgroupArcSdeWorkspaceFromFile(String connectionFile)
{
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.SdeWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  return workspaceFactory.OpenFromFile(connectionFile, 0);
}

[VB.NET]
' For example, connectionFile = "C:\myData\Connection to tivo.sde".
Public Shared Function WorkgroupArcSdeWorkspaceFromFile(ByVal connectionFile As String) As IWorkspace
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
Return workspaceFactory.OpenFromFile(connectionFile, 0)
End Function
Using the OpenFromString method to open a personal or workspace ArcSDE geodatabase only differs from opening an enterprise ArcSDE geodatabase because of the string that is passed in. Ensure that OSA is set and that direct connect is used for the instance property value pair. See the following code example:
 

[C#]
// For example, connectionString = "SERVER=tivo_sqlexpress;DATABASE=sewer;INSTANCE=sde:sqlserver:tivo\\sqlexpress;AUTHENTICATION_MODE=OSA;VERSION=dbo.DEFAULT".
public IWorkspace WorkgroupArcSdeWorkspaceFromString(String connectionString)
{
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.SdeWorkspaceFactory");
  IWorkspaceFactory2 workspaceFactory2 = (IWorkspaceFactory2)
    Activator.CreateInstance(factoryType);
  return workspaceFactory2.OpenFromString(connectionString, 0);
}

[VB.NET]
' For example, connectionString = "SERVER=tivo_sqlexpress;DATABASE=sewer;INSTANCE=sde:sqlserver:tivo\sqlexpress;AUTHENTICATION_MODE=OSA;VERSION=dbo.DEFAULT".

Public Function WorkgroupArcSdeWorkspaceFromString(ByVal connectionString As String) As IWorkspace
    Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory")
    Dim workspaceFactory2 As IWorkspaceFactory2 = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory2)
    Return workspaceFactory2.OpenFromString(connectionString, 0)
End Function
Using the DataServerManager approach for a personal or workgroup ArcSDE
The DataServerManager object is used to access and administer one or more geodatabases stored on a data server. A connection can be opened to each geodatabase in a data server using the IWorkspaceName and IName interfaces. Unlike an enterprise ArcSDE geodatabase, opening a connection to a geodatabase stored in a personal or workgroup ArcSDE does not require a set of properties. Instead, the geodatabase name, type of version, and version specifier are used.
 
There are only three types of versions that can be used to connect to the geodatabase. See the following:
The version specifier must correspond to the type of version (for example, if VERSION is specified, the version name must be listed as the version specifier). Also, since  the workspace connection can only represent one version, only one of the three version properties (VERSION, or HISTORICAL_NAME, or HISTORICAL_TIMESTAMP) should be used.
The following code example shows how to connect to a transactional version of a geodatabase stored in a DataServer for a personal or workgroup ArcSDE using the DataServerManager:
 

[C#]
// For example, serverName = "tivo\\sqlexpress"
// gdbName = "sewer"
// versionPropName = "VERSION"
// versionName = "dbo.DEFAULT"
public static IWorkspace WorkgroupArcSdeWorkspaceFromDSM(String serverName,
  String gdbName, String versionPropName, String versionName)
{
  // Create a Data Server Manager object.
  IDataServerManager dataServerManager = new DataServerManagerClass();

  // Set the server name and connect to the server.
  dataServerManager.ServerName = serverName;
  dataServerManager.Connect();

  // Open one of the geodatabases in the Database Server.
  IDataServerManagerAdmin dataServerManagerAdmin = (IDataServerManagerAdmin)
    dataServerManager;
  IWorkspaceName workspaceName = dataServerManagerAdmin.CreateWorkspaceName
    (gdbName, versionPropName, versionName);

  // Cast from the workspace name to utilize the Open method.
  IName name = (IName)workspaceName;
  IWorkspace workspace = (IWorkspace)name.Open();
  return workspace;
}

[VB.NET]
' For example, serverName = "tivo\sqlexpress"
' gdbName = "sewer"
' versionPropName = "VERSION"
' versionName = "dbo.DEFAULT"
Public Shared Function WorkgroupArcSdeWorkspaceFromDSM(ByVal serverName As String, ByVal gdbName As String, _
                                                       ByVal versionPropName As String, ByVal versionName As String) As IWorkspace
' Create a Data Server Manager object.
Dim dataServerManager As IDataServerManager = New DataServerManagerClass()

' Set the server name and connect to the server.
dataServerManager.ServerName = serverName
dataServerManager.Connect()

' Open one of the geodatabases in the Database Server.
Dim dataServerManagerAdmin As IDataServerManagerAdmin = CType(dataServerManager, IDataServerManagerAdmin)
Dim workspaceName As IWorkspaceName = dataServerManagerAdmin.CreateWorkspaceName(gdbName, versionPropName, versionName)

' Cast from the workspace name to utilize the Open method.
Dim Name As IName = CType(workspaceName, IName)
Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace)
Return workspace
End Function

Connecting to a shapefile workspace using the geodatabase API

The geodatabase is not only a physical store for geographic data, it is also the API used to access other types of geographic data with ArcGIS. Therefore, the geodatabase API can be used to access non-geodatabase workspaces such as shapefiles. Shapefiles have a workspace type of esriFileSystemWorkspace.
 
Like the geodatabase workspace, a workspace factory must be instantiated to access a shapefile workspace. Unlike geodatabases, the workspace factory for shapefiles is found in the DataSourcesFile library.
 
In the following code example, the Open method is used with a shapefile workspace factory to return the shapefile workspace. The workspace for shapefiles is the folder in which they are stored.
 

[C#]
// For example, path = @"C:\myData\shapefiles".
public static IWorkspace ShapefileWorkspaceFromPropertySet(String path)
{
  IPropertySet propertySet = new PropertySetClass();
  propertySet.SetProperty("DATABASE", path);
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesFile.ShapefileWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  return workspaceFactory.Open(propertySet, 0);
}

[VB.NET]
' For example, path = "C:\myData\shapefiles".
Public Shared Function ShapefileWorkspaceFromPropertySet(ByVal Path As String) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()

propertySet.SetProperty("DATABASE", Path)
    Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory")
    Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
    Return workspaceFactory.Open(propertySet, 0)
End Function
The OpenFromFile method can also be used to open a shapefile geodatabase workspace. This method requires only the path of the folder containing the shapefile and is often the simplest solution for connecting to a shapefile workspace. See the following code example:
 

[C#]
// For example, path = @"C:\myData\shapefiles".
public static IWorkspace ShapefileWorkspaceFromPath(String path)
{
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesFile.ShapefileWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  return workspaceFactory.OpenFromFile(path, 0);
}

[VB.NET]
' For example, path = "C:\myData\shapefiles".
Public Shared Function ShapefileWorkspaceFromPath(ByVal Path As String) As IWorkspace
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
Return workspaceFactory.OpenFromFile(Path, 0)
End Function
As with geodatabase workspaces, shapefile workspaces also support the IWorkspaceFactory2 interface and the OpenFromString method. As shown in the following code example, the input string is slightly larger than that required for the OpenFromFile method and might reduce the convenience normally associated with this method.
 

[C#]
// For example, connectionString = @"DATABASE=C:\myData\shapefiles".
public static IWorkspace ShapefileWorkspaceFromString(String connectionString)
{
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesFile.ShapefileWorkspaceFactory");
  IWorkspaceFactory2 workspaceFactory2 = (IWorkspaceFactory2)
    Activator.CreateInstance(factoryType);
  return workspaceFactory2.OpenFromString(connectionString, 0);
}

[VB.NET]
' For example, connectionString = "DATABASE=C:\myData\shapefiles".
Public Shared Function ShapefileWorkspaceFromString(ByVal connectionString As String) As IWorkspace
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory")
Dim workspaceFactory2 As IWorkspaceFactory2 = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory2)
Return workspaceFactory2.OpenFromString(connectionString, 0)
End Function

Connecting to a coverage workspace using the geodatabase API

Along with shapefiles, coverages also have a workspace type of esriFileSystemWorkspace and can be accessed (with limited functionality) through the geodatabase API.  The workspace for coverages is not a folder containing .pat and .adf files, but rather the parent directory with access to both that directory as well as the coverage's info directory.  Although all three methods for opening shapefile workspaces will work with coverages, only one code example is shown, as the process of opening coverage workspaces is the same as opening shapefile workspaces, with the exception of the type of factory used.
 
In the following code example, the Open method is used with an ArcInfo workspace factory to return the coverage workspace:
 

[C#]
// For example, path = @"C:\myData\coverages".
public static IWorkspace CoverageWorkspaceFromPropertySet(String path)
{
  IPropertySet propertySet = new PropertySetClass();
  propertySet.SetProperty("DATABASE", path);
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesFile.ArcInfoWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  return workspaceFactory.Open(propertySet, 0);
}

[VB.NET]
' For example, path = "C:\myData\coverages".
Public Shared Function CoverageWorkspaceFromPropertySet(ByVal Path As String) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()

propertySet.SetProperty("DATABASE", Path)
    Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesFile.ArcInfoWorkspaceFactory")
    Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
    Return workspaceFactory.Open(propertySet, 0)
End Function


See Also:

How to use the ESRI OLE DB provider with a geodatabase