连接workspace的几种方法

1.Connecting to a personal geodatabase workspace stored in Access

   The workspace factory required to connect to a personal geodatabase is an AccessWorkspaceFactory.

    For local database workspaces, the IWorkspaceFactory.Open method usually requires a property set with a single property named DATABASE, the value of which should be the path to the workspace (including the filename).

例:

    The following code example accepts a string that is used to populate the required property to open and return a personal geodatabase workspace with the Open method:
// For example, database = "C:\\myData\\mypGDB.mdb".
    public IWorkspace AccessWorkspaceFromPropertySet(string database)
{ IPropertySet propertySet
= new PropertySetClass(); propertySet.SetProperty("DATABASE", database); IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass(); return workspaceFactory.Open(propertySet, 0);
}

   The OpenFromFile method for personal geodatabases requires the path name of the .mdb file that stores the Access database.

// For example, path = "C:\\myData\\mypGDB.mdb".
public IWorkspace AccessWorkspaceFromPath(string path)
{
    IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
    return workspaceFactory.OpenFromFile(path, 0);
}

2.Connecting to a file geodatabase workspace

    The required code to connect to a file geodatabase is almost identical to the code for connecting to a personal geodatabase. Both are local database workspaces, but as stated previously, a different type of workspace factory is required. Additionally, the extension used on the database will be different. For a file geodatabase, the extension is .gdb.
    The following code example uses the Open method to return a file geodatabase workspace:
// For example, database = "C:\\myData\\myfGDB.gdb".
public IWorkspace FileGdbWorkspaceFromPropertySet(string database)
{
    IPropertySet propertySet = new PropertySetClass();
    propertySet.SetProperty("DATABASE", database);
    IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
    return workspaceFactory.Open(propertySet, 0);
}
   The OpenFromFile method can also be used to open a file geodatabase workspace. See the following code example:
// For example, path = "C:\\myData\\myfGDB.gdb".
public IWorkspace FileGdbWorkspaceFromPath(String path)
{
    IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
    return workspaceFactory.OpenFromFile(path, 0);
}

3.Connecting to an enterprise ArcSDE geodatabase workspace

     SdeWorkspaceFactory is the required workspace factory to connect to an ArcSDE geodatabase. The connection properties of an esriRemoteDatabaseWorkspace specify the server and instance to connect to and can be saved in a connection file (.sde) on the file system.
     In the case of remote database workspaces accessed via ArcSDE, the properties can include the following connection properties of the database being connected to:
  • SERVER—The SDE server being connected to.
  • INSTANCE—The instance being connected to.
  • DATABASE—The database being connected to. The DATABASE property is optional and is required for ArcSDE instances that manage multiple databases (for example, SQL Server).
  • USER—The connected user.
  • PASSWORD—The connected user's password.
  • VERSION—The transactional version to connect to. The acceptable value is a string that represents a transaction version name.

     In the following code example, a property set is populated from supplied strings to make a connection to an ArcSDE geodatabase workspace with a transactional version.

     The property set is then used by the Open method to return a workspace. This approach involves the most code; however, it can be customized into a general function for connecting to ArcSDE databases based on supplied parameters rom the user.

// For example, server = "Kona".
// Database = "SDE" or "" if Oracle.
// Instance = "5151".
// User = "vtest".
// Password = "go".
// Version = "SDE.DEFAULT".
public IWorkspace VersionedArcSdeWorkspace(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);
    IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass();
    return workspaceFactory.Open(propertySet, 0);
}

Resource:ms-help://ESRI.EDNv9.3/NET_Engine/c778d2bb-eb36-4793-9c89-20795811c5eb.htm

 

 

 

 

 

 

 

posted @ 2013-04-11 15:03  刘朝样  阅读(893)  评论(0)    收藏  举报