Click here to Skip to main content
16,004,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I'm developing a Win Forms application which uses a local database, how can i update this local DB with values from a remote DB using c# code, is it possible ?
Posted
Updated 31-Jan-14 23:02pm
v2

1 solution

Hello,

You could do that using SQL server replication. The idea is to use features built in SQL server to allow synchronising databases located on different machines.

There is a lot of literature on SQL replication, do a google search and spend some time reading.

Here's a good start:
http://code.msdn.microsoft.com/windowsdesktop/SQL-Server-Express-05c73322[^]

And here is a code sample provided by Microsoft: (in the link above)

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

// These namespaces are required.
using Microsoft.SqlServer.Replication;
using Microsoft.SqlServer.Management.Common;

namespace MergeAgentRMO
{
    class Program
    {
        static void Main(string[] args)
        {
            SynchronizeMergePullSubscriptionViaRMO();
        }

        static void SynchronizeMergePullSubscriptionViaRMO()
        {
            // Define the server, publication, and database names.
            string subscriberName = "WIN8CP\\SQLEXPRESS";
            string publisherName = "WS2008R2_1";
            string distributorName = "WS2008R2_1";
            string publicationName = "TestMergePub2";
            string subscriptionDbName = "TestSubDB1";
            string publicationDbName = "AdventureWorksLT";

            // Create a connection to the Subscriber.
            ServerConnection conn = new ServerConnection(subscriberName);

            MergePullSubscription subscription;
            MergeSynchronizationAgent agent;

            try
            {
                // Connect to the Subscriber.
                conn.Connect();

                // Define the pull subscription.
                subscription = new MergePullSubscription();
                subscription.ConnectionContext = conn;
                subscription.DatabaseName = subscriptionDbName;
                subscription.PublisherName = publisherName;
                subscription.PublicationDBName = publicationDbName;
                subscription.PublicationName = publicationName;

                // If the pull subscription exists, then start the synchronization.
                if (subscription.LoadProperties())
                {
                    // Get the agent for the subscription.
                    agent = subscription.SynchronizationAgent;

                    // Set the required properties that could not be returned
                    // from the MSsubscription_properties table.
                    agent.PublisherSecurityMode = SecurityMode.Integrated;
                    agent.DistributorSecurityMode = SecurityMode.Integrated;
                    agent.Distributor = publisherName;

                    // Enable verbose merge agent output to file.
                    agent.OutputVerboseLevel = 4;
                    agent.Output = "C:\\TEMP\\mergeagent.log";

                    // Synchronously start the Merge Agent for the subscription.
                    agent.Synchronize();
                }
                else
                {
                    // Do something here if the pull subscription does not exist.
                    throw new ApplicationException(String.Format(
                        "A subscription to '{0}' does not exist on {1}",
                        publicationName, subscriberName));
                }
            }
            catch (Exception ex)
            {
                // Implement appropriate error handling here.
                throw new ApplicationException("The subscription could not be " +
                    "synchronized. Verify that the subscription has " +
                    "been defined correctly.", ex);
            }
            finally
            {
                conn.Disconnect();
            }
        }
    }
}



Valery.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900