从包含 xml 参数的 SQL CLR 函数调用 System.Diagnostics.Process.Start
我在 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,一切都可以正常工作。
I have a stored procedure in SQL Server 2008 CLR project. This function call a console application with a parameter that contain some 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();
}
}
The problem here with param that contain xml.
This is a expression for execute stored procedure:
declare @Result nvarchar(max)
exec ExecuteApp 'C:\Console.exe', 'send "<messages><message>my message</message></messages>"', @Result out
select @Result
After executing stored procedure I have got an error like:
< was unexpected at this time.
I want to noticed that without xml all is working properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 XML 包含由命令 shell 解释的特殊字符(
>
和<
)。您需要用
""
引用参数并转义其中的引号。另外,您应该直接执行您的程序(而不是通过
cmd /c
);这将解决您的一些问题。Your XML contains special characters which are interpreted by the command shell (
>
and<
).You need to quote the parameter and escape quotes inside of it with
""
.Also, you should execute your program directly (not through
cmd /c
); that will resolve some of your issues.