Remote Data Synchronization
Basic Structure:
One central database: db1 (including table T1)
A terminal Database: DB2 (including table T2)
T1 has the same structure as T2.
Now, I want to use a WebService to implement the following functions:
The terminal transmits the T2 dataset in DB2 to WebService, and WebService updates the changed part of the dataset to T1 in db1. Similarly, WebService downloads the dataset obtained from T1 in db1 to the terminal, and updates the changes in dataset to t2 in DB2. In this way, data in T1 of db1 is synchronized with that in T2 of DB2.
I want to use the dataadapter uodate method, but I haven't figured it out for a long time. Please help me to see how to do it, or provide us with a better solution.
A simple model is created:
Try
{
String str_sconn = "Data Source = (local); initial catalog = cosco_express; Password = 12345; user id = sa ";
String str_oconn = @ "provider = Microsoft. Jet. oledb.4.0; Data Source = F: \ db5.mdb ";
Sqlconnection sconn = new sqlconnection (str_sconn );
Oledbconnection oconn = new oledbconnection (str_oconn );
Sqldataadapter SDA = new sqldataadapter ("select * From myuser", sconn );
Dataset SDS = new dataset ();
SDA. Fill (SDS, "myuser ");
Oledbdataadapter ODA = new oledbdataadapter ("select * From myuser", oconn );
Oledbcommandbuilder custcb = new oledbcommandbuilder (ODA );
Dataset ODS = new dataset ();
ODA. Fill (ODS, "myuser ");
This. datagrid1.datasource = ODS. Tables ["myuser"]. defaultview;
MessageBox. Show ("first! ");
// ODS. Merge (SDS. Tables ["myuser"]);
// ODS. Merge (SDS, false );
// ODS. acceptchanges ();
This. datagrid1.datasource = ODS. Tables ["myuser"]. defaultview;
MessageBox. Show ("merged! ");
ODA. Update (ODS, "myuser"); // This statement does not work :(
MessageBox. Show ("updated! ");
}
Catch (system. Exception ERR)
{
MessageBox. Show (ERR. Message );
}
There are two records in the central table, and there is no data in the orders table. The data is displayed in the DataGrid and the content of the Start grid is empty. After merge, two records are displayed in the grid, indicating that the merge is successful, but the last update does not work, and there is still no data in the tables Table :(
If the data in the two databases is different at the beginning, you may have to write one class yourself to do the work similar to merge: traverse all the records of SDS and compare it with ODS, if records with the same primary key exist, compare their fields. If they are different, modify the records of ODS. If records with the same primary key do not exist in ODS, add a new record; finally, when we traverse ODS and compare it with SDS, if there is no record with the same primary key, the corresponding record in ODS will be deleted.
However, this solution is not a good solution in all aspects :(
Agree. I also thought about this method. I used to use the dataadapter update method. I cannot use it now :(
My idea is to use several fields as the marker (insert, update, delete) when designing the database, then sort them out, and then process the dataset for each type separately. However, if this is the case, it must be the traversal of each datarow of the passed datatable. What should we do next?
I think the most stupid way is to use SQL statements to operate each datarow (too many Io operations, low efficiency). Is there a better way? If dataset can be used, it should be done in the memory, batch submission is the best, but in addition to insert operations, delete and update will certainly have problems :(
I wrote a class, but there are a lot of restrictions: for example, the table must have an architecture.
Public class mymerge
{
Public static void Merge (Dataset ds1, string dt1, dataset ds2, string dt2)
{
Object [] newrow = new object [ds1.tables [dt1]. primarykey. Length];
Datacolumn [] Dc = ds1.tables [dt1]. primarykey;
For (INT I = 0; I <ds1.tables [dt1]. Rows. Count; I ++)
{
For (int K = 0; k <newrow. length; k ++)
{
Newrow [k] = ds1.tables [dt1]. Rows [I] [DC [K]. columnname];
}
Ds2.tables [dt2]. beginloaddata ();
Ds2.tables [dt2]. loaddatarow (newrow, false );
Ds2.tables [dt2]. endloaddata ();
}
For (INT I = 0; I <ds2.tables [dt2]. Rows. Count; I ++)
{
For (int K = 0; k <newrow. length; k ++)
{
Newrow [k] = ds2.tables [dt2]. Rows [I] [DC [K]. columnname];
}
If (! Ds1.tables [dt1]. Rows. Contains (newrow ))
{
Ds2.tables [dt2]. Rows [I]. Delete ();
}
}
}
}