How to create geodatabases


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 create several types of geodatabases, including personal, file, personal and workgroup ArcSDE, scratch (two types), and in-memory. It also describes how to create a connection file to an ArcSDE geodatabase, and how to create a shapefile workspace.

Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime: Geodatabase Update

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 instantiate existing datasets and to create new datasets. The following are the three types of workspaces:
 
To create a workspace, first create an appropriate workspace factory. Each workspace type has its own workspace factory. A workspace factory is a dispenser of workspaces and allows a client to create a workspace specified by the directory, file name, and/or connection properties. A workspace factory is a co-creatable 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 the non-geodatabase workspaces mentioned in this article are found in the DataSourcesFile library.
 
The Create method can be used to create a new file system workspace or local geodatabase, or to create a connection file to an ArcSDE geodatabase. The connectionProperties parameter specifies any additional connection properties needed for ArcSDE geodatabases, such as the server, instance, and so on.
 
When a connection file to an ArcSDE geodatabase is being created—if no connection properties are specified—a dialog box appears prompting the user for the required properties. The hWnd parameter gives the Create method a handle to a parent window; typically a value of 0 can be used.
 
The Create method returns an IWorkspaceName reference that can be used to open or return certain information about the workspace. The Create method cannot be used to create geodatabases in an enterprise, personal, or workgroup ArcSDE (see Creating a geodatabase in a personal or workgroup ArcSDE workspace in this topic).
 
Activator.CreateInstance vs. New
 
The examples in this article instantiate workspace factories using the Activator.CreateInstance method rather than the new keyword. This is because workspace factories are singleton COM classes; for more on this topic, see the article System.__ComObject and casting to strongly typed RCWs.

Creating a personal geodatabase workspace stored in Access

The required workspace factory to create a personal geodatabase is an AccessWorkspaceFactory. The following code example creates a personal geodatabase in the specified directory with the supplied name. The connectionProperties parameter is null as it is not required for the creation of a personal geodatabase.
 

[C#]
public static IWorkspace CreateAccessWorkspace(String path)
{
  // Instantiate an Access workspace factory and create a personal geodatabase.
  // The Create method returns a workspace name object.

  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.AccessWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  IWorkspaceName workspaceName = workspaceFactory.Create(path, "Sample.mdb",
    null, 0);

  // Cast the workspace name object to the IName interface and open the workspace.
  IName name = (IName)workspaceName;
  IWorkspace workspace = (IWorkspace)name.Open();
  return workspace;
}

[VB.NET]
Public Shared Function CreateAccessWorkspace(ByVal Path As String) As IWorkspace
' Instantiate an Access workspace factory and create a personal geodatabase.
' The Create method returns a workspace name object.

Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
Dim workspaceName As IWorkspaceName = workspaceFactory.Create(Path, "Sample.mdb", Nothing, 0)

' Cast the workspace name object to the IName interface and open the workspace.
Dim Name As IName = CType(workspaceName, IName)
Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace)
Return workspace
End Function

Creating a file geodatabase workspace

The code required to create a file geodatabase is almost identical to the code to create a personal geodatabase. Both are local geodatabase workspaces, but as stated previously, a different type of workspace factory—FileGDBWorkspaceFactory—is required. Additionally, the extension given to the database name will be different; for a file geodatabase, the extension is .gdb.
 
The following code example creates a file geodatabase in the specified directory with the supplied name and shows how to connect to it. The connectionProperties parameter is null and is not required for the creation of a file geodatabase.
 

[C#]
public static IWorkspace CreateFileGdbWorkspace(String path)
{
  // Instantiate a file geodatabase workspace factory and create a file geodatabase.
  // The Create method returns a workspace name object.

  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.FileGDBWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  IWorkspaceName workspaceName = workspaceFactory.Create(path, "Sample.gdb",
    null, 0);

  // Cast the workspace name object to the IName interface and open the workspace.
  IName name = (IName)workspaceName;
  IWorkspace workspace = (IWorkspace)name.Open();
  return workspace;
}

[VB.NET]
Public Shared Function CreateFileGdbWorkspace(ByVal Path As String) As IWorkspace
' Instantiate a file geodatabase workspace factory and create a file geodatabase.
' The Create method returns a workspace name object.

Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
Dim workspaceName As IWorkspaceName = workspaceFactory.Create(Path, "Sample.gdb", Nothing, 0)

' Cast the workspace name object to the IName interface and open the workspace.
Dim Name As IName = CType(workspaceName, IName)
Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace)
Return workspace
End Function

Creating a connection file (.sde) to an enterprise ArcSDE workspace

When invoked on an ArcSDE workspace factory, the Create method does not create a geodatabase; instead, it creates an ArcSDE connection file.
 
There are two different procedures used to create an ArcSDE geodatabase. One is used for creating ArcSDE personal and workgroup geodatabases. The other is used to create ArcSDE enterprise geodatabases. ArcSDE enterprise geodatabases are stored using separate database management system (DBMS) products. Therefore, before creating the ArcSDE geodatabase system tables and your data tables, set up the underlying DBMS. The specifics of this vary based on the type of DBMS being used; consult the DBMS documentation for setup information.
 
The following code example demonstrates how to use the Create method to make an ArcSDE connection file to an ArcSDE geodatabase:
 

[C#]
public static IWorkspaceName CreateConnectionFile(String path, 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.Create(path, "Sample.sde", propertySet, 0);
}

[VB.NET]
Public Shared Function CreateConnectionFile(ByVal Path As String, 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 IWorkspaceName
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.Create(Path, "Sample.sde", propertySet, 0)
                    End Function

Creating a geodatabase in a personal or workgroup ArcSDE workspace

The DataServerManager is used to access and administer one or more geodatabases stored on a data server. A connection can also be opened to each geodatabase in a data server by using the IWorkspaceName and IName interfaces. The following code example shows how to connect to a geodatabase stored in a data server for a personal or workgroup ArcSDE using the DataServerManager.
 
Creating a geodatabase in a personal or workgroup ArcSDE workspace creates databases on an SQL Server Express database instance. Only ArcSDE database server administrators can create ArcSDE personal or workgroup geodatabases.
 

[C#]
// dataServerName parameter should be in "<machine_name>\\<sql_instance>" format
public static void CreatePersonalOrWorkgroupArcSdeWorkspace(String
  dataServerName)
{
  // Create a data server manager object.
  IDataServerManager dataServerManager = new DataServerManagerClass();

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

  // Cast to the admin interface, check permissions, and create the geodatabase.
  IDataServerManagerAdmin dataServerManagerAdmin = (IDataServerManagerAdmin)
    dataServerManager;
  if (dataServerManagerAdmin.IsConnectedUserAdministrator)
  {
    dataServerManagerAdmin.CreateGeodatabase("LandUse", @"C:\Temp\LandUse.mdf",
      0, "", 0);

    // Create a Name object to open the workspace.
    IWorkspaceName workspaceName = dataServerManagerAdmin.CreateWorkspaceName(
      "LandUse", "VERSION", "dbo.Default");
    IName name = (IName)workspaceName;
    IWorkspace workspace = (IWorkspace)name.Open();
  }
}

[VB.NET]
' dataServerName parameter should be in "<machine_name>\<sql_instance>" format
Public Shared Sub CreatePersonalOrWorkgroupArcSdeWorkspace(ByVal dataServerName As String)
' Create a data server manager object.
Dim dataServerManager As IDataServerManager = New DataServerManagerClass()

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

' Cast to the admin interface, check permissions, and create the geodatabase.
Dim dataServerManagerAdmin As IDataServerManagerAdmin = CType(dataServerManager, IDataServerManagerAdmin)
If dataServerManagerAdmin.IsConnectedUserAdministrator Then
    dataServerManagerAdmin.CreateGeodatabase("LandUse", "C:\Temp\LandUse.mdf", 0, "", 0)
    
    ' Create a Name object to open the workspace.
    Dim workspaceName As IWorkspaceName = dataServerManagerAdmin.CreateWorkspaceName("LandUse", "VERSION", "dbo.Default")
    Dim Name As IName = CType(workspaceName, IName)
    Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace)
End If

End Sub

Creating a shapefile workspace

A ShapefileWorkspaceFactory is used to create and open folders containing shapefiles. As opposed to the geodatabase workspace factories, which create geodatabases or connection files, the ShapefileWorkspaceFactory does not create a shapefile; instead, it creates a folder or workspace for shapefiles. The following code example demonstrates how to create a shapefile workspace:
 

[C#]
public static IWorkspace CreateShapefileWorkspace(String path)
{
  // Instantiate a shapefile workspace factory and create a shapefile workspace.
  // The Create method returns a workspace name object.

  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesFile.ShapefileWorkspaceFactory");
  IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
    Activator.CreateInstance(factoryType);
  IWorkspaceName workspaceName = workspaceFactory.Create(path, 
    "StreetShapefiles", null, 0);

  // Cast the workspace name object to the IName interface and open the workspace.
  IName name = (IName)workspaceName;
  IWorkspace workspace = (IWorkspace)name.Open();
  return workspace;
}

[VB.NET]
Public Shared Function CreateShapefileWorkspace(ByVal Path As String) As IWorkspace
' Instantiate a shapefile workspace factory and create a shapefile workspace.
' The Create method returns a workspace name object.
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
Dim workspaceName As IWorkspaceName = workspaceFactory.Create(Path, "StreetShapefiles", Nothing, 0)

' Cast the workspace name object to the IName interface and open the workspace.
Dim Name As IName = CType(workspaceName, IName)
Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace)
Return workspace
End Function
Creating a shapefile workspace as described above has the same effect as creating a new directory using the Directory.CreateDirectory(String) method from the System.IO namespace.

Creating a scratch workspace

The ScratchWorkspaceFactory and FileGDBScratchWorkspaceFactory classes are used to connect to a temporary Personal or File geodatabase known as a scratch workspace. These workspaces are commonly used to store intermediate results in operations that require several steps. These factories are fundamentally different from the AccessWorkspaceFactory or FileGDBWorkspaceFactory classes for two reasons:
 
The two types of scratch workspace factories are used in a similar manner, but scratch workspaces created as file geodatabases can take advantage of their cross-platform characteristics and can handle larger amounts of data.
 
Scratch workspace factories maintain a "default" scratch workspace. The first time the default scratch workspace is requested, a new scratch workspace will be created and returned. Subsequent requests within the same session will return the same scratch workspace.
 
The following code example shows how to request the default scratch workspace:
 

[C#]
public static IWorkspace OpenScratchWorkspace()
{
  // Create a scratch workspace factory.

  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.ScratchWorkspaceFactory");
  IScratchWorkspaceFactory scratchWorkspaceFactory = (IScratchWorkspaceFactory)
    Activator.CreateInstance(factoryType);

  // Get the default scratch workspace.
  IWorkspace scratchWorkspace = scratchWorkspaceFactory.DefaultScratchWorkspace;
  return scratchWorkspace;
}

[VB.NET]
Public Shared Function OpenScratchWorkspace() As IWorkspace
' Create a scratch workspace factory.
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.ScratchWorkspaceFactory")
Dim scratchWorkspaceFactory As IScratchWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IScratchWorkspaceFactory)

' Get the default scratch workspace.
Dim scratchWorkspace As IWorkspace = scratchWorkspaceFactory.DefaultScratchWorkspace
Return scratchWorkspace
End Function
The following code example shows how to get the default scratch workspace based on a file geodatabase:
 

[C#]
public static IWorkspace OpenFileGdbScratchWorkspace()
{
  // Create a file scratch workspace factory.
  Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.FileGDBScratchWorkspaceFactory");
  IScratchWorkspaceFactory scratchWorkspaceFactory = (IScratchWorkspaceFactory)
    Activator.CreateInstance(factoryType);

  // Get the default scratch workspace.
  IWorkspace scratchWorkspace = scratchWorkspaceFactory.DefaultScratchWorkspace;
  return scratchWorkspace;
}

[VB.NET]
Public Shared Function OpenFileGdbScratchWorkspace() As IWorkspace
' Create a file scratch workspace factory.
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBScratchWorkspaceFactory")
Dim scratchWorkspaceFactory As IScratchWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IScratchWorkspaceFactory)

' Get the default scratch workspace.
Dim scratchWorkspace As IWorkspace = scratchWorkspaceFactory.DefaultScratchWorkspace
Return scratchWorkspace
End Function

Creating an in-memory workspace

An InMemoryWorkspaceFactoryis used to create a temporary workspace that is 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 last reference to the workspace is released, the workspace is destroyed and the memory released.
 
Feature classes contained in in-memory workspaces support many parts of the geodatabase model, such as cursors, selection sets, and so on. However, in-memory workspaces do not support all aspects of the geodatabase model; for example, feature datasets and all advanced datasets that require feature datasets are not supported by in-memory workspaces. The following code example shows how to create and open an in-memory workspace:
 

[C#]
public static IWorkspace CreateInMemoryWorkspace()
{
  // 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("", "MyWorkspace",
    null, 0);

  // Cast for IName and open a reference to the in-memory workspace through the name object.
  IName name = (IName)workspaceName;
  IWorkspace workspace = (IWorkspace)name.Open();
  return workspace;
}

[VB.NET]
Public Shared Function CreateInMemoryWorkspace() As IWorkspace
' 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("", "MyWorkspace", Nothing, 0)

' Cast for IName and open a reference to the in-memory workspace through the name object.
Dim Name As IName = CType(workspaceName, IName)
Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace)
Return workspace
End Function


See Also:

How to connect to a geodatabase