Skip to main content

Articles

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Sapphire Soltuions

Using replication on a SQL Server Database

Written by Pete Vickers  [author's bio]  [read 15585 times]
Edited by Derek

Download the code

Page 1  Page 2  Page 3 

Figure 10 – Basic screen layout

First of all we set up our replication parameters.

Dim replicator As New SqlCeReplication

replicator.InternetUrl = interneturl

replicator.Publisher = publisher_server

replicator.PublisherDatabase = publisher_database

replicator.PublisherSecurityMode = publisher_secmode

replicator.Publication = publication

replicator.Subscriber = subscriber

replicator.SubscriberConnectionString = connection_string

Table 2 explains the settings for replication.

Table 2 – Replication parameters

Name

Description

InternetURL

SQL Server CE communicates with a SQL Server Agent (sscesa20.dll) running on the desktop computer by means of HTTP. IIS on the desktop computer has been configured to accept SQL Server CE requests and forward them to SQL Server by using the SQL Server Agent. The InternetURL property specifies the URL that has been configured for this purpose. In this example, the value is http://192.168.1.25 /Northwind/sscesa20.dll. You will need to replace 192.168.1.25 with the IP address of your server running IIS. Refer to Figures 7 and 9 for verification.

Publisher

The Publisher property identifies the instance of SQL Server publishing the publication in the form machineName\SQLServerInstanceName. In this example, the value is delldesktop.

PublisherDatabase

The PublisherDatabase is the Sql Server database we are using for Replication. In this example, it is Northwind

PublisherSecurityMode

The PublisherSecurityMode determines whether the subscriber application will use NT authentication (as in our sample), or PublisherLogin and PublisherPassword entries e.g. ‘sa’ and ‘admin’.

Publication

This identifies the subscribing device or computer.

Subscriber

This identifies the subscribing device or computer. This example uses DNS.Gethostname() to send the name of the device.

SubscriberConnectionString

This specifies the connection string for the local ‘northwind’ database, and is used both in replication, and in accessing the local database.

Next we attempt to connect to the IIS system, and create our local copy of ‘northwind’, by using AddSubscription

If Not System.IO.File.Exists(local_database) Then

Try

replicator.AddSubscription(AddOption.CreateDatabase)

Catch ex As Exception

Cursor.Current = Cursors.Default

DisplaySQLCeErrors(ex, "Add")

End Try

End If

After we have created the database, we then attempt to populate it with the data from our publication (our ‘northwind’ SQL Server database).

Try

replicator.Synchronize()

Cursor.Current = Cursors.Default

MessageBox.Show("Database synched with Server", "Finished")

Open_Local_Database()

Refresh_Grid()

Catch ex As Exception

Cursor.Current = Cursors.Default

DisplaySQLCeErrors(ex, "Synch")

End Try

If this is successful, we display a message, as shown in Figure 11, otherwise we display the error collection, in the same way we did in the SQLCLIEN sample.

Figure 11 – Successful creation and replication.

We then open the local database, and display the regions in a grid control as show in figure 12.

Figure 12 – Records displayed from local database after replication.

Our application so far has connected to our IIS server, created a local database, and populated it. We will now move on to adding records to our local database, and replicating these back to our SQL Server. The code behind the Add Region button, will bring up a data-entry form, allowing us to enter a new region and description. Our data entry form will also do some validation, to ensure the fields are filled in correctly, and we are not trying to add a duplicate entry.

strSql = "Insert into region (regionid,regiondescription) values ("

strSql += txtID.Text & ",'" & txtRegion.Text & "')"

SqlCmd = New SqlCeCommand

SqlCmd = sqlCeConn.CreateCommand

SqlCmd.CommandText = strSql

Try

SqlCmd.ExecuteNonQuery()

SqlCmd = Nothing

Catch ex As SqlCeException

DisplaySQLCeErrors(ex, strSql)

End Try

Previous Page  Next Page