· 6 years ago · Jul 01, 2019, 03:46 PM
1// Set the Publisher, publication database, and publication names.
2 string publicationName = "productionTran";
3 string publicationDbName = "production1";
4 string publisherName = "OSKAR-KOMPUTER";
5
6 ReplicationDatabase publicationDb;
7 TransPublication publication;
8
9 // Create a connection to the Publisher using Windows Authentication.
10 ServerConnection conn;
11 conn = new ServerConnection(publisherName);
12
13
14 try
15 {
16 // Connect to the Publisher.
17 conn.Connect();
18
19 // Enable the AdventureWorks database for transactional publishing.
20 publicationDb = new ReplicationDatabase(publicationDbName, conn);
21
22 // If the database exists and is not already enabled,
23 // enable it for transactional publishing.
24 if (publicationDb.LoadProperties())
25 {
26 if (!publicationDb.EnabledTransPublishing)
27 {
28 publicationDb.EnabledTransPublishing = true;
29 }
30
31 // If the Log Reader Agent does not exist, create it.
32 if (!publicationDb.LogReaderAgentExists)
33 {
34 // Specify the Windows account under which the agent job runs.
35 // This account will be used for the local connection to the
36 // Distributor and all agent connections that use Windows Authentication.
37 //publicationDb.LogReaderAgentProcessSecurity.Login = winLogin;
38 //publicationDb.LogReaderAgentProcessSecurity.Password = winPassword;
39
40 // Explicitly set authentication mode for the Publisher connection
41 // to the default value of Windows Authentication.
42 publicationDb.LogReaderAgentPublisherSecurity.WindowsAuthentication = true;
43
44 // Create the Log Reader Agent job.
45 publicationDb.CreateLogReaderAgent();
46 }
47 }
48 else
49 {
50 throw new ApplicationException(String.Format(
51 "The {0} database does not exist at {1}.",
52 publicationDb, publisherName));
53 }
54
55 // Set the required properties for the transactional publication.
56 publication = new TransPublication();
57 publication.ConnectionContext = conn;
58 publication.Name = publicationName;
59 publication.DatabaseName = publicationDbName;
60
61 // Specify a transactional publication (the default).
62 publication.Type = PublicationType.Transactional;
63
64 // Activate the publication so that we can add subscriptions.
65 publication.Status = State.Active;
66
67 // Enable push and pull subscriptions and independent Distribition Agents.
68 publication.Attributes |= PublicationAttributes.AllowPull;
69 publication.Attributes |= PublicationAttributes.AllowPush;
70 publication.Attributes |= PublicationAttributes.IndependentAgent;
71
72 // Specify the Windows account under which the Snapshot Agent job runs.
73 // This account will be used for the local connection to the
74 // Distributor and all agent connections that use Windows Authentication.
75 // publication.SnapshotGenerationAgentProcessSecurity.Login = winLogin;
76 // publication.SnapshotGenerationAgentProcessSecurity.Password = winPassword;
77
78 // Explicitly set the security mode for the Publisher connection
79 // Windows Authentication (the default).
80 publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = true;
81
82 if (!publication.IsExistingObject)
83 {
84 // Create the transactional publication.
85 publication.Create();
86
87 // Create a Snapshot Agent job for the publication.
88 publication.CreateSnapshotAgent();
89 }
90 else
91 {
92 throw new ApplicationException(String.Format(
93 "The {0} publication already exists.", publicationName));
94 }
95 }
96
97 catch (Exception ex)
98 {
99 // Implement custom application error handling here.
100 throw new ApplicationException(String.Format(
101 "The publication {0} could not be created.", publicationName), ex);
102 }
103 finally
104 {
105 conn.Disconnect();
106 }
107
108DROP TABLE dbo.iharticles;
109DROP TABLE dbo.ihcolumns;
110DROP TABLE dbo.ihconstrainttypes;
111DROP TABLE dbo.ihindextypes;
112DROP TABLE dbo.ihpublications;
113DROP TABLE dbo.ihpublishercolumnconstraints;
114DROP TABLE dbo.ihpublishercolumnindexes;
115DROP TABLE dbo.ihpublishercolumns;
116DROP TABLE dbo.ihpublisherconstraints;
117DROP TABLE dbo.ihpublisherindexes;
118DROP TABLE dbo.ihpublishers;
119DROP TABLE dbo.ihpublishertables;
120DROP TABLE dbo.ihsubscriptions;
121DROP TABLE dbo.syssubscriptions
122DROP VIEW dbo.syspublications;
123DROP VIEW dbo.sysarticlecolumns;
124DROP VIEW dbo.syssubscriptions;
125DROP VIEW dbo.sysextendedarticlesview;
126DROP VIEW dbo.ihextendedarticleview;
127DROP VIEW dbo.ihextendedsubscriptionview;