This post originated from an RSS feed registered with .NET Buzz
by Doug Thews.
Original Post: So You Think SQL CE Merge/Replication is Hard?
Feed Title: IlluminatiLand
Feed URL: http://apps5.oingo.com/apps/domainpark/domainpark.cgi?client=netw8744&s=JETBRAINS.COM
Feed Description: A technology blog for people enlightened enough to think for themselves
Actually, configuring a SQL Server database for Merge/Replication is the hard part. Here's the snippet of code you'll need to perform Merge/Replication with the SQL Merge Agent. Notice that the only thing to consider is whether to create the local database or use the existing one.
Imports System.Data.SqlServerCe
Imports System.Data.SqlServerCe.SqlCeException
Dim oRepl As New SqlCeReplication
' Set the values for the CE replication object
oRepl.Publisher = "(SQL Server Name)"
oRepl.PublisherDatabase = "(Publication Database)"
oRepl.Publication = "(Publication Name)"
oRepl.PublisherLogin = "(Publisher Login)"
oRepl.PublisherPassword = "(Publisher Password)"
oRepl.SubscriberConnectionString = "(Local DB Connection String)"
oRepl.Subscriber = "(Subscriber Name)"
oRepl.InternetUrl = "(SQL CE IIS URL)"
oRepl.InternetLogin = "(SQL CE IIS Login)"
oRepl.InternetPassword = "(SQL CE IIS Password)"
' Make sure we get updates as well as send them
oRepl.ExchangeType = ExchangeType.BiDirectional
Try
' If the local DB exists, then just synch to it,
If Not File.Exists("") Then
' Tell SQL CE to create the database
oRepl.AddSubscription(AddOption.CreateDatabase)
End If
' Now synchronize with the current snapshot
' in Merge Replication
oRepl.Synchronize()
Catch ex As SqlCeException
MessageBox(objSqlMessage.ComposeSqlErrorMessage(ex))
End Try
' Cleanup
oRepl.Dispose()
Once you create the local database, you can then use the familiar objects like SqlCeConnection and SqlCeDataAdapter (just like their ADO.NET counterparts) to get your data into datatables and datasets so that you can work with them just like a WinForms (or ASP.NET) application.
Now, you probably will want better error handling that what this example shows, but hopefully what you gain from this is that there is not that much additional coding overhead to use erge/Replication to create disconnected database applications on the CF.NET framework.