Creating geodatabases


Summary This topic explains how to create several types of geodatabases, including personal, file, personal and workgroup ArcSDE, and two types of scratch workspaces. It also describes how to create a connection file to an ArcSDE geodatabase.

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 datasets. The following are the three types of workspaces:
  • Shapefiles and ArcInfo workspaces are examples of file system workspaces.
  • Personal and file geodatabases are examples of local geodatabase workspaces.
  • An ArcSDE geodatabase stored in a relational database management system (RDBMS), such as Oracle, DB2, Structured Query Language (SQL) Server, or Informixis an example of a remote geodatabase workspace.
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 (see Interacting with singleton objects). 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 topic are found in the DataSourcesFile library.
The Create method can be used to create a 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. NewThe 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. 

Creating a file geodatabase

The required workspace factory to create a file geodatabase is a FileGDBWorkspaceFactory. The following code example creates a file 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 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 personal geodatabase

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 connection file to an enterprise ArcSDE geodatabase

When invoked on an ArcSDE workspace factory, the Create method does not create a geodatabase; instead, it creates an ArcSDE connection file.
The following are the 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.
The following code example shows 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 personal or workgroup ArcSDE

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 create a geodatabase in a personal or workgroup ArcSDE using the DataServerManager.
Creating a geodatabase in a personal or workgroup ArcSDE creates databases on an SQL Server Express database instance. Only 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 scratch workspace

The ScratchWorkspaceFactory and FileGDBScratchWorkspaceFactory classes are used to connect to a temporary file or personal 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 FileGDBWorkspaceFactory and AccessWorkspaceFactory classes for the following two reasons:
  • The factories implement interfaces specific to scratch workspaces (IScratchWorkspaceFactory and IScratchWorkspaceFactory2). The methods and properties of these interfaces are designed specifically for temporary geodatabases.
  • Scratch workspaces were designed to be created and used within a single session. If an application is started again at a later time, there is no reliable method for retrieving data that was previously stored in a scratch workspace.
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 scratch workspace will be created and returned. Subsequent requests within the same session returns 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


See Also:

Connecting to a geodatabase




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):
Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime: Geodatabase Update