ArcGIS Engine+分散式資料庫操作

weixin_34402090發表於2012-08-03

我們知道Geodatabase的分散式資料庫可以線上也可以離線,而線上和離線的介面是不同的,這是因為兩者的操作過程不一樣,我們看一下兩者的區別:
                  

                                
          
                                        線上

 



                                                                                                                      離線




通過這4張圖很清楚的說明了原理(其中每一種方式又分為區域網和網際網路)

重要的介面:

GeoDataServer  代表了一個資料庫的連線,這句話可能不好理解,但是通過程式碼,也許會明白:

public IGeoDataServer InitGeoDataServerFromFile(String path)

{

    // Create the GeoDataServer and cast to the the IGeoDataServerInit interface.

    IGeoDataServer geoDataServer = new GeoDataServerClass();

    IGeoDataServerInit geoDataServerInit = (IGeoDataServerInit)geoDataServer;

    geoDataServerInit.InitFromFile(@"C:\arcgis\ArcTutor\DatabaseServers\hazards.gdb");

    return geoDataServer;

}

在離線方式下,我們的資料同步是通過匯入,匯出,而匯入,匯出這些方法由此介面提供,看下面的例子:

public void ExportReplicaDataChanges(IGeoDataServer sourceGDS, String replicaName,

    String outputDirectory)

{

    try

    {

        // Set the export options.

        GDSExportOptions gdsExportOptions = new GDSExportOptions();

        gdsExportOptions.ExportFormat = esriGDSExportFormat.esriGDSExportFormatXml;

        gdsExportOptions.Compressed = true;

        gdsExportOptions.BinaryGeometry = false;



        // Export the data changes. Note: The replica must be in a Sending Data State.

        IGDSData gdsData = sourceGDS.ExportReplicaDataChanges(replicaName,

            gdsExportOptions, esriGDSTransportType.esriGDSTransportTypeUrl,

            esriExportGenerationsOption.esriExportGenerationsAll, false);



        // Force deletion of folder and contents if they exist.

        if (Directory.Exists(outputDirectory))

        {

            Directory.Delete(outputDirectory, true);

        }



        // Create the output folder.

        Directory.CreateDirectory(outputDirectory);



        // Get the compressed data changes document from the URL to the local output directory.

        if (gdsData.TransportType == esriGDSTransportType.esriGDSTransportTypeUrl)

        {

            string fileName = System.IO.Path.GetFileName(gdsData.URL);

            string outputFileName = System.IO.Path.Combine(outputDirectory, fileName)

                ;

            WebClient wc = new WebClient();

            wc.DownloadFile(gdsData.URL, outputFileName);

            wc.Dispose();

        }

        else

        {

            // The file has been embedded because there is no output directory set on ArcGIS Server.

            Console.WriteLine("Server is not configured with a virtual directory.");

        }

    }

    catch (COMException comExc)

    {

        throw new Exception(String.Format(

            "Exporting data changes errored: {0}, Error Code: {1}", comExc.Message,

            comExc.ErrorCode), comExc);

    }

    catch (Exception exc)

    {

        throw new Exception(String.Format("Exporting data changes errored: {0}",

            exc.Message), exc);

    }

}

而在線上方式下,用ReplicatonAgent介面

ReplicatonAgent 介面就是用來實現線上方式下的同步

public void SynchronizeReplica(IGeoDataServer parentGDS, IGeoDataServer childGDS,

    String replicaName, esriReplicationAgentReconcilePolicy conflictPolicy,

    esriReplicaSynchronizeDirection syncDirection, Boolean columnLevel)

{

    try

    {

        // Iterate through the replicas of the parent geodata server.

        IGPReplicas gpReplicas = parentGDS.Replicas;

        IGPReplica parentReplica = null;

        for (int i = 0; i < gpReplicas.Count; i++)

        {

            // See if the unqualified replica name matches the replicaName parameter.

            IGPReplica currentReplica = gpReplicas.get_Element(i);

            String currentReplicaName = currentReplica.Name;

            int dotIndex = currentReplicaName.LastIndexOf(".") + 1;

            String baseName = currentReplicaName.Substring(dotIndex,

                currentReplicaName.Length - dotIndex);

            if (baseName.ToLower() == replicaName.ToLower())

            {

                parentReplica = currentReplica;

                break;

            }

        }



        // Check to see if the parent replica was found.

        if (parentReplica == null)

        {

            throw new ArgumentException(

                "The requested replica could not be found on the parent GDS.");

        }



        // Iterate through the replica of the child geodata server.

        gpReplicas = childGDS.Replicas;

        IGPReplica childReplica = null;

        for (int i = 0; i < gpReplicas.Count; i++)

        {

            // See if the unqualified replica name matches the replicaName parameter.

            IGPReplica currentReplica = gpReplicas.get_Element(i);

            String currentReplicaName = currentReplica.Name;

            int dotIndex = currentReplicaName.LastIndexOf(".") + 1;

            String baseName = currentReplicaName.Substring(dotIndex,

                currentReplicaName.Length - dotIndex);

            if (baseName.ToLower() == replicaName.ToLower())

            {

                childReplica = currentReplica;

                break;

            }

        }



        // Check to see if the child replica was found.

        if (childReplica == null)

        {

            throw new ArgumentException(

                "The requested replica could not be found on the child GDS.");

        }



        // Synchronize the replica.

        IReplicationAgent replicationAgent = new ReplicationAgentClass();

        replicationAgent.SynchronizeReplica(parentGDS, childGDS, parentReplica,

            childReplica, conflictPolicy, syncDirection, columnLevel);

    }

    catch (COMException comExc)

    {

        throw new Exception(String.Format(

            "Create replica errored: {0}, Error Code: {1}", comExc.Message,

            comExc.ErrorCode), comExc);

    }

    catch (Exception exc)

    {

        throw new Exception(String.Format("Create replica errored: {0}", exc.Message)

            , exc);

    }

相關文章