2011-01-10 19 views
0

我在SQL Server 2008 CLR項目中有一個存儲過程。該函數使用包含一些xml的參數調用控制檯應用程序。從SQL CLR函數中調用System.Diagnostics.Process.Start,該函數包含與xml的參數

[Microsoft.SqlServer.Server.SqlProcedure()] 
public static SqlInt32 ExecuteApp(SqlString utilPath, SqlString arguments, ref SqlString result) 
{ 
    ProcessStartInfo info = new ProcessStartInfo("cmd.exe"); 
    Process p = new Process(); 

    try 
    { 
     //ProcessStartInfo to run the DOS command attrib 

     info.RedirectStandardOutput = true; 
     info.RedirectStandardError = true; 
     info.UseShellExecute = false; 
     info.CreateNoWindow = true; 
     info.Arguments = string.Format(@"/c ""{0}"" {1}", utilPath, arguments); 
     SqlContext.Pipe.Send(info.Arguments); 

     p.StartInfo = info; 
     p.Start(); 
     String processResults = p.StandardOutput.ReadToEnd(); 
     SqlContext.Pipe.Send(processResults); 
     result = processResults; 
     if (String.IsNullOrEmpty((String)result)) 
      result = p.StandardError.ReadToEnd(); 

     return 1; 
    } 
    finally 
    { 
     p.Close(); 
    } 
} 

這裏的問題與param包含xml。 這是執行存儲過程的一個表達式:

declare @Result nvarchar(max) 
exec ExecuteApp 'C:\Console.exe', 'send "<messages><message>my message</message></messages>"', @Result out 
select @Result 

執行存儲過程後我有這樣的錯誤:

<是意外的在這個時候。

我想注意到,沒有xml所有工作正常。

+0

您有_severe_安全漏洞。 – SLaks 2011-01-10 16:18:54

+1

你爲什麼不使用`xp_cmdshell`? – SLaks 2011-01-10 16:25:14

回答

1

您的XML包含由命令shell(><)解釋的特殊字符。
您需要引用參數並使用""將其內部的引號轉義。此外,你應該直接執行你的程序(而不是通過cmd /c);你應該直接執行你的程序(不通過cmd /c);而且,你應該直接執行你的程序(不通過cmd /c)。這將解決您的一些問題。

相關問題