以编程方式从远程db sql server更新本地数据库 [英] updating a local database from remote db sql server programmatically

查看:70
本文介绍了以编程方式从远程db sql server更新本地数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我正在开发一个使用本地数据库的Win Forms应用程序,如何使用c#从远程数据库更新此本地数据库的值代码,是否可能?

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 ?

推荐答案

您好,



您可以使用SQL Server复制来实现。我们的想法是使用SQL服务器内置的功能来同步位于不同机器上的数据库。



有很多关于SQL复制的文献,做谷歌搜索和花一些时间阅读。



这是一个好的开始:

http://code.msdn.microsoft.com/windowsdesktop/SQL-Server-Express-05c73322 [ ^ ]



And这是Microsoft提供的代码示例:(在上面的链接中)



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。




Valery.


这篇关于以编程方式从远程db sql server更新本地数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆