IWorkSpace接口介绍 1.打开各种数据库

IWorkspace接口提供访问工作空间的通用属性和方法,如它的连接属性,以及包含的数据集的方法。

IWorkspace的成员字段:

Members

 

Description

ConnectionProperties

The connection properties of the workspace.

DatasetNames

The DatasetNames in the workspace.

Datasets

The datasets in the workspace.

ExecuteSQL

Executes the specified SQL statement.

Exists

Checks if the workspace exists.

IsDirectory

TRUE if the workspace is a file system directory.

PathName

The file system full path of the workspace.

Type

The Type of the Workspace.

WorkspaceFactory

The factory that created the workspace.

 

 

如何打开一个数据库 

 要打开一个数据库,也就意味着我们要得到那个工作空间,而工作空间是一个普通类,也就意味着我们只

能从其他类来得到这个工作空间,这个类就是工作空间工厂(WorkspaceFactory),而这个类又是一个抽

象类,也就意味着我们只能使用它的子类来实例化一个对象,WorkspaceFactory有众多的子类

IWorkSpaceFactoryGeodatabase的入口,它定义了数据库的通用属性,比如打开,创建等,我们在ArcGIS Engine的帮助中可以详细的得到它的信息,如下图: 

Members

   

Description

 

ContainsWorkspace

Indicates if parentDirectory contains a valid workspace, or is a valid file-system workspace.

 

Copy

Copies a workspace to the specified destination folder.

 

Create

Creates a new workspace specified by the directory, file name, and connection properties.

 

GetClassID

The class ID of the WorkspaceFactory.

 

GetWorkspaceName

Retrieves the workspace name of a workspace from the given list of file names.

 

IsWorkspace

True if the specified file identifies a workspace supported by the workspace factory.

 

Move

Moves a workspace to the specified destination folder.

 

Open

Opens the workspace specified by the connection properties.

 

OpenFromFile

Opens the workspace specified by the given file name.

 

ReadConnectionPropertiesFromFile

The connection properties from the specified file.

 

WorkspaceDescription

A singular or plural description of the type of workspace the workspace factory opens/creates.

 

WorkspaceType

The type of workspace the workspace factory opens/creates.

 

 

 

 

打开数据库有两种方式,从上面的图表中也可以看出OpenFromFile方法和Open方法,这两者的区

方法中的参数不同,其中Open方法需要一个IPropertySet对象,这个方法我们经常在打开SDE

使用,注意(这个方法同样可以用来打开个人数据库,文件数据库)。 

打开个人数据库 

    public IWorkspace GetMDBWorkspace(String _pGDBName) 

       { 

           IWorkspaceFactory pWsFac = new AccessWorkspaceFactoryClass(); 

 

           IWorkspace pWs = pWsFac.OpenFromFile(_pGDBName,0); 

       return pWs; 

       }

}

 打开文件数据库 

  public IWorkspace GetFGDBWorkspace(String _pGDBName) 

        { 

            IWorkspaceFactory pWsFac = new FileGDBWorkspaceFactoryClass(); 

 

            IWorkspace pWs = pWsFac.OpenFromFile(_pGDBName, 0); 

 

            return pWs; 

        } 

打开SDE数据库 

打开SDE数据库我们使用的是Open方法,要用这个方法,我们就要对IPropertySet对象设置,要打开SDE

数据库,我们要获取SDE数据库的服务器地址,数据库实例,数据库,用户,密码等参数。而IPropertySet

就好比一个Key-Value的对象,用来帮组我们设置这些,然后传到Open方法中。 

   public IWorkspace GetSDEWorkspace(String _pServerIP, String _pInstance, String _pUser, String 

_pPassword, String _pDatabase, String _pVersion) 

        { 

                 

            ESRI.ArcGIS.esriSystem.IPropertySet pPropertySet = new 

ESRI.ArcGIS.esriSystem.PropertySetClass(); 

            pPropertySet.SetProperty("SERVER", _pServerIP); 

            pPropertySet.SetProperty("INSTANCE", _pInstance); 

            pPropertySet.SetProperty("DATABASE", _pDatabase); 

            pPropertySet.SetProperty("USER", _pUser); 

            pPropertySet.SetProperty("PASSWORD", _pPassword); 

            pPropertySet.SetProperty("VERSION", _pVersion); 

            ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 workspaceFactory; 

            workspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)new 

ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass(); 

            return workspaceFactory.Open(pPropertySet, 0); 

        } 

原文地址:https://www.cnblogs.com/qiushuixizhao/p/3242345.html