首页 > 解决方案 > SQL CLR 调用 Process.Start 挂起或失败

问题描述

我已经检查过,有很多类似的帖子,但我无法解决这个问题。这是一个 SQL CLR proc,用于运行解析 PDF 文件的 exe。我可以在控制台应用程序中运行与我自己相同的代码。权限不是问题。我正在转储到事件日志,并且 ProcessStartInfo 已完整形成。UseShellExecute - 我不确定。我还创建了一个 .bat 文件来执行代码 - 手动运行它 - 它运行良好。通过这个 CLR 程序?立即挂起或奔跑,但什么也不做。斗智斗勇。我的目标是使用 itext 中的 Nuget 创建 CLR,但我很难安装。SQL CLR 不支持它,但我还是尝试了。

提前致谢。

[Microsoft.SqlServer.Server.SqlProcedure]
    public static void PDFToCSVViaExe_CLR()
    {

        ProcessStartInfo startInfo = new ProcessStartInfo();
        string args = @"\\app\Systems\Universal";
        args = " \"" + args + "\"" + " \"" + "*" + "\"" + " \"" + "LVM" + "\"";
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = true;
        startInfo.FileName = "c:\\SqlCLR\\PDFRipper\\PDFToCSV.exe";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.Arguments = args;
        EventLog.WriteEntry(".NET Runtime", startInfo.FileName.ToString()+" " + args, EventLogEntryType.Warning, 1000);

        string folder = "C:\\SqlCLR\\PDFRipper\\LVM.bat";
        try
        {
            //using (new ImpersonationNamespace.Impersonation("domain", "user", "password"))
            {
                EventLog.WriteEntry(".NET Runtime", System.Security.Principal.WindowsIdentity.GetCurrent().Name + " OR " + Environment.UserName, EventLogEntryType.Warning, 1000);
                folder = System.IO.Path.GetDirectoryName(folder);
                if (!Directory.Exists(folder))
                {
                    throw (new Exception("Directory does not exist for " + folder));
                }
                else
                { EventLog.WriteEntry(".NET Runtime", folder + " exists.", EventLogEntryType.Warning, 1000); }
                // Start the process with the info we specified.
                // Call WaitForExit and then the using statement will close.
                using (Process exeProcess = Process.Start(startInfo))
                {
                    exeProcess.WaitForExit();
                }

            }

        }
        catch (Exception ex)
        {
            using (EventLog EventLog = new EventLog("Application"))
            {
                EventLog.Source = "Application";
                //eventLog.WriteEntry(ex.Message);
                EventLog.WriteEntry(".NET Runtime", ex.Message + "Trace" + ex.StackTrace, EventLogEntryType.Warning, 1000);
                //eventLog.WriteEntry(".NET Runtime", ex.Message, EventLogEntryType.Warning, 1000);
            }

        }

标签: c#sqlclrprocess.start

解决方案


推荐阅读