【问题标题】:SQLCMD was not found when running Process from ASP .NET从 ASP .NET 运行进程时找不到 SQLCMD
【发布时间】:2015-12-03 16:57:47
【问题描述】:

我有一个将 sqlcmd.exe 作为进程启动的 ASP .NET 应用程序。在我们三个测试环境中的两个中,我们没有问题。但是,在第三台机器上,即使 sqlcmd.exe 与客户端连接工具一起安装,Process.exe 也找不到 sqlcmd.exe。显示的错误是:

Error running process: System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
       at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
       at L3_CNM.Utilities.Common.SqlcmdHelper.RunRemoteScript(String ServerAddress, String datbaseName, String filePath, String outputFilePath

我不知道为什么会发生这种情况。一切正常的情况与失败的情况之间的区别是:

1) 当它工作时,有一个完整的 SQL 服务器安装。当它失败时,我们只是将 sqlcmd 作为下载的安装包安装,它指向驻留在其他地方的 SQL 服务器。

2) 当它工作时,应用程序从与 Windows 安装相同的磁盘卷运行。在失败的机器上,应用程序安装在 Windows 安装的另一个卷上。

其他关键点 - PATH 变量显示 sqlcmd.exe 的位置,作为应用程序池身份的用户是本地系统管理员的一部分,当通过命令提示符运行 sqlcmd.exe 时,结果是预期的。

任何帮助将不胜感激。

谢谢

编辑:添加代码:

try
{
    Process process = new Process();
    process.StartInfo.FileName = "sqlcmd.exe";
    Logging.Instance.Log(Logging.Levels.Message, "Process start arguments: " + process.StartInfo.Arguments);
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    process.StartInfo.CreateNoWindow = true;
    if (process.Start())
        Logging.Instance.Log(Logging.Levels.Message, "Process successfully started");
    else
    {
        Logging.Instance.Log(Logging.Levels.Message, "Unable to start process");
    }
    string output = process.StandardOutput.ReadToEnd();
    output += process.StandardError.ReadToEnd();
    process.WaitForExit();
    Logging.Instance.Log(Logging.Levels.Message, "Process exited with output: " + output);
    return output;
}
catch (Exception e)
{
    Logging.Instance.Log(Logging.Levels.Error, "Error running process: " + e.ToString());
    return e.ToString();
}

【问题讨论】:

  • 我们也不知所措,没有看到您的代码,谁能准确地回答这个问题。这可能有多种原因。也许其中一个非工作环境的设置不完全像 2工作环境..有点像在黑暗中射击并期望每次都能击中靶心..请提供更有意义的信息或代码..你能展示你如何指向其他sql服务器吗..?
  • @MethodMan,我现在不关心 SQL 连接是否成功,我只是想达到 SQLCMD 执行的地步。我试图提供尽可能多的信息,但我已经用用于执行 SQLCMD 的代码更新了原始问题。
  • 对不起间距!感谢@MethodMan 修复它
  • 你的问题在这里process.StartInfo.FileName = "sqlcmd.exe"; 也许你可以在.config 文件中添加一个条目sqlcmd.exe 所在的位置.. 很明显你的Environment Variables 有问题为什么不你不是从工作机器和非工作机器复制过去的环境变量到文本编辑器并比较路径
  • 还有那台不工作的机器你试过让他们在添加环境变量后做一个简单的Reboot吗?只是好奇

标签: c# asp.net windows-server-2012 filenotfoundexception sqlcmd


【解决方案1】:

我猜这与 PATH 环境变量有关。

几个测试!一、打开命令窗口,输入

WHERE SQLCMD

这将在任何可以看到 SQLCMD 的地方打印出来。如果您一无所获,则需要更新您的路径。如果你得到一些东西,它可能是一个用户环境变量,而不是系统环境变量,这是 ASP.NET 将使用的。

您能否检查该变量的 SYSTEM 版本是否包含正确的文件夹?在win8及以上,打开开始菜单,输入“编辑系统环境变量”。然后点击“环境变量”,并确保在系统变量下,PATH包含WHERE SQLCMD返回的文件夹,如果没有,请将其放入,用分号与其他文件夹隔开。

【讨论】:

  • 我已经手动查看了 sqlcmd.exe 的位置,并验证了该文件夹位于 PATH 系统变量中,但我不会看到 WHERE SQLCMD 返回的内容并进行验证。谢谢!
【解决方案2】:

一个简单的重启。上帝我有时讨厌 Windows。

感谢@Methodman 的建议。

【讨论】:

    猜你喜欢
    • 2013-02-22
    • 1970-01-01
    • 2014-07-03
    • 2011-04-05
    • 1970-01-01
    • 2021-05-28
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多