CreateLogReaderAgent Method

ReplicationDatabase.CreateLogReaderAgent Method ()


Creates a Log Reader Agent job for a database published with transactional replication.

Namespace:   Microsoft.SqlServer.Replication
Assembly:  Microsoft.SqlServer.Rmo (in Microsoft.SqlServer.Rmo.dll)

public void CreateLogReaderAgent()

Exception Condition

When the Log Reader Agent job already exists.

When the replication database does not exist or has not been enabled for transactional publishing.

When the Log Reader Agent has not been upgraded to the Microsoft SQL Server 2005 replication agent security model. For more information, see Security and Protection (Replication).

The CreateLogReaderAgent method can be called by members of the sysadmin fixed server role and by members of the db_owner fixed database role.

Calling CreateLogReaderAgent is equivalent to executing sp_addlogreader_agent (Transact-SQL).

// Set the Publisher, publication database, and publication names.
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks2012";
string publisherName = publisherInstance;

ReplicationDatabase publicationDb;
TransPublication publication;

// Create a connection to the Publisher using Windows Authentication.
ServerConnection conn;
conn = new ServerConnection(publisherName);

	// Connect to the Publisher.

	// Enable the AdventureWorks2012 database for transactional publishing.
	publicationDb = new ReplicationDatabase(publicationDbName, conn);

	// If the database exists and is not already enabled, 
	// enable it for transactional publishing.
	if (publicationDb.LoadProperties())
		if (!publicationDb.EnabledTransPublishing)
			publicationDb.EnabledTransPublishing = true;

		// If the Log Reader Agent does not exist, create it.
		if (!publicationDb.LogReaderAgentExists)
			// Specify the Windows account under which the agent job runs.
			// This account will be used for the local connection to the 
			// Distributor and all agent connections that use Windows Authentication.
			publicationDb.LogReaderAgentProcessSecurity.Login = winLogin;
			publicationDb.LogReaderAgentProcessSecurity.Password = winPassword;

			// Explicitly set authentication mode for the Publisher connection
			// to the default value of Windows Authentication.
			publicationDb.LogReaderAgentPublisherSecurity.WindowsAuthentication = true;

			// Create the Log Reader Agent job.
		throw new ApplicationException(String.Format(
			"The {0} database does not exist at {1}.",
			publicationDb, publisherName));

	// Set the required properties for the transactional publication.
	publication = new TransPublication();
	publication.ConnectionContext = conn;
	publication.Name = publicationName;
	publication.DatabaseName = publicationDbName;

	// Specify a transactional publication (the default).
	publication.Type = PublicationType.Transactional;

	// Activate the publication so that we can add subscriptions.
	publication.Status = State.Active;

             // Enable push and pull subscriptions and independent Distribition Agents.
             publication.Attributes |= PublicationAttributes.AllowPull;
             publication.Attributes |= PublicationAttributes.AllowPush;
             publication.Attributes |= PublicationAttributes.IndependentAgent;

	// Specify the Windows account under which the Snapshot Agent job runs.
	// This account will be used for the local connection to the 
	// Distributor and all agent connections that use Windows Authentication.
	publication.SnapshotGenerationAgentProcessSecurity.Login = winLogin;
	publication.SnapshotGenerationAgentProcessSecurity.Password = winPassword;

	// Explicitly set the security mode for the Publisher connection
	// Windows Authentication (the default).
	publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = true;

	if (!publication.IsExistingObject)
		// Create the transactional publication.

		// Create a Snapshot Agent job for the publication.
		throw new ApplicationException(String.Format(
			"The {0} publication already exists.", publicationName));

catch (Exception ex)
	// Implement custom application error handling here.
	throw new ApplicationException(String.Format(
		"The publication {0} could not be created.", publicationName), ex);
Return to top
© 2015 Microsoft