public static void SysPublication() { // Define the server, publication, and database names. string subscriberName = "NGUYEN-PC\\SQLSERVER_1"; string publisherName = "NGUYEN-PC"; string publicationName = "TN_CSDLPT_CS1"; string subscriptionDbName = "TN_CSDLPT"; string publicationDbName = "TN_CSDLPT"; // Create a connection to the Publisher. ServerConnection conn = new ServerConnection(publisherName); MergeSubscription subscription; try { // Connect to the Publisher conn.Connect(); // Define the subscription. subscription = new MergeSubscription(); subscription.ConnectionContext = conn; subscription.DatabaseName = publicationDbName; subscription.PublicationName = publicationName; subscription.SubscriptionDBName = subscriptionDbName; subscription.SubscriberName = subscriberName; // If the push subscription exists, start the synchronization. if (subscription.LoadProperties()) { // Check that we have enough metadata to start the agent. if (subscription.SubscriberSecurity != null) { // Synchronously start the Merge Agent for the subscription. subscription.SynchronizationAgent.Synchronize(); } else { throw new ApplicationException("There is insufficent metadata to " + "synchronize the subscription. Recreate the subscription with " + "the agent job or supply the required agent properties at run time."); } } else { // Do something here if the push subscription does not exist. throw new ApplicationException(String.Format( "The 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.", ex); } finally { conn.Disconnect(); } }
public void SynchronizeMergePullSubscriptionViaRMO() { // Create a connection to the Subscriber. //ServerConnection conn = new ServerConnection(subscriberName); ServerConnection conn = new ServerConnection(publisherName, srvLogin, srvPass); // Merge pull subscription MergeSubscription subscription; try { // Connect to the Subscriber. conn.Connect(); // Define the pull subscription. subscription = new MergeSubscription(); subscription.ConnectionContext = conn; subscription.DatabaseName = publicationDbName; subscription.PublicationName = publicationName; subscription.SubscriptionDBName = subscriptionDbName; subscription.SubscriberName = subscriberName; subscription.PublisherSecurity.SqlStandardLogin = srvLogin; subscription.PublisherSecurity.SqlStandardPassword = srvPass; // If the pull subscription exists, then start the synchronization. if (subscription.LoadProperties() && subscription.AgentJobId != null) { // Get the agent for the subscription. agent = subscription.SynchronizationAgent; // Set the required properties that could not be returned // from the MSsubscription_properties table. agent.DistributorSecurityMode = SecurityMode.Standard; agent.DistributorLogin = srvLogin; agent.DistributorPassword = srvPass; agent.PublisherSecurityMode = SecurityMode.Standard; agent.PublisherLogin = srvLogin; agent.PublisherPassword = srvPass; // Enable verbose merge agent output to file. //agent.OutputVerboseLevel = 4; //agent.Output = "C:\\TEMP\\mergeagent.log"; // Handle the Status event agent.Status += new AgentCore.StatusEventHandler(agent_Status); // 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(); } }
private void btnPushData_Click(object sender, EventArgs e) { ServerConnection conn = new ServerConnection(publisherName, srvLogin, srvPass); MergeSubscription subscription; try { // Connect to the Publisher. conn.Connect(); //conn.Login = srvLogin; //conn.Password = srvPass; // Define push subscription. subscription = new MergeSubscription(); subscription.ConnectionContext = conn; subscription.DatabaseName = publicationDbName; subscription.PublicationName = publicationName; subscription.SubscriptionDBName = subscriptionDbName; subscription.SubscriberName = subscriberName; subscription.PublisherSecurity.SqlStandardLogin = srvLogin; subscription.PublisherSecurity.SqlStandardPassword = srvPass; // If the push subscription and the job exists, start the agent job. if (subscription.LoadProperties() && subscription.AgentJobId != null) { // Start the Merge Agent asynchronously. //subscription.SynchronizeWithJob(); subscription.SynchronizationAgent.DistributorSecurityMode = SecurityMode.Standard; subscription.SynchronizationAgent.DistributorLogin = srvLogin; subscription.SynchronizationAgent.DistributorPassword = srvPass; subscription.SynchronizationAgent.PublisherSecurityMode = SecurityMode.Standard; subscription.SynchronizationAgent.PublisherLogin = srvLogin; subscription.SynchronizationAgent.PublisherPassword = srvPass; subscription.SynchronizationAgent.Synchronize(); //Create this message box in case data exchange is successful MessageBox.Show(" " + subscriberName + " successfully exchanged data with " + publisherName + " ", "SETS: System Data Exchange Window", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); } else { // Do something here if the subscription does not exist. throw new ApplicationException(String.Format( "A subscription to '{0}' does not exists on {1}", publicationName, subscriberName)); } } catch (Exception ex) { // Implement appropriate error handling here. throw new ApplicationException("The subscription could not be synchronized.", ex); } finally { conn.Disconnect(); } }