C # basic steps and functions used in data synchronization

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.