我们已经创建了一个Windows窗体应用程序,用于使用合并发布和合并拉取订阅来同步数据库。我们已成功创建出版和订阅。现在我们要开始同步数据。
为此,我们要在SQL Command下面执行:
-- Declare the variables.
DECLARE @publicationDB AS sysname;
DECLARE @Subscriber AS sysname;
DECLARE @Publisher AS sysname;
DECLARE @SubscriptionDB AS sysname;
DECLARE @Publication AS sysname;
DECLARE @sql VARCHAR(8000);
DECLARE @sqlDist VARCHAR(8000);
SET @Publisher = 'MSSQLSERVER2014';
SET @Subscriber = 'SQLEXPRESS2014';
SET @PublicationDB = 'ServerDB' ;
SET @SubscriptionDB = 'ClinetDB';
SET @Publication = 'ServerDB_PUBLICATION';
--Start the Merge Agent with concurrent upload and download processes.
SET @sql = '"C:\Program Files\Microsoft SQL Server\130\COM\REPLMERG.EXE" -Publication ' + @Publication+ ' -Publisher ' + @Publisher + ' -Subscriber ' + @Subscriber + ' -Distributor ' + @Publisher + ' -PublisherDB ' + @PublicationDB + ' -SubscriberDB ' + @SubscriptionDB + ' -PublisherSecurityMode 0 -PublisherLogin sa -PublisherPassword Abc@1234 -OutputVerboseLevel 2 -SubscriberSecurityMode 1 -SubscriptionType 1 -DistributorSecurityMode 0 -DistributorLogin sa -DistributorPassword Abc@1234 -Validate 3 -ParallelUploadDownload 1'
EXEC master..xp_cmdshell @sql
如何在C#中执行上述命令?我们不会使用cmd或powershell。
我们已尝试过以下但在@publication上遇到语法错误:
string SP_xp_cmdshell = "'C:\\Program Files\\Microsoft SQL Server\\130\\COM\\REPLMERG.EXE' -Publication " + mPublication + " -Publisher " + mPublisher + " -Subscriber " + mSubscriber + " -Distributor " + mPublisher + " -PublisherDB " + mPublicationDatabase + " -SubscriberDB " + mSubscriptionDatabase + " -PublisherSecurityMode 0 -PublisherLogin " + mLogin + " -PublisherPassword " + mPassword + " -OutputVerboseLevel 2 -SubscriberSecurityMode 1 -SubscriptionType 1 -DistributorSecurityMode 0 -DistributorLogin " + mLogin + " -DistributorPassword " + mPassword + " -Validate 3 -ParallelUploadDownload 1";
cmd.Connection = mConnection; //master connection
cmd.CommandText = "xp_cmdshell "+ SP_xp_cmdshell;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
答案 0 :(得分:1)
xp_cmdshell用于从Sql Server Management Studio调用命令提示符。 要从SSMS执行REPLMERG.EXE,您需要使用路径和参数执行xp_cmdshell。
要从C#执行REPLMERG.EXE,请参阅以下代码:
string Query= @"""C:\\Program Files\\Microsoft SQL Server\\130\\COM\\REPLMERG.EXE"" " +
@"-Publisher [SQLSERVER] -PublisherDB [Server_Database] -Publication [PUBLICATION_NAME] " +
@"-Subscriber [SQLEXPRESS] -SubscriberDB [Express_Database] -SubscriptionType 1 " +
@"-SubscriberSecurityMode 0 -SubscriberLogin SQLLogin -SubscriberPassword SQLPass -Distributor [SQLSERVER] " +
@"-OutputVerboseLevel 2 -PublisherSecurityMode 0 -PublisherLogin SQLLogin -PublisherPassword SQLPass " +
@"-DistributorSecurityMode 0 -DistributorLogin SQLLogin -DistributorPassword SQLPass -Validate 1 -ParallelUploadDownload 1";
try
{
Process proc = new Process();
proc.StartInfo.FileName = "CMD.exe";
proc.StartInfo.Arguments = "/c " + Query;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //to hide console window
proc.Start();
proc.WaitForExit();
int result = proc.ExitCode;
if (result != 0) // exitcode is 0 for successful synchronization
{
throw new Exception();
}
}
catch (Exception ex)
{
// Implement appropriate error handling here.
Console.WriteLine("error:" + ex.Message);
}