This post originated from an RSS feed registered with .NET Buzz
by Doug Thews.
Original Post: Programming SQL CE Merge/Replication
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
Configuring a SQL Server database for Merge/Replication is the hardest part of developing a solution. 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 Conn. Str)"
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("Local DB Filename.sdf") 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
'TODO: Error handling code here
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.
You'll want to insert error handling in the area specified, 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.
(* re-posted for formatting)