Keep the Access database synchronization

xiaoxiao2021-03-06  108

Synchronization is an important concept that the database is applied in the network environment. Its basic process is more than the following steps: first set a database to copy the copy attribute, make it a design original (known as the original version of the VB, referring to the design master); then according to the implementation of the application needs Original copies of multiple copies (VB's Name Replica), these copies have a copy collection (design is also seen as the first, initial copy); finally changed the data or structure of any replica The synchronization mechanism will be enabled to send changes and applied to other members in this copy of this copy, allowing members in the copies to consistency in data or structures. This process of achieving synchronization is called synchronization. VB6.0 provides multiple attributes and methods in the database object to implement this process, which introduces several steps to synchronize in the database objects.

1. Replicable properties:

The Replicable property is used to make objects such as a database object or a table object in a database, and become a copy of the design. But the database object does not provide this property, so we must first create it with the createProTy method, then add it to the object's properties set, and finalize it, make the database a design original. For database objects, set the replicable property to "T" will cause the database object to be copied. The following code will make the nwind.mdb database included in the VB6.0 installation directory into a design original (to ensure security suggestions, back up this library file before the operation):

Private Sub Command1_Click () DIM DBNWIND AS DATABASE 'If the end reference DAO must first reference the DIM PrpNew As Property Set dbnWind = OpenDatabase ("Nwind.mdb", true) with dbnwind' to establish a replicable property, if this property is already existing Slight this ON Error Resume next set prpNew = .createproperty ("replicable", dbtext, "t") .properties.Append prpNew 'Setting the replicable property of the database is True .properties ("replicable) =" t ".close End with end sub

2. MAKEREPLICA method:

The MakeReplica method copies a new full copy from the design original. Its syntax is: Database.makeReplica Replica, Description, Options, where replica is a string representing a new copy path name; Description is a description string for the new copy being created; Options is an option, which can be dbrepmakepartial constant (Create a part copy) or DBRepmakeReadOnly constant (prevent user modification of the copyable object in the new copy), if you want to create a read-only part copy, you should join the parameter constant DBRepmakeReadOnly DBRepmakePartial.

In the first example, the code is added before closing the database: .makeReplica "nwreplica", "Replica of nwind.mdb", select a copy named NWREPLICA.MDB from the NWIND.MDB design, where the location is in nwind. MDB in the same directory. The following is a function of flexible call by passing the parameters in practical applications, and you can create a new copy per call: Function MakeadDitionAlReplica (StrnePlica AS String, InTrNewReplica AS String, InTrNewReplica AS String, InTrNewReplica AS String, InTOTIONS INTEGER) AS INTEGER DIM DBSTEMP AS DATABASE IN ERROR GOTO ERRORHANDAL SET DBSTEMP = OPENDATABASE (STRREPLICABASE (StrrePlicableDB) If this function is called, the parameter item is given, then the parameter item is ignored, 'The default is established, and a complete, readable / written copy, otherwise on the use of the parameters provided on request a copy If intOptions = 0 then dbsTemp.MakeReplica strNewReplica, "replica of" & strReplicableDB Else dbsTemp.MakeReplica strNewReplica, "replica of" & strReplicableDB, intOptions End If dbsTemp.Close ErrorHandler: Select Case Err Case 0: makeadditionalRreplica = 0 exit function case else: msgbox "error" & err & ":" & error makeadditionalReplica = err exit function end select end function

3. SYNCHRONIZE method:

The SYNCHRONIZE method makes two complete copies (including design original) synchronization. Its syntax is: Database.synchronize Pathname, Exchange. Where Pathname is the path name string (in the string in the .mdb extension) to synchronize (omitted); Exchange is used to identify synchronization directions between the two databases (such as Table 1), this is an option, default For the third option in the table, the two-way exchange. Using the fourth DBREPSYNCINTERNET constant option in the table, you can also synchronize the database interconnected by the Internet. At this time, you should replace the local network path option Pathname with the URL address.

Table 1, synchronization direction constant

Constant synchronization direction DBREPEXPORTCHANGES DBREPEXPORTCHANGES DBREPIMPORTCHANGES DBREPIMPORTCHANGES Database DBREPIMPEXPCHANGES DBREPIMPEXPCHANGES DBREPIMPEXPCHANGES DBREPIMPEXPCHANGES DBRE DBRE DBREPSYNCINTERNET Transfer changes between databases connected via Internet Path

Before synchronization operations, make sure that the replicable property has enabled a database to initially design the original, and more than one copy is copied using the MakeReplica method.

By copying the statement after the first example is copied, the following statement is added: .synchronize "nwreplica.mdb", DBREPEXPORTCHANGES, to deliver any changes to the copy of the design of the database NWind to the copy NWREPLICA. We can change some data content in the NWIND.MDB library, then run this example, we will find that the change between the nwind.mdb library is reflected in the NWReplica.mdb. The above statement implements synchronization from the database to the copy path (passing the data or structural change of the design or structure to the copy), change the DBREPEXPORTCHANGES constant to DBREPIMPORTCHANGES and DBREPIMPEXPCHANGES to implement the copy path name to the database (change on the database reception) As well as two-way exchange (two-way data transfer between the two).

The Synchronize method can also synchronize the database through the Internet, the following statement implements the local database is initially synchronized with a copy of the Internet server: dbnwind.synchronize "www.mycompany.myserver.com" & "/ files / nwreplica. MDB ", DBREPIMPEXPCHANGES DBREPSYNCINTERNET

4. PopulatePartial method:

The above introduces the Synchronize method to synchronize two full copies, without problems, but if a full copy is used to synchronize a part copy, because some copies are re-generated from a copy filter to filter from a complete copy, there may be possible The so-called "isolated" record is generated in some copies, that is, these records cannot be synchronized with other copies. To resolve this problem, another method called PopulatePartial, which is similar to the SYNCHRONIZE method, but it is a synchronization of some copies and full copies, first clear all records in some copies, then according to The current copy filter regenerates some copies, which solves the problem of "Isolate" record. Its syntax is: Database.PopulatePartial DBName. DBNAME is a full copy of the path name. Due to space limitations and their similarity to the Synchronize method, this is no longer described in detail, please refer to the related online help.

转载请注明原文地址:https://www.9cbs.com/read-97749.html

New Post(0)