0

我在 SQL Server 2008 CLR 项目中有一个存储过程。此函数使用包含一些 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();
    }
}

这里的问题是包含 xml 的参数。这是执行存储过程的表达式:

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

执行存储过程后,出现如下错误:

<此时是出乎意料的。

我想注意到没有 xml 一切正常。

4

1 回答 1

2

您的 XML 包含由命令 shell (><) 解释的特殊字符。
您需要用 . 引用参数并在其中转义引号""

此外,您应该直接执行您的程序(而不是通过cmd /c);这将解决您的一些问题。

于 2011-01-10T16:18:41.193 回答