<html> <head> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Simple Example</title> <link rel="stylesheet" type="text/css" href="../../../style.css"> </head> <body> <div class="CommonContent"> <div class="CommonContentArea"> <h1>Simple Example</h1><script> //We attach this function to a browser object as a variable so that if multiple of these blocks are //present on the same page, then they will simply over-write eachother with the same function instead //of generating a naming collision. window.WikiCodeFormattingCopyToClipboard = function copyToClipboard(sID){ var sContent = document.getElementById(sID).innerText; if( window.clipboardData && clipboardData.setData ) { clipboardData.setData("Text", sContent); } else { alert("You must enable javascript access to your clipboard for this feature to work. Please referr to your browser documentation or Google search for instructions."); } } </script> <p>The following example does a simple replication from a handheld database to the desktop database:</p> <span name="cs_wiki_filter" csw_filters="cs"> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">ReplicationExample.cs: <span class="fscMemberName">Replicate</span></span></div><div class="fscCode"><pre ID="....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">01</span><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/None.gif" align="top"/><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> Replicate() </span><span style="color: #008080;">02</span><span style="color: #000000;"><img id="....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_39_837_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_39_837_Open_Text').style.display='none'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_39_837_Closed_Image').style.display='inline'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_39_837_Closed_Text').style.display='inline';"/><img id="....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_39_837_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_39_837_Closed_Text').style.display='none'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_39_837_Open_Image').style.display='inline'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_39_837_Open_Text').style.display='inline';"/> </span><span id="....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_39_837_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_39_837_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">03</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IObjectContainer desktop </span><span style="color: #000000;">=</span><span style="color: #000000;"> Db4oFactory.OpenFile(DtFileName); </span><span style="color: #008080;">04</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IObjectContainer handheld </span><span style="color: #000000;">=</span><span style="color: #000000;"> Db4oFactory.OpenFile(HhFileName); </span><span style="color: #008080;">05</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IReplicationSession replication </span><span style="color: #000000;">=</span><span style="color: #000000;"> Db4objects.Drs.Replication.Begin(handheld, desktop); </span><span style="color: #008080;">06</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #008080;">07</span><span style="color: #000000;"><img id="....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_312_513_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_312_513_Open_Text').style.display='none'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_312_513_Closed_Image').style.display='inline'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_312_513_Closed_Text').style.display='inline';"/><img id="....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_312_513_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedSubBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_312_513_Closed_Text').style.display='none'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_312_513_Open_Image').style.display='inline'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_312_513_Open_Text').style.display='inline';"/> </span><span id="....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_312_513_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">/**/</span><span id="....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_312_513_Open_Text"><span style="color: #008000;">/*</span><span style="color: #008000;"> </span><span style="color: #008080;">08</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> * There is no need to replicate all the objects each time. </span><span style="color: #008080;">09</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> * ObjectsChangedSinceLastReplication methods gives us </span><span style="color: #008080;">10</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> * a list of modified objects </span><span style="color: #008080;">11</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"/> </span><span style="color: #008000;">*/</span></span><span style="color: #000000;"> </span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IObjectSet changed </span><span style="color: #000000;">=</span><span style="color: #000000;"> replication.ProviderA().ObjectsChangedSinceLastReplication(); </span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #008000;">//</span><span style="color: #008000;">Iterate through changed objects, replicate them</span><span style="color: #008000;"> </span><span style="color: #008080;">14</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/></span><span style="color: #000000;"> </span><span style="color: #0000FF;">while</span><span style="color: #000000;"> (changed.HasNext()) </span><span style="color: #008080;">15</span><span style="color: #000000;"><img id="....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_721_793_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_721_793_Open_Text').style.display='none'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_721_793_Closed_Image').style.display='inline'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_721_793_Closed_Text').style.display='inline';"/><img id="....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_721_793_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedSubBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_721_793_Closed_Text').style.display='none'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_721_793_Open_Image').style.display='inline'; document.getElementById('....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_721_793_Open_Text').style.display='inline';"/> </span><span id="....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_721_793_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="....ReplicationCS.ZipsReplicationExample.Cs_ReplicationExample.Cs_Replicate_164_69_721_793_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">16</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> replication.Replicate(changed .Next ()); </span><span style="color: #008080;">17</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"/> }</span></span><span style="color: #000000;"> </span><span style="color: #008080;">18</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> replication.Commit(); </span><span style="color: #008080;">19</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> }</span></span></div></pre></div></div> </span> <span name="cs_wiki_filter" csw_filters="vb"> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">ReplicationExample.vb: <span class="fscMemberName">Replicate</span></span></div><div class="fscCode"><pre ID="....ReplicationVB.ZipsReplicationExample.Vb_ReplicationExample.Vb_Replicate_297_69"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">01</span><img id="....ReplicationVB.ZipsReplicationExample.Vb_ReplicationExample.Vb_Replicate_297_69_14_767_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('....ReplicationVB.ZipsReplicationExample.Vb_ReplicationExample.Vb_Replicate_297_69_14_767_Open_Text').style.display='none'; document.getElementById('....ReplicationVB.ZipsReplicationExample.Vb_ReplicationExample.Vb_Replicate_297_69_14_767_Closed_Image').style.display='inline'; document.getElementById('....ReplicationVB.ZipsReplicationExample.Vb_ReplicationExample.Vb_Replicate_297_69_14_767_Closed_Text').style.display='inline';"/><img id="....ReplicationVB.ZipsReplicationExample.Vb_ReplicationExample.Vb_Replicate_297_69_14_767_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('....ReplicationVB.ZipsReplicationExample.Vb_ReplicationExample.Vb_Replicate_297_69_14_767_Closed_Text').style.display='none'; document.getElementById('....ReplicationVB.ZipsReplicationExample.Vb_ReplicationExample.Vb_Replicate_297_69_14_767_Open_Image').style.display='inline'; document.getElementById('....ReplicationVB.ZipsReplicationExample.Vb_ReplicationExample.Vb_Replicate_297_69_14_767_Open_Text').style.display='inline';"/><span style="color: #0000FF;">Public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Shared</span><span style="color: #000000;"> </span><span id="....ReplicationVB.ZipsReplicationExample.Vb_ReplicationExample.Vb_Replicate_297_69_14_767_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">Sub Replicate()</span><span id="....ReplicationVB.ZipsReplicationExample.Vb_ReplicationExample.Vb_Replicate_297_69_14_767_Open_Text"><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> Replicate() </span><span style="color: #008080;">02</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> desktop </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IObjectContainer </span><span style="color: #000000;">=</span><span style="color: #000000;"> Db4oFactory.OpenFile(DtFileName) </span><span style="color: #008080;">03</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> handheld </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IObjectContainer </span><span style="color: #000000;">=</span><span style="color: #000000;"> Db4oFactory.OpenFile(HhFileName) </span><span style="color: #008080;">04</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> replic </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IReplicationSession </span><span style="color: #000000;">=</span><span style="color: #000000;"> Replication.Begin(handheld, desktop) </span><span style="color: #008000;">' </span><span style="color: #008080;">05</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/></span><span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;"> There is no need to replicate all the objects each time. </span><span style="color: #008000;"> </span><span style="color: #008080;">06</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/></span><span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;"> ObjectsChangedSinceLastReplication methods gives us </span><span style="color: #008000;"> </span><span style="color: #008080;">07</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/></span><span style="color: #000000;"> </span><span style="color: #008000;">'</span><span style="color: #008000;"> a list of modified objects</span><span style="color: #008000;"> </span><span style="color: #008080;">08</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/></span><span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> changed </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IObjectSet </span><span style="color: #000000;">=</span><span style="color: #000000;"> replic.ProviderA().ObjectsChangedSinceLastReplication() </span><span style="color: #008080;">09</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #008000;">'</span><span style="color: #008000;">Iterate through the changed objects, replicate them</span><span style="color: #008000;"> </span><span style="color: #008080;">10</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/></span><span style="color: #000000;"> </span><span style="color: #0000FF;">While</span><span style="color: #000000;"> changed.HasNext() </span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> replic.Replicate(changed.Next()) </span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">While</span><span style="color: #000000;"> </span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> replic.Commit() </span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> </span><span style="color: #0000FF;">End Sub</span></span></div></pre></div></div> </span> We start by opening two ObjectContainers. The next line creates the ReplicationSession. This object contains all of the replication-related logic. <p>After creating the session, there is an interesting line:</p> <span name="cs_wiki_filter" csw_filters="cs"> <p>C#:</p> <p><code>IObjectSet changed = replication.ProviderA().ObjectsChangedSinceLastReplication(); </code></p> </span> <span name="cs_wiki_filter" csw_filters="vb"> <p>VB:</p> <p><code>Dim changed As IObjectSet = replication.ProviderA().ObjectsChangedSinceLastReplication() </code></p> </span> This line of code will get the provider associated with the first of our sources (the handheld ObjectContainer in this case). Then it finds all of the objects that have been updated or created. The new/modified objects will be returned in an enumerable ObjectSet.<br> <p> After that comes a simple loop where the resulting objects are replicated one at a time.<br> The <code>replication.commit()</code> call at the end is important. This line will save all of the changes we have made, and end any needed transactions. Forgetting to make this call will result in your replication changes being discarded when your application ends, or your ObjectContainers are closed.<br> The <code>#commit()</code> calls also mark all objects as replicated. Therefore, changed/new objects that are not replicated in this session will be marked as replicated. </p> </div> </div> <div id="footer"> This revision (2) was last Modified 2007-07-08T21:19:24 by Tetyana. </div> </body> </html>