Supported with:
Additional library information: Contents, Object Model Diagram
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):
See the following sections for more information about this namespace:
For further information see:
Creating geodatabases
Connecting to a geodatabase
For further information see:
Query classes and cursors
Working with SQL workspaces
- Engine
- ArcView
- ArcEditor
- ArcInfo
- Server
Additional library information: Contents, Object Model Diagram
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):
ESRI.ArcGIS.DataSourcesGDB ESRI.ArcGIS.Geodatabase ESRI.ArcGIS.System (ESRI.ArcGIS.esriSystem)
The DataSourcesGDB library contains the implementation of the Geodatabase application programming interface (API) for database data sources. These data sources include file geodatabases, relational database management systems (RDBMSs) supported by ArcSDE, and Microsoft Access.
The DataSourcesGDB library is not extended by developers.
See the following sections for more information about this namespace:
- About DataSourcesGDB
- FileGDBWorkspaceFactory
- AccessWorkspaceFactory
- SdeWorkspaceFactory
- ScratchWorkspaceFactory
- FileGDBScratchWorkspaceFactory
- SqlWorkspaceFactory
- InMemoryWorkspaceFactory
- DataServerManager
About DataSourcesGDB
The most significant classes in the DataSourcesGDB library are workspace factories and the DataServerManager class. A workspace factory is a dispenser of workspaces and allows a client to connect to a workspace using a set of connection properties or a path in the file system. A workspace is a container of datasets; examples of datasets include tables, feature classes, and relationship classes. The DataSourcesGDB library has workspace factories for connecting to file geodatabases, personal geodatabases, ArcSDE geodatabases, scratch workspaces, query layer workspaces, and in-memory workspaces.
Workspace factories are singleton objects (a singleton object can only be instantiated once in a process). Each workspace factory maintains a pool of currently connected, active workspaces that are referenced by the application. Connection properties are specified using a PropertySet object and, in the case of remote workspaces, can be saved to a connection file.
The examples in this topic instantiate workspace factories using the Activator.CreateInstance method rather than the new keyword. This is because workspace factories are singleton Component Object Model (COM) classes. For more information, see Interacting with singleton objects.
For further information see:
Creating geodatabasesConnecting to a geodatabase
FileGDBWorkspaceFactory
The FileGDBWorkspaceFactory class is used to connect to file geodatabases. The following code example shows how to create and connect to a file geodatabase:
[C#]
// Create a file geodatabase workspace factory.
Type factoryType = Type.GetTypeFromProgID(
"esriDataSourcesGDB.FileGDBWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
(factoryType);
// Create a file geodatabase.
IWorkspaceName workspaceName = workspaceFactory.Create(@"C:\Data", "California",
null, 0);
// Open the geodatabase using the name object.
IName name = (IName)workspaceName;
IWorkspace workspace = (IWorkspace)name.Open();
[VB.NET]
' Create a file geodatabase workspace factory.
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
' Create a file geodatabase.Dim workspaceName As IWorkspaceName = workspaceFactory.Create("C:\Data", "California", Nothing, 0)
' Open the geodatabase using the name object.Dim Name As IName = CType(workspaceName, IName)
Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace)
AccessWorkspaceFactory
The AccessWorkspaceFactory class is used to connect to personal geodatabases. The following code example shows how to create and connect to a personal geodatabase:
[C#]
// Create a personal geodatabase workspace factory.
Type factoryType = Type.GetTypeFromProgID(
"esriDataSourcesGDB.AccessWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
(factoryType);
// Create a personal geodatabase.
IWorkspaceName workspaceName = workspaceFactory.Create(@"C:\Data", "California",
null, 0);
// Open the geodatabase using the name object.
IName name = (IName)workspaceName;
IWorkspace workspace = (IWorkspace)name.Open();
[VB.NET]
' Create a personal geodatabase workspace factory.
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
' Create a personal geodatabase.Dim workspaceName As IWorkspaceName = workspaceFactory.Create("C:\Data", "California", Nothing, 0)
' Open the geodatabase using the name object.Dim Name As IName = CType(workspaceName, IName)
Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace)
SdeWorkspaceFactory
The SdeWorkspaceFactory class is used to connect to ArcSDE geodatabases. There are a few interfaces on this workspace factory that are not implemented by other geodatabase data sources. ISetDefaultConnectionInfo and ISetDefaultConnectionInfo2 are used to specify the user name, password, and connection version of SDE workspaces when those parameters are not specified by the connection properties. This is important if you want to save a connection file that does not persist this information.
IRemoteDatabaseWorkspaceFactory is used to maintain connection files. These files, which store the connection properties used to connect to ArcSDE workspaces, commonly have a .sde extension.
The following code example shows how to connect to an Enterprise ArcSDE geodatabase using a property set of connection properties:
[C#]
// Create an ArcSDE workspace factory.
Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
(factoryType);
// Create a property set and populate it with connection properties.
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("SERVER", "tangerine");
propertySet.SetProperty("INSTANCE", "5940");
propertySet.SetProperty("DATABASE", "kashmir");
propertySet.SetProperty("USER", "bonzo");
propertySet.SetProperty("PASSWORD", "123456");
propertySet.SetProperty("VERSION", "sde.DEFAULT");
// Open the geodatabase using the property set.
IWorkspace workspace = workspaceFactory.Open(propertySet, 0);
[VB.NET]
' Create an ArcSDE workspace factory.
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
' Create a property set and populate it with connection properties.Dim propertySet As IPropertySet = New PropertySetClass()
propertySet.SetProperty("SERVER", "tangerine")
propertySet.SetProperty("INSTANCE", "5940")
propertySet.SetProperty("DATABASE", "kashmir")
propertySet.SetProperty("USER", "bonzo")
propertySet.SetProperty("PASSWORD", "123456")
propertySet.SetProperty("VERSION", "sde.DEFAULT")
' Open the geodatabase using the property set.Dim workspace As IWorkspace = workspaceFactory.Open(propertySet, 0)
ScratchWorkspaceFactory
The ScratchWorkspaceFactory class is used to connect to temporary personal geodatabases. These workspaces are commonly used to hold the results of a selection set or an analysis operation. This workspace factory is fundamentally different than the AccessWorkspaceFactory in the following ways:
- First, there are interfaces—IScratchWorkspaceFactory and IScratchWorkspaceFactory2—on it to create temporary workspaces or to connect to existing temporary workspaces.
- Second, when the last reference to the workspace is released, the workspace is automatically deleted from the disk.
The following code example shows how to create a scratch workspace:
[C#]
// Create a scratch workspace factory.
Type factoryType = Type.GetTypeFromProgID(
"esriDataSourcesGDB.ScratchWorkspaceFactory");
IScratchWorkspaceFactory scratchWorkspaceFactory = (IScratchWorkspaceFactory)
Activator.CreateInstance(factoryType);
// Create a scratch workspace.
IWorkspace workspace = scratchWorkspaceFactory.CreateNewScratchWorkspace();
[VB.NET]
' Create a scratch workspace factory.
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.ScratchWorkspaceFactory")
Dim scratchWorkspaceFactory As IScratchWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IScratchWorkspaceFactory)
' Create a scratch workspace.Dim workspace As IWorkspace = scratchWorkspaceFactory.CreateNewScratchWorkspace()
FileGDBScratchWorkspaceFactory
The FileGDBScratchWorkspaceFactory class is used the same way as the ScratchWorkspaceFactory class, except that the underlying data source is a temporary file geodatabase rather than a personal geodatabase. The following code example shows how to create a file geodatabase scratch workspace:
[C#]
// Create a file geodatabase scratch workspace factory.
Type factoryType = Type.GetTypeFromProgID(
"esriDataSourcesGDB.FileGDBScratchWorkspaceFactory");
IScratchWorkspaceFactory scratchWorkspaceFactory = (IScratchWorkspaceFactory)
Activator.CreateInstance(factoryType);
// Create a file geodatabase scratch workspace.
IWorkspace workspace = scratchWorkspaceFactory.CreateNewScratchWorkspace();
[VB.NET]
' Create a file geodatabase scratch workspace factory.
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBScratchWorkspaceFactory")
Dim scratchWorkspaceFactory As IScratchWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IScratchWorkspaceFactory)
' Create a file geodatabase scratch workspace.Dim workspace As IWorkspace = scratchWorkspaceFactory.CreateNewScratchWorkspace()
SqlWorkspaceFactory
The SqlWorkspaceFactory class is used to connect directly to spatial databases without using ArcSDE. This functionality is exposed through ArcGIS Desktop as query layers, and through the Geodatabase API as query classes and query cursors. Note that these workspaces are not geodatabases, and do not implement many of the interfaces typically used by workspaces (for example, IFeatureWorkspace is not implemented). The following code example shows how to use this factory to connect to an Oracle 11g database:
[C#]
// Create the workspace factory.
Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SqlWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
(factoryType);
// Create the connection properties.
IPropertySet connectionProps = new PropertySetClass();
connectionProps.SetProperty("dbclient", "Oracle11g");
connectionProps.SetProperty("serverinstance", "Riverside");
connectionProps.SetProperty("authentication_mode", "DBMS");
connectionProps.SetProperty("user", "BlackDog");
connectionProps.SetProperty("password", "123456");
// Open the workspace.
IWorkspace workspace = workspaceFactory.Open(connectionProps, 0);
[VB.NET]
' Create the workspace factory.
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SqlWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
' Create the connection properties.Dim connectionProps As IPropertySet = New PropertySetClass()
connectionProps.SetProperty("dbclient", "Oracle11g")
connectionProps.SetProperty("serverinstance", "Riverside")
connectionProps.SetProperty("authentication_mode", "DBMS")
connectionProps.SetProperty("user", "BlackDog")
connectionProps.SetProperty("password", "123456")
' Open the workspace.Dim workspace As IWorkspace = workspaceFactory.Open(connectionProps, 0)
For further information see:
Query classes and cursorsWorking with SQL workspaces
InMemoryWorkspaceFactory
The InMemoryWorkspaceFactory class is used to connect to temporary workspaces that are stored in memory. These workspaces are commonly used to hold the results of an analysis operation or to hold objects in memory before persisting them to disk. When the workspace is no longer needed, it is the developer's responsibility to call IDataset.Delete on the workspace to dispose of it.
In-memory workspaces support tables and simple feature classes with geometries of point, multipoint, multipatch, polyline, and polygon. Zs and Ms are supported. Feature classes and tables contained in in-memory workspaces can be used in a similar fashion as a geodatabase dataset (for example, by using cursors and selection sets).
The following list is a description of what is not supported by in-memory workspaces. This list is not exhaustive, as it reflects some of the more commonly used aspects of the geodatabase. In-memory workspaces do not support the following:
- Feature datasets
- Relationship classes
- Subtypes and domains
- QueryDefs
- Controller and extension datasets, such as topology, geometric networks, terrains, representations, cadastral fabrics, and network datasets
- Raster catalogs and raster datasets
- Annotation and dimension feature classes
- Classes with custom class identifiers (CLSIDs) and extension class identifiers (EXTCLSIDs)
The following code example shows how to create and connect to an in-memory geodatabase:
[C#]
// Create an in-memory workspace factory.
Type factoryType = Type.GetTypeFromProgID(
"esriDataSourcesGDB.InMemoryWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
(factoryType);
// Create an in-memory workspace.
IWorkspaceName workspaceName = workspaceFactory.Create(null, "TempWS", null, 0);
// Open the workspace using the name object.
IName name = (IName)workspaceName;
IWorkspace workspace = (IWorkspace)name.Open();
[VB.NET]
' Create an in-memory workspace factory.
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.InMemoryWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
' Create an in-memory workspace.Dim workspaceName As IWorkspaceName = workspaceFactory.Create(Nothing, "TempWS", Nothing, 0)
' Open the workspace using the name object.Dim Name As IName = CType(workspaceName, IName)
Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace)
DataServerManager
The DataServerManager class is used to access and administer one or more geodatabases stored on a data server. You can also open a connection to each geodatabase in a data server through the IWorkspaceName and IName interfaces. The following code example shows how to connect to a personal ArcSDE geodatabase using DataServerManager:
[C#]
// Create a DataServerManager object.
IDataServerManager dataServerManager = new DataServerManagerClass();
// Set the server name and connect to the server.
dataServerManager.ServerName = @"evermore\sqlexpress";
dataServerManager.Connect();
// Open one of the geodatabases from the database.
IDataServerManagerAdmin dataServerManagerAdmin = (IDataServerManagerAdmin)
dataServerManager;
IWorkspaceName workspaceName = dataServerManagerAdmin.CreateWorkspaceName(
"MistyMountain", "VERSION", "dbo.Default");
IName name = (IName)workspaceName;
IWorkspace workspace = (IWorkspace)name.Open();
[VB.NET]
' Create a DataServerManager object.
Dim dataServerManager As IDataServerManager = New DataServerManagerClass()
' Set the server name and connect to the server.
dataServerManager.ServerName = "evermore\sqlexpress"
dataServerManager.Connect()
' Open one of the geodatabases from the database.Dim dataServerManagerAdmin As IDataServerManagerAdmin = CType(dataServerManager, IDataServerManagerAdmin)
Dim workspaceName As IWorkspaceName = dataServerManagerAdmin.CreateWorkspaceName("MistyMountain", "VERSION", "dbo.Default")
Dim Name As IName = CType(workspaceName, IName)
Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace)