private void CreateSubscription() { ReplicationDatabase replDatabase; Server subServer; Database newDatabase; MergeSynchronizationAgent syncAgent; ReplicationServer Subscriber; BusinessLogicHandler insertUpdateHandler; Boolean isRegistered = false; this.Show(); Application.DoEvents(); // We need to collect valid Windows credentials to be able to // store Web synchronization information in the database. LogonUser logon = new LogonUser(); DialogResult dr = logon.ShowDialog(this); string username = logon.UserName; string password = logon.Password; if (dr == DialogResult.OK) { //try //{ // // Create connections to the Publisher and Distributor. // publisherConn = new ServerConnection(publisherServer); // publisherConn.Connect(); //} //catch (Microsoft.SqlServer.Replication.ConnectionFailureException ex) //{ // ExceptionMessageBox emb = new ExceptionMessageBox( // Properties.Resources.ExceptionCannotConnectPublisher, // Properties.Resources.ExceptionSqlServerError, // ExceptionMessageBoxButtons.OK); // emb.InnerException = ex; // emb.Show(this); // // Shutdown the application because we can't continue. // Application.Exit(); //} try { // Make the connection and get the subscription properties. subscriberConn = new ServerConnection(subscriberServer); subscriberConn.Connect(); } catch (Microsoft.SqlServer.Replication.ConnectionFailureException ex) { ExceptionMessageBox emb = new ExceptionMessageBox( Properties.Resources.ExceptionCannotConnectLocal, Properties.Resources.ExceptionSqlServerError, ExceptionMessageBoxButtons.OK); emb.InnerException = ex; emb.Show(this); // Shutdown the application because we can't continue. Application.Exit(); } // Instantiate a pull subscription object. mergePullSub = new MergePullSubscription(); // Set the required properties needed to create the subscription. mergePullSub.ConnectionContext = subscriberConn; mergePullSub.PublicationName = publicationName; mergePullSub.PublisherName = publisherServer; mergePullSub.PublicationDBName = publicationDatabase; mergePullSub.DatabaseName = subscriptionDatabase; // Specify an anonymous Subscriber type since we can't // register at the Publisher with a direct connection. mergePullSub.SubscriberType = MergeSubscriberType.Anonymous; // Enable Web synchronization. mergePullSub.UseWebSynchronization = true; mergePullSub.InternetUrl = Properties.Settings.Default.WebSynchronizationUrl; // Specify the same Windows credentials to use when connecting to the // Web server using HTTPS Basic Authentication. mergePullSub.InternetSecurityMode = AuthenticationMethod.BasicAuthentication; //mergePullSub.InternetLogin = username; //mergePullSub.InternetPassword = password; mergePullSub.InternetLogin = internetLogin; mergePullSub.InternetPassword = internetPassword; // Create the subscription database using SMO if it does not exist. subServer = new Server(subscriberConn); replDatabase = new ReplicationDatabase( mergePullSub.DatabaseName, subscriberConn); newDatabase = subServer.Databases[replDatabase.Name]; // Create the subscription database. if (subServer.Databases.Contains(replDatabase.Name) == false) { currentStatusTextBox.Text += statusCreateDatabase + Environment.NewLine; Application.DoEvents(); newDatabase = new Database(subServer, replDatabase.Name); newDatabase.Create(); } replDatabase.Load(); replDatabase.EnabledMergePublishing = false; replDatabase.CommitPropertyChanges(); // This is needed so that we can store Web synchronization // information in the MSsubscription_properties table and access this // information using the MergePullSubscription class. For a SQL Server 2005 // Subscriber an agent job will be created. For a SQL Server 2005 Express Edition // Subscriber, only the meta data will be created because this edition does not // support SQL Server Agent. mergePullSub.CreateSyncAgentByDefault = true; // Set the process security which is required for the agent job. mergePullSub.SynchronizationAgentProcessSecurity.Login = username; mergePullSub.SynchronizationAgentProcessSecurity.Password = password; // Set the HostName property. mergePullSub.HostName = subscriberHostName; // 调用 远程接口注册该订阅 /*DBCenterWebService service = new DBCenterWebService(); * SubscriptionInfo info = new SubscriptionInfo(); * info.subscriberName = mergePullSub.Name; * info.subscriptionDbName = mergePullSub.DatabaseName; * service.CreateSubscriptionRequest(info);*/ //if (!mergePub.SnapshotAvailable) //{ // throw new ApplicationException( // Properties.Resources.ExceptionSalesDataNotAvailable // + Environment.NewLine // + Properties.Resources.ExceptionContactTechSupport); //} // Define the handler for inserts and updates at the Subscriber. Subscriber = new ReplicationServer(subscriberConn); insertUpdateHandler = new BusinessLogicHandler(); insertUpdateHandler.FriendlyName = handlerFriendlyName; insertUpdateHandler.DotNetAssemblyName = "BusinessLogic.dll"; insertUpdateHandler.DotNetClassName = "Microsoft.Samples.SqlServer.CustomBusinessLogicHandler"; insertUpdateHandler.IsDotNetAssembly = true; try { // Create the pull subscription. currentStatusTextBox.Text += statusCreateSubscription + Environment.NewLine; Application.DoEvents(); mergePullSub.Create(); mergePullSub.Refresh(); // Get the Merge Agent for synchronous execution. syncAgent = mergePullSub.SynchronizationAgent; // We have to set these because of an RMO bug. // Remove for RTM. syncAgent.DistributorSecurityMode = SecurityMode.Integrated; syncAgent.PublisherSecurityMode = SecurityMode.Integrated; syncAgent.SubscriberSecurityMode = SecurityMode.Integrated; // Generate a troubleshooting log file. syncAgent.OutputVerboseLevel = outputLevel; syncAgent.Output = outputLogFile; // Define the event handler. syncAgent.Status += new AgentCore.StatusEventHandler(Sync_Status); currentStatusTextBox.Text += statusInitialize + Environment.NewLine; Application.DoEvents(); // Start the Merge Agent synchronously to apply the initial snapshot. syncAgent.Synchronize(); // Make sure that the initialization was successful. mergePullSub.Refresh(); if (mergePullSub.LastAgentStatus != ReplicationStatus.Succeeded) { throw new SubscriptionInitializationException( Properties.Resources.ExceptionSubscriptionNotSync); } currentStatusTextBox.Text += statusSuccess.ToString() + Environment.NewLine; statusProgressBar.Value = 100; } catch (Exception ex) { currentStatusTextBox.Text += statusFail.ToString() + Environment.NewLine; statusProgressBar.Value = 0; // If an exception occurs, undo subscription registration at both // the Publisher and Subscriber and remove the handler registration. mergePullSub.Remove(); if (isRegistered) { Subscriber.UnregisterBusinessLogicHandler(insertUpdateHandler); isRegistered = false; } throw new SubscriptionCreationException( Properties.Resources.ExceptionSubscriptionNotCreated, ex); } finally { closeButton.Enabled = true; subscriberConn.Disconnect(); //publisherConn.Disconnect(); } } else { // If we can't create the subscription, close the application. ExceptionMessageBox emb = new ExceptionMessageBox( Properties.Resources.ExceptionSubscriptionNotCreated); emb.Buttons = ExceptionMessageBoxButtons.RetryCancel; DialogResult drRetry = emb.Show(this); if (drRetry == DialogResult.Retry) { this.CreateSubscription(); } else { Application.Exit(); } } }
private void CreateSubscription() { ReplicationDatabase replDatabase; Server subServer; Database newDatabase; MergeSynchronizationAgent syncAgent; ReplicationServer Subscriber; BusinessLogicHandler insertUpdateHandler; Boolean isRegistered = false; this.Show(); Application.DoEvents(); // We need to collect valid Windows credentials to be able to // store Web synchronization information in the database. LogonUser logon = new LogonUser(); DialogResult dr = logon.ShowDialog(this); string username = logon.UserName; string password = logon.Password; if (dr == DialogResult.OK) { //try //{ // // Create connections to the Publisher and Distributor. // publisherConn = new ServerConnection(publisherServer); // publisherConn.Connect(); //} //catch (Microsoft.SqlServer.Replication.ConnectionFailureException ex) //{ // ExceptionMessageBox emb = new ExceptionMessageBox( // Properties.Resources.ExceptionCannotConnectPublisher, // Properties.Resources.ExceptionSqlServerError, // ExceptionMessageBoxButtons.OK); // emb.InnerException = ex; // emb.Show(this); // // Shutdown the application because we can't continue. // Application.Exit(); //} try { // Make the connection and get the subscription properties. subscriberConn = new ServerConnection(subscriberServer); subscriberConn.Connect(); } catch (Microsoft.SqlServer.Replication.ConnectionFailureException ex) { ExceptionMessageBox emb = new ExceptionMessageBox( Properties.Resources.ExceptionCannotConnectLocal, Properties.Resources.ExceptionSqlServerError, ExceptionMessageBoxButtons.OK); emb.InnerException = ex; emb.Show(this); // Shutdown the application because we can't continue. Application.Exit(); } // Instantiate a pull subscription object. mergePullSub = new MergePullSubscription(); // Set the required properties needed to create the subscription. mergePullSub.ConnectionContext = subscriberConn; mergePullSub.PublicationName = publicationName; mergePullSub.PublisherName = publisherServer; mergePullSub.PublicationDBName = publicationDatabase; mergePullSub.DatabaseName = subscriptionDatabase; // Specify an anonymous Subscriber type since we can't // register at the Publisher with a direct connection. mergePullSub.SubscriberType = MergeSubscriberType.Anonymous; // Enable Web synchronization. mergePullSub.UseWebSynchronization = true; mergePullSub.InternetUrl = Properties.Settings.Default.WebSynchronizationUrl; // Specify the same Windows credentials to use when connecting to the // Web server using HTTPS Basic Authentication. mergePullSub.InternetSecurityMode = AuthenticationMethod.BasicAuthentication; //mergePullSub.InternetLogin = username; //mergePullSub.InternetPassword = password; mergePullSub.InternetLogin = internetLogin; mergePullSub.InternetPassword = internetPassword; // Create the subscription database using SMO if it does not exist. subServer = new Server(subscriberConn); replDatabase = new ReplicationDatabase( mergePullSub.DatabaseName, subscriberConn); newDatabase = subServer.Databases[replDatabase.Name]; // Create the subscription database. if (subServer.Databases.Contains(replDatabase.Name) == false) { currentStatusTextBox.Text += statusCreateDatabase + Environment.NewLine; Application.DoEvents(); newDatabase = new Database(subServer, replDatabase.Name); newDatabase.Create(); } replDatabase.Load(); replDatabase.EnabledMergePublishing = false; replDatabase.CommitPropertyChanges(); // This is needed so that we can store Web synchronization // information in the MSsubscription_properties table and access this // information using the MergePullSubscription class. For a SQL Server 2005 // Subscriber an agent job will be created. For a SQL Server 2005 Express Edition // Subscriber, only the meta data will be created because this edition does not // support SQL Server Agent. mergePullSub.CreateSyncAgentByDefault = true; // Set the process security which is required for the agent job. mergePullSub.SynchronizationAgentProcessSecurity.Login = username; mergePullSub.SynchronizationAgentProcessSecurity.Password = password; // Set the HostName property. mergePullSub.HostName = subscriberHostName; // 调用 远程接口注册该订阅 /*DBCenterWebService service = new DBCenterWebService(); SubscriptionInfo info = new SubscriptionInfo(); info.subscriberName = mergePullSub.Name; info.subscriptionDbName = mergePullSub.DatabaseName; service.CreateSubscriptionRequest(info);*/ //if (!mergePub.SnapshotAvailable) //{ // throw new ApplicationException( // Properties.Resources.ExceptionSalesDataNotAvailable // + Environment.NewLine // + Properties.Resources.ExceptionContactTechSupport); //} // Define the handler for inserts and updates at the Subscriber. Subscriber = new ReplicationServer(subscriberConn); insertUpdateHandler = new BusinessLogicHandler(); insertUpdateHandler.FriendlyName = handlerFriendlyName; insertUpdateHandler.DotNetAssemblyName = "BusinessLogic.dll"; insertUpdateHandler.DotNetClassName = "Microsoft.Samples.SqlServer.CustomBusinessLogicHandler"; insertUpdateHandler.IsDotNetAssembly = true; try { // Create the pull subscription. currentStatusTextBox.Text += statusCreateSubscription + Environment.NewLine; Application.DoEvents(); mergePullSub.Create(); mergePullSub.Refresh(); // Get the Merge Agent for synchronous execution. syncAgent = mergePullSub.SynchronizationAgent; // We have to set these because of an RMO bug. // Remove for RTM. syncAgent.DistributorSecurityMode = SecurityMode.Integrated; syncAgent.PublisherSecurityMode = SecurityMode.Integrated; syncAgent.SubscriberSecurityMode = SecurityMode.Integrated; // Generate a troubleshooting log file. syncAgent.OutputVerboseLevel = outputLevel; syncAgent.Output = outputLogFile; // Define the event handler. syncAgent.Status += new AgentCore.StatusEventHandler(Sync_Status); currentStatusTextBox.Text += statusInitialize + Environment.NewLine; Application.DoEvents(); // Start the Merge Agent synchronously to apply the initial snapshot. syncAgent.Synchronize(); // Make sure that the initialization was successful. mergePullSub.Refresh(); if (mergePullSub.LastAgentStatus != ReplicationStatus.Succeeded) { throw new SubscriptionInitializationException( Properties.Resources.ExceptionSubscriptionNotSync); } currentStatusTextBox.Text += statusSuccess.ToString() + Environment.NewLine; statusProgressBar.Value = 100; } catch (Exception ex) { currentStatusTextBox.Text += statusFail.ToString() + Environment.NewLine; statusProgressBar.Value = 0; // If an exception occurs, undo subscription registration at both // the Publisher and Subscriber and remove the handler registration. mergePullSub.Remove(); if (isRegistered) { Subscriber.UnregisterBusinessLogicHandler(insertUpdateHandler); isRegistered = false; } throw new SubscriptionCreationException( Properties.Resources.ExceptionSubscriptionNotCreated, ex); } finally { closeButton.Enabled = true; subscriberConn.Disconnect(); //publisherConn.Disconnect(); } } else { // If we can't create the subscription, close the application. ExceptionMessageBox emb = new ExceptionMessageBox( Properties.Resources.ExceptionSubscriptionNotCreated); emb.Buttons = ExceptionMessageBoxButtons.RetryCancel; DialogResult drRetry = emb.Show(this); if (drRetry == DialogResult.Retry) { this.CreateSubscription(); } else { Application.Exit(); } } }