从SQLCLR调用进程时,被调用的进程必须是64位的吗?

When calling process from SQLCLR, must the called process be 64bit?

假设我想从 Sql 服务器中的 SqlClr 存储过程 运行 调用 32 位可执行命令,使用类似于以下代码的代码:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Diagnostics;
using System.Text;

public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void ecommerce_export_cmd(SqlString directoryPath,
                                            SqlString execName,
                                            SqlString arguments)
    {
        // Make sure the file exists
        string fullPath = Path.Combine(directoryPath.Value, execName.Value);
        FileInfo fi = new FileInfo(fullPath);
        if (!fi.Exists)
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = string.Format(
                        "raiserror('File ->{0}<- is not found.',16,1)",
                          execName.Value);
                try { SqlContext.Pipe.ExecuteAndSend(cmd); }
                catch { return; }
            }
        }
        // ProcessStartInfo to run the DOS command attrib
        ProcessStartInfo pInfo = new ProcessStartInfo("cmd.exe");
        pInfo.WorkingDirectory = directoryPath.Value;
        pInfo.UseShellExecute = true;
        // quote the arguments in case it has spaces
        pInfo.Arguments = string.Format("{0} {1}", execName.Value, arguments.Value);
        // Start a new process and wait for it to exit
        Process process = new Process();
        process.StartInfo = pInfo;        
        process.Start();
        process.WaitForExit();
    }
}

考虑到 SqlServer 是一个 64 位进程,鉴于上面的代码,我应该 由于位数不同,是否会出现潜在问题?我当然不想 使 Sql 服务器崩溃。

我希望如果出现问题,会通过错误消息指出。一般来说,我认为你不需要担心这个,因为你启动的任何进程都不是 SQL 服务器的一部分(因此只有当程序集被标记为 UNSAFE 时才有可能)。

实际上,cmd.exe 是一个 64 位应用程序,您不需要执行 cmd.exe 在使用 UseShellExecute = true 时,您可以直接执行命令或应用程序。 CMD 是一个 shell,用 UseShellExecute = true 创建一个新进程已经在调用 cmd.exe.