Data Synchronization comparison steps:
1. generate xml files for the corresponding data tables in the two databases respectively.
/// <Summary>
/// Save a datatable into a specified object in XML format
/// </Summary>
/// <Param name = "DT"> </param>
/// <Param name = "filepath"> </param>
Public void savedatatabletoxml (datatable DT, string filepath)
{
// Create a folder
If (! Directory. exists (path. getdirectoryname (filepath )))
{
Directory. createdirectory (path. getdirectoryname (filepath ));
}
Dataset DS = new dataset ();
DS. Tables. Add (Dt. Copy ());
DS. writexml (filepath );
}
/// <Summary>
/// Read the datatable from a specified object
/// </Summary>
/// <Param name = "filepath"> </param>
Public datatable readdatatablefromxml (string filepath)
{
Dataset DS = new dataset ();
DS. readxml (filepath );
If (Ds. Tables. Count> 0)
{
Return Ds. Tables [0];
}
Else
{
Return NULL;
}
}
2. Upload the XML data file to the server or download the XML file from the server to the local device.
C # ect asynchronous transmission or WebClient Transmission
3. Compare the data to be synchronized
/// <Summary>
/// Comparison file
/// </Summary>
/// <Param name = "localfile"> local file </param>
/// <Param name = "remotefile"> Remote File </param>
/// <Returns> </returns>
Private bool filecompare (string localfile, string remotefile)
{
Int localfilebyte;
Int remotefilebyte;
Filestream localfilestream;
Filestream remotefilestream;
If (localfile = remotefile)
{
Return true;
}
Localfilestream = new filestream (localfile, filemode. Open );
Remotefilestream = new filestream (remotefile, filemode. Open );
If (localfilestream. length! = Remotefilestream. length)
{
Localfilestream. Close ();
Remotefilestream. Close ();
Return false;
}
Do
{
Localfilebyte = localfilestream. readbyte ();
Remotefilebyte = remotefilestream. readbyte ();
}
While (localfilebyte = remotefilebyte) & (localfilebyte! =-1 ));
Localfilestream. Close ();
Remotefilestream. Close ();
Return (localfilebyte-remotefilebyte) = 0 );
}
/// <Summary>
/// Compare the data table
/// </Summary>
/// <Param name = "localdatatable"> local data table </param>
/// <Param name = "remotedatatable"> remote data table </param>
/// <Returns> </returns>
Public bool datatablecompare (datatable localdatatable, datatable remotedatatable)
{
If (localdatatable = NULL | remotedatatable = NULL)
{
Return false;
}
If (localdatatable. Rows. Count! = Remotedatatable. Rows. Count)
{
Return false;
}
If (localdatatable. Columns. Count! = Remotedatatable. Columns. Count)
{
Return false;
}
For (INT I = 0; I <localdatatable. Rows. Count; I ++)
{
For (Int J = 0; j <localdatatable. Columns. Count; j ++)
{
If (localdatatable. Rows [I] [J]. tostring ()! = Remotedatatable. Rows [I] [J]. tostring ())
{
Return false;
}
}
}
Return true;
}