C# 从 SQL Server 运行可执行文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11469457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:58:18  来源:igfitidea点击:

Run executable from SQL Server

c#sql-serversql-server-2008

提问by diyoda_

I have been stuck on a research on running an external exe file. What I have so far found was it can be done using this:

我一直在研究运行外部 exe 文件。到目前为止,我发现可以使用以下方法完成:

EXEC master..xp_cmdshell '"C:\New Folder\test.exe"'

And also this must not be directly called in a trigger because it has to wait until the execution is done to complete the trigger.

而且这不能在触发器中直接调用,因为它必须等到执行完成才能完成触发器。

So the encouraged approach for this is to have a scheduled job to poll for the table and call the .exe file from there to without creating any performance issues. So far I have accepted it and working on it.

因此,鼓励的方法是安排一个计划作业来轮询表并从那里调用 .exe 文件,而不会产生任何性能问题。到目前为止,我已经接受了它并致力于它。

So, before trying on this I am working on each part that has to be learned before implementing.I am testing the above piece of code keeping the database as master. I have tried several more.

所以,在尝试这个之前,我正在研究在实现之前必须学习的每个部分。我正在测试上面保持数据库为主的代码。我已经尝试了更多。

EXEC master..xp_cmdshell '"C:\New Folder\r.rar"'

EXEC master..xp_cmdshell '"C:\New Folder\text.text"'

So, I am thinking of this xp_cmdshell as a normal command prompt. I was expecting to be visible the exe file opening and opening of the tet file and the rar file. But its not working.

所以,我认为这个 xp_cmdshell 是一个普通的命令提示符。我希望可以看到 exe 文件的打开和 tet 文件和 rar 文件的打开。但它不起作用。

I have given the above details to tell my approach, Please give me a feed back if you have a better approach in your earlier experiences. Thanks in advance.

我已经给出了上面的细节来告诉我我的方法,如果您在之前的经验中有更好的方法,请反馈给我。提前致谢。

回答by Thirusanguraja Venkatesan

What type of error you getting?

你得到什么类型的错误?

You can get this problem because of component is turned off as part of the security configuration for your sql server.

您可能会遇到此问题,因为组件已作为 sql server 的安全配置的一部分关闭。

A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', see "Surface Area Configuration" in SQL Server Books Online.

系统管理员可以使用 sp_configure 启用“xp_cmdshell”。有关启用“xp_cmdshell”的详细信息,请参阅 SQL Server 联机丛书中的“表面区域配置”。

回答by Anuj Masand

Try Trigger

尝试触发器

Following is the code to run .exe file from a trigger:

以下是从触发器运行 .exe 文件的代码:

CREATE TRIGGER trgAfterInsert on dbo.table_Demo
DECLARE @CMDSQL VARCHAR(1000)

set path of your exe file and can include argument file if needed.

设置 exe 文件的路径,如果需要,可以包含参数文件。

SET @CMDSQL = 'cmd.exe /C "D:\Phoenix restart2 League\code\PhoenixMallTest\bin\Release\PhoenixMallTest.Console.exe" App.ini'
Exec master..xp_cmdshell @CMDSQL
PRINT 'AFTER INSERT trigger fired.'

If sql server blocks xp_cmdshell or says to enable xp_cmdshell, you can do as following.

如果 sql server 阻塞 xp_cmdshell 或说启用 xp_cmdshell,您可以执行以下操作。

Use Master
GO

EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO

EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
GO