我正在尝试使用RMO编程创建一个合并复制,我从here获得了!
string publisherName = "DataSourceName";
string publicationName = "AdvWorksSalesOrdersMerge";
string publicationDbName = "AdventureWorksDW2008R2";
ReplicationDatabase publicationDb;
MergePublication publication;
// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);
try
{
//Connect to the Publisher.
conn.Connect();
// Enable the database for merge publication.
publicationDb = new ReplicationDatabase(publicationDbName, conn);
if (publicationDb.LoadProperties())
{
if (!publicationDb.EnabledMergePublishing)
{
publicationDb.EnabledMergePublishing = true;
}
}
else
{
// Do something here if the database does not exist.
throw new ApplicationException(String.Format(
"The {0} database does not exist on {1}.",
publicationDb, publisherName));
}
// Set the required properties for the merge publication.
publication = new MergePublication();
publication.ConnectionContext = conn;
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
// Enable precomputed partitions.
publication.PartitionGroupsOption = PartitionGroupsOption.True;
//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 = userid;
publication.SnapshotGenerationAgentProcessSecurity.Password = password;
//Explicitly set the security mode for the Publisher connection
// Windows Authentication (the default).
publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = true;
//Enable Subscribers to request snapshot generation and filtering.
publication.Attributes |= PublicationAttributes.AllowSubscriberInitiatedSnapshot;
publication.Attributes |= PublicationAttributes.DynamicFilters;
// Enable pull and push subscriptions.
publication.Attributes |= PublicationAttributes.AllowPull;
publication.Attributes |= PublicationAttributes.AllowPush;
if (!publication.IsExistingObject)
{
//Create the merge publication.
publication.Create();
// Create a Snapshot Agent job for the publication.
publication.CreateSnapshotAgent();
}
else
{
throw new ApplicationException(String.Format(
"The {0} publication already exists.", publicationName));
}
}
catch (Exception ex)
{
//Implement custom application error handling here.
throw new Exception(String.Format("The publication {0} could not be created.", publicationName), ex);
}
finally
{
conn.Disconnect();
}
但在这一行
publicationDb.EnabledTransPublishing = true;
我收到错误 - “ 执行Transact-SQL语句或批处理时发生异常。 ”
所以请帮我解决这个问题。
等待你的答案..
答案 0 :(得分:0)
你可能现在已经回答了问题,但对于那些可能会问同样问题的人。这是因为您使用的是SQL Server的快速版本,并且无法在任何版本的SQl Server Express中创建发布者/分发者。
您的代码中的实例没有有效的实例,因此会引发异常。
看看:
http://msdn.microsoft.com/en-us/library/ms151819(v=sql.105).aspx
和说
的行Microsoft SQL Server 2008 Express可以作为所有类型复制的订阅者,提供了一种将数据分发到使用此版本SQL Server的客户端应用程序的便捷方法。在复制拓扑中使用SQL Server Express时,请记住以下注意事项: SQL Server Express不能用作发布服务器或分发服务器。但是,合并复制允许在发布服务器和订阅服务器之间双向复制更改。 块引用