使用 C# 执行 xp_cmdshell

Execute xp_cmdshell using C#

我们已经创建了一个 Windows Forms 应用程序来使用合并发布和合并请求订阅来同步数据库。我们已经成功创建了发布和订阅。现在我们要开始同步数据了。

为此,我们要执行以下 SQL 命令:

-- 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 Server0\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();

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);
             }