ArcEngine open the local database

Look at GeoDatabase core structural model:

   

1 workspace objects plant WorkspaceFactory

   WorkspaceFactory entrance of GeoDatabase, is an abstract class, have many sub-categories, e.g. SdeWorkspaceFactory, AccessWorkspaceFactory, ShapfileWorkspaceFactory CadWorkspaceFactory etc.

   IWorkspaceFactory interface defines the general properties and methods for all workspace objects

   public IWorkspaceName the Create ( String  ParentDirectory, String  the Name, IPropertySet  the ConnectionProperties, int  the hWnd ); for generating a new workspace name of the object, and the first two arguments are the path name of the database, the third parameter is a set of attributes (properset) object.
   public the IWorkspace the Open (  IPropertySet  the ConnectionProperties, int  the hWnd ); open an existing workspace SDE image database;

      public  the IWorkspace  OpenFromFile ( String  fileName, int  the hWnd ); open a file type of data

2 workspace WorkSpace objects

   Workspace is logically a data set containing data container space and non-space data set, the data including feature classes, raster data sets, like the object table

   Workspace The main target of some of the interfaces:

        1 IWorkspace Interface: Defines a workspace of the most common properties and methods.

        2 IFeatureWorkspace Interface

        This feature is primarily used to manage the interface data sets, such as table (the Table), object class (the ObjectClass), feature class (FeatureClass), feature data set (of type FeatureDataset) and the relationship between class (RelationshipClass) and the like.

   The main members:

  

   public IFeatureClass OpenFeatureClass (  String  the Name ) to open an existing feature class, whether this feature class is concentrated in the working space in a data element
    public IFeatureDataset OpenFeatureDataset ( String  the Name ); open feature to an existing dataset
    public an ITable OpenTable ( String  the Name ); open an existing table;

3 RasterWorkspace objects

   The main achievement of IRasterWorkspace Interface

    public IRasterDataset OpenRasterDataset ( string Name);

4 Object attribute set Propertyset

  Propertyset object is an object for setting a special property, which is for a collection of name-value corresponding to similar Hash table. Must be a string attribute name, attribute value can be a string, numeric, or date may be an object

  public void SetProperty (string Name, object Value);

5 DataSet data set object

   Dataset object can be divided into two categories: Table and GeoDataset, GeoDataset is an abstract class, it has represented the spatial properties of the data set, including FeatureDataset, feature classes FeatureClass, TIN and raster datasets RasterDataset.

6 FeatureDataset objects

 The main interface to achieve:

1 IFeatureDataset Interface

      public IEnumDataset Subsets {get;}Datasets contained within this dataset.

2 IFeatureClassContainer Interface

   For managing feature dataset inside a feature class, ClassByName the interface and Class (index) and other attributes can be used to obtain specific data set of feature class (very strange here only attribute in no way help but point into the document is one way Ok you probably do not understand what I say)

       public IFeatureClass get_Class (int ClassIndex);

       public IFeatureClass get_ClassByID (int ID);

      public IFeatureClass get_ClassByName (string Name);

   Next, a few examples of various types of data loaded

   1 Load Shapefiles file

            OpenFileDialog dlg=new OpenFileDialog();
            dlg.Title="打开shapefile文件";
            dlg.Filter="(*.shp)|*.shp";
            dlg.ShowDialog();
            string filename=dlg.FileName;
            int index=filename.LastIndexOf(@"\");
            string path=filename.Substring(0,index);
            string name=filename.Substring(index+1);

            IWorkspaceFactory pWsFactory = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace  pWorkSpace = pWsFactory.OpenFromFile(path, 0) as IFeatureWorkspace;
            IFeatureClass pFeatureClass = pWorkSpace.OpenFeatureClass(name);
            IFeatureLayer pFeatureLayer = new FeatureLayerClass();
            pFeatureLayer.Name = pFeatureClass.AliasName;
            pFeatureLayer.FeatureClass = pFeatureClass;

            axMapControl1.Map.AddLayer(pFeatureLayer);
            axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

   2. Load raster data

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "打开Raster文件";
            dlg.Filter = " Layer File(*.lyr)|*.jpg;*.bmp;*.tiff ";
            dlg.ShowDialog();
            string filename = dlg.FileName;
            int index = filename.LastIndexOf(@"\");
            string path = filename.Substring(0, index);
            string name = filename.Substring(index + 1);

            IWorkspaceFactory pWsFactory =new RasterWorkspaceFactoryClass();
            IRasterWorkspace pRasterWorkspace = pWsFactory.OpenFromFile(path, 0) as IRasterWorkspace;
            IRasterDataset pRasterDataset = pRasterWorkspace.OpenRasterDataset(name);
            IRasterLayer pRasterLayer = new RasterLayerClass();
            pRasterLayer.CreateFromDataset(pRasterDataset);
            axMapControl1.Map.AddLayer(pRasterLayer);
            axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

    3  Load the CAD data

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "打开CAD文件";
            dlg.Filter = " CAD(*.dwg)|*.dwg|All Files(*.*)|*.* ";
            dlg.ShowDialog();
            string filename = dlg.FileName;
            int index = filename.LastIndexOf(@"\");
            string path = filename.Substring(0, index);
            string name = filename.Substring(index + 1);

            IWorkspaceFactory pWsFactory = new CadWorkspaceFactoryClass();
            IFeatureWorkspace pFeatureWorkspace = pWsFactory.OpenFromFile(path, 0) as IFeatureWorkspace;
            IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(name);
            IFeatureClassContainer pFCContainer = pFeatureDataset as IFeatureClassContainer;
            for (int i = 0; i < pFCContainer.ClassCount; i++)
            {
                IFeatureClass pFeatureClass = pFCContainer.get_Class(i);
                if (pFeatureClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation)
                {
                    IFeatureLayer pFeatureLayer = new CadAnnotationLayerClass();

                }
                else
                {
                    IFeatureLayer pFeatureLayer = new FeatureLayerClass();
                    pFeatureLayer.FeatureClass = pFeatureClass;
                    pFeatureLayer.Name = pFeatureClass.AliasName;
                    axMapControl1.Map.AddLayer(pFeatureLayer);

                }
                axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

      4  Load PersonGeodatabase data

        

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "打开Geodatabase文件";
            dlg.Filter = " Personal Geodatabase(*.mdb)|*.mdb|All Files(*.*)|*.* ";
            dlg.ShowDialog();
            string path = dlg.FileName;
          

            IWorkspaceFactory pAccessWorkspaceFactory;
            IFeatureWorkspace pFeatureWorkspace;
            IFeatureLayer pFeatureLayer;
            IFeatureDataset pFeatureDataset;
            pAccessWorkspaceFactory = new AccessWorkspaceFactoryClass();
          
            IWorkspace pWorkspace = pAccessWorkspaceFactory.OpenFromFile(path, 0);
            IEnumDataset pEnumDataset = pWorkspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
            pEnumDataset.Reset();
            IDataset pDataset = pEnumDataset.Next();
           
            if (pDataset is IFeatureDataset)
            {
                pFeatureWorkspace = (IFeatureWorkspace)pAccessWorkspaceFactory.OpenFromFile(path, 0);
                pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(pDataset.Name);
                IEnumDataset pEnumDataset1 = pFeatureDataset.Subsets;
                pEnumDataset1.Reset();
                IDataset pDataset1 = pEnumDataset1.Next();
               
                if (pDataset1 is IFeatureClass)
                {
                    pFeatureLayer = new FeatureLayerClass();
                    pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pDataset1.Name);
                    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
                    axMapControl1.Map.AddLayer(pFeatureLayer);
                    axMapControl1.ActiveView.Refresh();
                }
              
             
            }
            else
            {
                pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
                pFeatureLayer = new FeatureLayerClass();
                pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pDataset.Name);
                pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
                axMapControl1.Map.AddLayer(pFeatureLayer);
                axMapControl1.ActiveView.Refresh();
            }

 

1, loaded personal database
personal database is stored in Access database. Loaded in two ways: by name by loading property (perhaps only two, AE to achieve the same function can have a variety of ways) and.
A, by setting the property to load personal database.
First, the path defined by the interface to be connected IPropertySet some of the relevant attributes of the database, the database in the personal database, for example:
IPropertySet new new PropertySetClass propset = (); 
           Propset.SetProperty ( "DATABASE", @ "D: \ Test \ Ao of \ Data \ sh \ MapData.mdb ");
      if after finished defining the properties and set properties can opening operation of the database, there are several commonly used to open and manipulate data space IWorkspaceFactory, IFeatureWorkspace, IFeatureClass, IFeatureLayer like ArcEngine development the interface surface features. IWorkspaceFactory is open and an interface for creating a working space, which is an abstract interface, we use the corresponding workspace instantiate it in a specific application, as follows:
IWorkspaceFactory new new AccessWorkspaceFactoryClass the Fact = ();
if we open the SDE database will use SdeWorkspaceFactoryClass instantiated Fact. When we are done after instantiating workspace to open the corresponding Access Database according to attribute the upper set. Open follows:
         IFeatureWorkspace Workspace = Fact.Open (propset, 0) AS IFeatureWorkspace;
Access open work space is the next thing to do, and very simple, find the corresponding feature class, assigned to the corresponding layer, layer by adding the corresponding MapControl control, and then refresh the map. The following code is added a layer:
       the IFeatureClass FCLS = Workspace.OpenFeatureClass ( "District");
       IFeatureLayer new new FeatureLayerClass Fly = ();
       Fly.FeatureClass = FCLS;
       MapCtr.Map.AddLayer (Fly);
       MapCtr.ActiveView.Refresh ( );
wherein District feature is the name of the class, MapCtr object in the MapControl AE. Loading property by way of the upper space of the data may also be used SDE database, the database is loaded when the SDE will be introduced.
The following C # code to a full load by setting the Access database property:
public void AddAccessDBByPro ()
{
       IPropertySet new new PropertySetClass propset = (); 
       Propset.SetProperty ( "DATABASE", @ "D: \ Test \ Ao of \ Data \ SH \ MapData. MDB ");
       IWorkspaceFactory new new AccessWorkspaceFactoryClass the Fact = ();
       Workspace = Fact.Open IFeatureWorkspace (propset, 0) AS IFeatureWorkspace;
         
       the IFeatureClass FCLS = Workspace.OpenFeatureClass ( "District");
       IFeatureLayer new new FeatureLayerClass Fly = ();
       Fly.FeatureClass = FCLS;
 
       MapCtr.Map.AddLayer (Fly);
       MapCtr .ActiveView.Refresh ();
}
B, loaded personal database by database name
in which I first write complete code, allowing you to talk to the top of the code to be compared. The following is the complete code:
public void AddAccessDBByName ()
{
      IWorkspaceName pWorkspaceName new new WorkspaceNameClass = ();
    pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.AccessWorkspaceFactory";
      pWorkspaceName.PathName @ = "D: \ Test \ Ao of \ Data \ SH \ MapData.mdb ";
      iname pWorkspaceName AS = n-iname;
      Workspace = n.Open IFeatureWorkspace () AS IFeatureWorkspace;
                                                             
     the IFeatureClass FCLS = Workspace.OpenFeatureClass ( "District");
     IFeatureLayer new new FeatureLayerClass Fly = ();
     Fly.FeatureClass = FCLS;
     MapCtr.Map.AddLayer (Fly);
     MapCtr.ActiveView. the refresh ();
}
careful have noticed, after opening the workspace Access following code is the same, is to find the corresponding feature class assigned to the corresponding layer, by adding the corresponding layer MapControl control and refresh map. Now explain the code above, first create a personal database workspace name, a specific workspace name ProgID, to determine whether to open what type of work space, for example, when you open the Access personal database is the code below:
  = new new WorkspaceNameClass pWorkspaceName IWorkspaceName ();
pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.AccessWorkspaceFactory";
pWorkspaceName.PathName @ = "D: \ Test \ Ao of \ Data \ SH \ MapData.mdb";
Properties WorkspaceFactoryProgID can ensure that the work space is AccessWorkspaceFactory, namely personal database, and specify the path to the database you want to open. To open the database, we can see through the class diagram of AE open work space must be used IName Interface (personal opinion, not necessarily correct, you can think about other ways to see if there is not), it is then defined IName object and converts into a workspace name iname iname type and assigned to the object, and () method to open the corresponding work space through the open iName object, as follows:
iname pWorkspaceName AS = n-iname;
IFeatureWorkspace workspace = n.Open () AS IFeatureWorkspace;
next thing is to the upper She mentioned.
2, load SDE database
What is the SDE database? To explain the problem in detail will spend a lot of time, but I can tell you SDE spatial database can be any relational database. ESRI company in order to make room for data to be stored in a relational database, and can be a good relevant spatial properties and the development of a middleware, can be a good use of SDE spatial data stored in relational databases. As Orcale, SQL Server and so on. SDE understand the specific details, please find relevant information, here only how to connect SDE database. SDE online database into a direct connection and the connection via SDE. When the server performance can be better when using SDE connection, or a direct connection, this can reduce the task server. It suggested a direct connection, in fact, SDE connection and direct connection is just one attribute parameters of the problem. With the personal attribute database connection by way of the same, define a property of the object, and then set the attribute parameter, and then defines a workspace by SdeWorkspaceFactoryClass () instantiate it, and then added to the load layers, as loading the code layer, and loading Like personal database layer method, in fact, only two data types loaded, loaded when other types of data is the same method to load layers, just work space using different instances of it, right below for the complete "/ / "as the analysis NOTE:
public void AddSDELayer (BOOL ChkSdeLinkModle)
{
     // definition of a property
     IPropertySet new new PropertySetClass propset = (); 
     IF (== ChkSdeLinkModle to true) using SDE connection //
     { 
            // the name of the database server, the server where the IP address
           Propset.SetProperty ( "SERVER", "192.168.2.41 ");
            // Set the port SDE, which is specified during installation when installed by default "Port: 5151"
           Propset.SetProperty ( "INSTANCE", "Port: 5151");
            // SDE user name
           Propset.SetProperty ( "USER" , "SA");
            // password
           Propset.SetProperty ( "pASSWORD", "SA");
          // set the name of the database, SQL Server Informix databases only need to set
           Propset.SetProperty ( "dATABASE", "sde ");
           // the SDE version, in this version of the default
           Propset.SetProperty ( "vERSION", "SDE.DEFAULT");
     }
     the else directly connected //
     {
           // the name of the database server, the machine can be used if it is "sde: sqlserver :. "
           Propset.SetProperty (" INSTANCE "," SDE:sqlserver: zhpzh ");
            // SDE user name
           Propset.SetProperty (" USER "," sa ");
            // password
           Propset.SetProperty ( "PASSWORD", "SA");
          // set the name of the database, SQL Server Informix databases only need to set           
          Propset.SetProperty ( "DATABASE", "SDE");
          // SDE version, in this version of the default
          Propset.SetProperty ( "vERSION", "SDE.DEFAULT");
    }
    // define a workspace, and the workspace into the SDE example
    IWorkspaceFactory new new SdeWorkspaceFactoryClass the Fact = ();
    // open SDE workspace , and into the workspace feature
    IFeatureWorkspace workspace = (IFeatureWorkspace) Fact.Open (propset, 0);
    / * define a feature class, and in the open tubes SDE point feature class must write the whole time of writing. the tube has a SDE dot layer, you can not write IFeatureClass Fcls = Workspace.OpenFeatureClass ( "point in pipe"); thus, must be written under way * /.
       IFeatureClass Fcls = Workspace.OpenFeatureClass ( "SDE.. Dbo point in pipe ");
           
       Fly = new new FeatureLayerClass IFeatureLayer ();
       Fly.FeatureClass = FCLS;
       MapCtr.Map.AddLayer (Fly);
       MapCtr.ActiveView.Refresh ();
    }
do not know noticed no direct connection with the biggest difference SDE connection is a direct connection Do not set the port, and they are not the same set of parameters, take good care of setting parameters.

3, the layer loading CAD
CAD loading layer can be divided into: a layered loading and loading the whole image
A, a layered loading
we can CAD drawings into point, line, surface, denoted loaded into the MapControl, with Like other data loading, first define a working space and with CadWorkspaceFactoryClass () instantiate it, as can be obtained after a workspace open the workspace, and then opens the specified type layer. Is a complete code below:
 public void AddCADByLayer ()
    {
      // definition of the workspace, and with CadWorkspaceFactoryClass () instantiate it
   IWorkspaceFactory the Fact = new new CadWorkspaceFactoryClass ();
   // open the workspace, and assigned to elements of space, OpenFromFile ()
   // path parameters for the CAD in the folder
Workspace = Fact.OpenFromFile IFeatureWorkspace (@ "the I: \ Test \", 0) AS IFeatureWorkspace;      
       / * open line feature class, if the feature point to be open type, it is necessary to put the code below:
   the IFeatureClass FCLS = Workspace.OpenFeatureClass ( "modle.dwg: point");
Thus modle.dwg, plus behind the type you want to open the feature class, separated by a colon to the name of the CAD drawing, you can think about is how polygons and label open . * /
     The IFeatureClass FCLS = Workspace.OpenFeatureClass ( "modle.dwg: Polyline");            
       IFeatureLayer new new FeatureLayerClass Fly = ();
       Fly.FeatureClass = FCLS;
MapCtr.Map.AddLayer (Fly);
       MapCtr.ActiveView.Refresh ();
}
B, loaded CAD drawing whole
when we want to load the whole CAD drawings, below the required code, which is quite different with class loading feature, described in detail see note code analysis:
public void AddWholeCAD ( )
{
       / * below the two lines of code is to define a CAD workspace, and then open it, but this is not assigned to
    Object IFeatureWorkspace object, but assigned to IWorkspace defined * /
IWorkspaceFactory Fact = new new CadWorkspaceFactoryClass ();
       IWorkspace Workspace = Fact.OpenFromFile (@ "the I: \ the Test \", 0);     
  // define a CAD drawing space, and the upper open work space assigned to it
       ICadDrawingWorkspace DW = workspace AS ICadDrawingWorkspace;
  // definition of a set of CAD data drawing, and the upper opening in a selected workspace FIG CAD
  @ and assigned to the CAD data set
       ICadDrawingDataset ds = dw. OpenCadDrawingDataset ( "modle.DWG");
  // through ICadLayer class assigned to the CAD data obtained upper ICadLayer class object to the Board
  // CadDrawingDataset property
       ICadLayer CadLayer new new CadLayerClass = ();
       CadLayer.CadDrawingDataset = DS;
        // use MapControl loading CAD layer
       MapCtr.Map.AddLayer (CadLayer);
     MapCtr.ActiveView.Refresh ();
}

Copy this https://www.cnblogs.com/henyihanwobushi/archive/2013/03/20/2971271.html

Guess you like

Origin www.cnblogs.com/wgj-blog/p/12082998.html