How to create new geodatabases

In this topic About workspaces Creating a personal geodatabase workspace stored in Access Creating a file geodatabase workspace Creating a connection file (.sde) to an Enterprise ArcSDE workspace Creating a geodatabase in a personal or workgroup ArcSDE workspace Creating a shapefile workspace Creating a scratch workspace Creating an InMemory workspace ---------------------------------------In this topic About workspaces Creating a personal geodatabase workspace stored in Access Creating a file geodatabase workspace Creating a connection file (.sde) to an Enterprise ArcSDE workspace Creating a geodatabase in a personal or workgroup ArcSDE workspace Creating a shapefile workspace Creating a scratch workspace Creating an InMemory workspace -------------------------------------------------------------------------------- About workspaces A workspace is a container of spatial and nonspatial datasets such as feature classes, raster datasets, and tables. It provides methods to instantiate existing datasets and to create new datasets. The following are the three types of workspaces: Shapefiles and ArcInfo workspaces are examples of esriFileSystemWorkspaces. A personal geodatabase stored in a Microsoft® Access or a file geodatabase is an example of esriLocalDatabaseWorkspace. An ArcSDE geodatabase stored in a relational database management system (RDBMS), such as Oracle®, DB2®, SQL Server™, or Informix® is an example of an esriRemoteDatabaseWorkspace. To create 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 create a workspace specified by the directory, file name, and 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. The Create method can be used to create a new esriFileSystemWorkspace, esriLocalDatabaseWorkspace, or a connection file to an esriRemoteDatabaseWorkspace. The optional connectionProperties parameter specifies any additional connection properties needed, such as the server, instance, and so on. In the case where 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 tells the Create method the control or dialog box to use. The Create method returns an IWorkspaceName object that can be used to open or return certain information about the workspace. The Create method cannot be used to create new geodatabases in an Enterprise, Personal, or Workgroup ArcSDE. Creating a personal geodatabase workspace stored in Access The workspace factory required to create a personal geodatabase is an AccessWorkspaceFactory. The following code example creates a new 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 IWorkspace CreateAccessWorkspace() { // Instantiate an Access workspace factory and create a new personal geodatabase. // The Create method returns a workspace name object. IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("C://temp//", "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 Function CreateAccessWorkspace() As IWorkspace ' Instantiate an Access workspace factory and create a new personal geodatabase. ' The Create method returns a workspace name object. Dim workspaceFactory As IWorkspaceFactory = New AccessWorkspaceFactoryClass() Dim workspaceName As IWorkspaceName = workspaceFactory.Create("C:/temp/", "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 FunctionCreating a file geodatabase workspace The code required to create a file geodatabase is almost identical to the code for creating a personal geodatabase. Both are local database workspaces, but as stated previously, a different type of workspace factory - the FileGDBWorkspaceFactory - is required. Additionally, the extension used on the database will be different; for a file geodatabase, the extension is .gdb. The following code example creates a new file geodatabase in the specified directory with the supplied name, then shows how to connect to it. The connectionProperties parameter is null as it is not required for the creation of a file geodatabase. [C#] public IWorkspace CreateFileGdbWorkspace() { // Instantiate a file geodatabase workspace factory and create a new file geodatabase. // The Create method returns a workspace name object. IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("C://temp//", "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 Function CreateFileGdbWorkspace() As IWorkspace ' Instantiate a file geodatabase workspace factory and create a new file geodatabase. ' The Create method returns a workspace name object. Dim workspaceFactory As IWorkspaceFactory = New FileGDBWorkspaceFactoryClass() Dim workspaceName As IWorkspaceName = workspaceFactory.Create("C:/temp/", "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 FunctionCreating a connection file (.sde) to an Enterprise ArcSDE workspace When used in conjunction with an ArcSDE workspace, the Create method does not create the geodatabase. Instead, it creates an SDE 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, the underlying DBMS must be set up. 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 IWorkspaceName CreateConnectionFile(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); IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2)new SdeWorkspaceFactoryClass(); return workspaceFactory.Create("c://temp//", "Sample.sde", propertySet, 0); } [VB.NET] Public Function CreateConnectionFile(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 workspaceFactory As IWorkspaceFactory2 = CType(New SdeWorkspaceFactoryClass(), IWorkspaceFactory2) Return workspaceFactory.Create("c:/temp/", "Sample.sde", propertySet, 0) End FunctionCreating 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 new databases on a SQL Server Express database instance. Only ArcSDE database server administrators can create new ArcSDE Personal or Workgroup geodatabases. [C#] // dataServerName parameter should be in " // " format public 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 " / " format Public 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 SubCreating a shapefile workspace A ShapefileWorkspaceFactory is used to create a folder in which shapefiles can be created. 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 IWorkspace CreateShapefileWorkspace() { IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2)new ShapefileWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("c://temp//", "StreetShapefiles", null, 0); IName name = (IName)workspaceName; IWorkspace workspace = (IWorkspace)name.Open(); return workspace; } [VB.NET] Public Function CreateShapefileWorkspace() As IWorkspace Dim workspaceFactory As IWorkspaceFactory2 = CType(New ShapefileWorkspaceFactoryClass(), IWorkspaceFactory2) Dim workspaceName As IWorkspaceName = workspaceFactory.Create("c:/temp/", "StreetShapefiles", Nothing, 0) Dim Name As IName = CType(workspaceName, IName) Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace) Return workspace End FunctionCreating a scratch workspace The ScratchWorkspaceFactory and FileGDBScratchWorkspaceFactory classes are used to connect to a temporary database stored in Microsoft Access or in a file geodatabase, known as a scratch workspace. These workspaces are commonly used to hold the results of a selection set or to hold the results of an analysis operation. These factories are fundamentally different from the AccessWorkspaceFactory or FileGDBWorkspaceFactory classes, and the manner in they are used is different: First, the factories implement scratch workspace-specific interfaces (IScratchWorkspaceFactory and IScratchWorkspaceFactory2) for creating new temporary workspaces or connect to existing temporary workspaces. Second, when the last reference to a scratch workspace is released, the workspace is automatically deleted from the disk. Whereas personal and file geodatabase workspaces are created much less often than they're connected to, scratch workspaces are created and connected to in the same session. The FileGDBScratchWorkspaceFactory and ScratchWorkspaceFactory classes are used in a similar fashion, but scratch workspaces based on file geodatabases can take advantage of their cross-platform. nature and can handle larger amounts of data. The following code example shows how to use the current scratch workspace to store a selection set (this example uses an Access-based scratch workspace): [C#] public IWorkspace OpenScratchWorkspace(ITable parcelTable, IQueryFilter queryFilter) { // Create a scratch workspace factory. IScratchWorkspaceFactory workspaceFactory = new ScratchWorkspaceFactoryClass() ; IScratchWorkspaceFactory2 workspaceFactory2 = workspaceFactory as IScratchWorkspaceFactory2; // Get the current scratch workspace. IWorkspace scratchWorkspace = workspaceFactory2.CurrentScratchWorkspace; // Select from an existing table, called parcelTable, using an existing query, called queryFilter, // putting the resulting selection set in the temporary workspace. ISelectionSet selSet = parcelTable.Select(queryFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, scratchWorkspace); return scratchWorkspace; } [VB.NET] Public Function OpenScratchWorkspace(ByVal parcelTable As ITable, ByVal queryFilter As IQueryFilter) As IWorkspace ' Create a scratch workspace factory. Dim workspaceFactory As IScratchWorkspaceFactory = New ScratchWorkspaceFactoryClass() Dim workspaceFactory2 As IScratchWorkspaceFactory2 = TryCast(workspaceFactory, IScratchWorkspaceFactory2) ' Get the current scratch workspace. Dim scratchWorkspace As IWorkspace = workspaceFactory2.CurrentScratchWorkspace ' Select from an existing table, called parcelTable, using an existing query, called queryFilter, ' putting the resulting selection set in the temporary workspace. Dim selSet As ISelectionSet = parcelTable.Select(queryFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, scratchWorkspace) Return scratchWorkspace End FunctionThe following code example shows how to get the current scratch workspace to use as part of a selection, using a file geodatabase: [C#] public IWorkspace OpenFileGdbScratchWorkspace(ITable parcelTable, IQueryFilter queryFilter) { // Create a file scratch workspace factory. IScratchWorkspaceFactory workspaceFactory = new FileGDBScratchWorkspaceFactoryClass(); IScratchWorkspaceFactory2 workspaceFactory2 = workspaceFactory as IScratchWorkspaceFactory2; // Get the current scratch workspace. IWorkspace scratchWorkspace = workspaceFactory2.CurrentScratchWorkspace; // Select from an existing table, called parcelTable, using an existing query, called queryFilter, // putting the resulting selection set in the temporary workspace. ISelectionSet selSet = parcelTable.Select(queryFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, scratchWorkspace); return scratchWorkspace; } [VB.NET] Public Function OpenFileGdbScratchWorkspace(ByVal parcelTable As ITable, ByVal queryFilter As IQueryFilter) As IWorkspace ' Create a file scratch workspace factory. Dim workspaceFactory As IScratchWorkspaceFactory = New FileGDBScratchWorkspaceFactoryClass() Dim workspaceFactory2 As IScratchWorkspaceFactory2 = TryCast(workspaceFactory, IScratchWorkspaceFactory2) ' Get the current scratch workspace. Dim scratchWorkspace As IWorkspace = workspaceFactory2.CurrentScratchWorkspace ' Select from an existing table, called parcelTable, using an existing query, called queryFilter, ' putting the resulting selection set in the temporary workspace. Dim selSet As ISelectionSet = parcelTable.Select(queryFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, scratchWorkspace) Return scratchWorkspace End FunctionCreating an InMemory 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 InMemory workspaces support the full geodatabase model. They can be used in conjunction with cursors, selections, and so on. However, InMemory workspaces do not support all aspects of the geodatabase model; advanced datasets such as topology and geometric networks are not supported. The following code example shows how to create and open an InMemory workspace: [C#] public IWorkspace CreateInMemoryWorkspace() { // Create an InMemory workspace factory. IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); // Create an InMemory geodatabase. IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); // Cast for IName. IName name = (IName)workspaceName; //Open a reference to the InMemory workspace through the name object. IWorkspace workspace = (IWorkspace)name.Open(); return workspace; } [VB.NET] Public Function CreateInMemoryWorkspace() As IWorkspace ' Create an InMemory workspace factory. Dim workspaceFactory As IWorkspaceFactory = New InMemoryWorkspaceFactoryClass() ' Create an InMemory geodatabase. Dim workspaceName As IWorkspaceName = workspaceFactory.Create("", "MyWorkspace", Nothing, 0) ' Cast for IName. Dim Name As IName = CType(workspaceName, IName) ' Open a reference to the InMemory workspace through the name object. Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace) Return workspace End Function----------------------------------------- About workspaces A workspace is a container of spatial and nonspatial datasets such as feature classes, raster datasets, and tables. It provides methods to instantiate existing datasets and to create new datasets. The following are the three types of workspaces: Shapefiles and ArcInfo workspaces are examples of esriFileSystemWorkspaces. A personal geodatabase stored in a Microsoft® Access or a file geodatabase is an example of esriLocalDatabaseWorkspace. An ArcSDE geodatabase stored in a relational database management system (RDBMS), such as Oracle®, DB2®, SQL Server™, or Informix® is an example of an esriRemoteDatabaseWorkspace. To create 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 create a workspace specified by the directory, file name, and 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. The Create method can be used to create a new esriFileSystemWorkspace, esriLocalDatabaseWorkspace, or a connection file to an esriRemoteDatabaseWorkspace. The optional connectionProperties parameter specifies any additional connection properties needed, such as the server, instance, and so on. In the case where 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 tells the Create method the control or dialog box to use. The Create method returns an IWorkspaceName object that can be used to open or return certain information about the workspace. The Create method cannot be used to create new geodatabases in an Enterprise, Personal, or Workgroup ArcSDE. Creating a personal geodatabase workspace stored in Access The workspace factory required to create a personal geodatabase is an AccessWorkspaceFactory. The following code example creates a new 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 IWorkspace CreateAccessWorkspace() { // Instantiate an Access workspace factory and create a new personal geodatabase. // The Create method returns a workspace name object. IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("C://temp//", "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 Function CreateAccessWorkspace() As IWorkspace ' Instantiate an Access workspace factory and create a new personal geodatabase. ' The Create method returns a workspace name object. Dim workspaceFactory As IWorkspaceFactory = New AccessWorkspaceFactoryClass() Dim workspaceName As IWorkspaceName = workspaceFactory.Create("C:/temp/", "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 FunctionCreating a file geodatabase workspace The code required to create a file geodatabase is almost identical to the code for creating a personal geodatabase. Both are local database workspaces, but as stated previously, a different type of workspace factory - the FileGDBWorkspaceFactory - is required. Additionally, the extension used on the database will be different; for a file geodatabase, the extension is .gdb. The following code example creates a new file geodatabase in the specified directory with the supplied name, then shows how to connect to it. The connectionProperties parameter is null as it is not required for the creation of a file geodatabase. [C#] public IWorkspace CreateFileGdbWorkspace() { // Instantiate a file geodatabase workspace factory and create a new file geodatabase. // The Create method returns a workspace name object. IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("C://temp//", "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 Function CreateFileGdbWorkspace() As IWorkspace ' Instantiate a file geodatabase workspace factory and create a new file geodatabase. ' The Create method returns a workspace name object. Dim workspaceFactory As IWorkspaceFactory = New FileGDBWorkspaceFactoryClass() Dim workspaceName As IWorkspaceName = workspaceFactory.Create("C:/temp/", "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 FunctionCreating a connection file (.sde) to an Enterprise ArcSDE workspace When used in conjunction with an ArcSDE workspace, the Create method does not create the geodatabase. Instead, it creates an SDE 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, the underlying DBMS must be set up. 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 IWorkspaceName CreateConnectionFile(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); IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2)new SdeWorkspaceFactoryClass(); return workspaceFactory.Create("c://temp//", "Sample.sde", propertySet, 0); } [VB.NET] Public Function CreateConnectionFile(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 workspaceFactory As IWorkspaceFactory2 = CType(New SdeWorkspaceFactoryClass(), IWorkspaceFactory2) Return workspaceFactory.Create("c:/temp/", "Sample.sde", propertySet, 0) End FunctionCreating 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 new databases on a SQL Server Express database instance. Only ArcSDE database server administrators can create new ArcSDE Personal or Workgroup geodatabases. [C#] // dataServerName parameter should be in "//" format public 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 "/" format Public 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 SubCreating a shapefile workspace A ShapefileWorkspaceFactory is used to create a folder in which shapefiles can be created. 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 IWorkspace CreateShapefileWorkspace() { IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2)new ShapefileWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("c://temp//", "StreetShapefiles", null, 0); IName name = (IName)workspaceName; IWorkspace workspace = (IWorkspace)name.Open(); return workspace; } [VB.NET] Public Function CreateShapefileWorkspace() As IWorkspace Dim workspaceFactory As IWorkspaceFactory2 = CType(New ShapefileWorkspaceFactoryClass(), IWorkspaceFactory2) Dim workspaceName As IWorkspaceName = workspaceFactory.Create("c:/temp/", "StreetShapefiles", Nothing, 0) Dim Name As IName = CType(workspaceName, IName) Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace) Return workspace End FunctionCreating a scratch workspace The ScratchWorkspaceFactory and FileGDBScratchWorkspaceFactory classes are used to connect to a temporary database stored in Microsoft Access or in a file geodatabase, known as a scratch workspace. These workspaces are commonly used to hold the results of a selection set or to hold the results of an analysis operation. These factories are fundamentally different from the AccessWorkspaceFactory or FileGDBWorkspaceFactory classes, and the manner in they are used is different: First, the factories implement scratch workspace-specific interfaces (IScratchWorkspaceFactory and IScratchWorkspaceFactory2) for creating new temporary workspaces or connect to existing temporary workspaces. Second, when the last reference to a scratch workspace is released, the workspace is automatically deleted from the disk. Whereas personal and file geodatabase workspaces are created much less often than they're connected to, scratch workspaces are created and connected to in the same session. The FileGDBScratchWorkspaceFactory and ScratchWorkspaceFactory classes are used in a similar fashion, but scratch workspaces based on file geodatabases can take advantage of their cross-platform. nature and can handle larger amounts of data. The following code example shows how to use the current scratch workspace to store a selection set (this example uses an Access-based scratch workspace): [C#] public IWorkspace OpenScratchWorkspace(ITable parcelTable, IQueryFilter queryFilter) { // Create a scratch workspace factory. IScratchWorkspaceFactory workspaceFactory = new ScratchWorkspaceFactoryClass() ; IScratchWorkspaceFactory2 workspaceFactory2 = workspaceFactory as IScratchWorkspaceFactory2; // Get the current scratch workspace. IWorkspace scratchWorkspace = workspaceFactory2.CurrentScratchWorkspace; // Select from an existing table, called parcelTable, using an existing query, called queryFilter, // putting the resulting selection set in the temporary workspace. ISelectionSet selSet = parcelTable.Select(queryFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, scratchWorkspace); return scratchWorkspace; } [VB.NET] Public Function OpenScratchWorkspace(ByVal parcelTable As ITable, ByVal queryFilter As IQueryFilter) As IWorkspace ' Create a scratch workspace factory. Dim workspaceFactory As IScratchWorkspaceFactory = New ScratchWorkspaceFactoryClass() Dim workspaceFactory2 As IScratchWorkspaceFactory2 = TryCast(workspaceFactory, IScratchWorkspaceFactory2) ' Get the current scratch workspace. Dim scratchWorkspace As IWorkspace = workspaceFactory2.CurrentScratchWorkspace ' Select from an existing table, called parcelTable, using an existing query, called queryFilter, ' putting the resulting selection set in the temporary workspace. Dim selSet As ISelectionSet = parcelTable.Select(queryFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, scratchWorkspace) Return scratchWorkspace End FunctionThe following code example shows how to get the current scratch workspace to use as part of a selection, using a file geodatabase: [C#] public IWorkspace OpenFileGdbScratchWorkspace(ITable parcelTable, IQueryFilter queryFilter) { // Create a file scratch workspace factory. IScratchWorkspaceFactory workspaceFactory = new FileGDBScratchWorkspaceFactoryClass(); IScratchWorkspaceFactory2 workspaceFactory2 = workspaceFactory as IScratchWorkspaceFactory2; // Get the current scratch workspace. IWorkspace scratchWorkspace = workspaceFactory2.CurrentScratchWorkspace; // Select from an existing table, called parcelTable, using an existing query, called queryFilter, // putting the resulting selection set in the temporary workspace. ISelectionSet selSet = parcelTable.Select(queryFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, scratchWorkspace); return scratchWorkspace; } [VB.NET] Public Function OpenFileGdbScratchWorkspace(ByVal parcelTable As ITable, ByVal queryFilter As IQueryFilter) As IWorkspace ' Create a file scratch workspace factory. Dim workspaceFactory As IScratchWorkspaceFactory = New FileGDBScratchWorkspaceFactoryClass() Dim workspaceFactory2 As IScratchWorkspaceFactory2 = TryCast(workspaceFactory, IScratchWorkspaceFactory2) ' Get the current scratch workspace. Dim scratchWorkspace As IWorkspace = workspaceFactory2.CurrentScratchWorkspace ' Select from an existing table, called parcelTable, using an existing query, called queryFilter, ' putting the resulting selection set in the temporary workspace. Dim selSet As ISelectionSet = parcelTable.Select(queryFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, scratchWorkspace) Return scratchWorkspace End FunctionCreating an InMemory 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 InMemory workspaces support the full geodatabase model. They can be used in conjunction with cursors, selections, and so on. However, InMemory workspaces do not support all aspects of the geodatabase model; advanced datasets such as topology and geometric networks are not supported. The following code example shows how to create and open an InMemory workspace: [C#] public IWorkspace CreateInMemoryWorkspace() { // Create an InMemory workspace factory. IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); // Create an InMemory geodatabase. IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); // Cast for IName. IName name = (IName)workspaceName; //Open a reference to the InMemory workspace through the name object. IWorkspace workspace = (IWorkspace)name.Open(); return workspace; } [VB.NET] Public Function CreateInMemoryWorkspace() As IWorkspace ' Create an InMemory workspace factory. Dim workspaceFactory As IWorkspaceFactory = New InMemoryWorkspaceFactoryClass() ' Create an InMemory geodatabase. Dim workspaceName As IWorkspaceName = workspaceFactory.Create("", "MyWorkspace", Nothing, 0) ' Cast for IName. Dim Name As IName = CType(workspaceName, IName) ' Open a reference to the InMemory workspace through the name object. Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace) Return workspace End Function

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值