我们知道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);
}