简体   繁体   English

从SQL触发器启动时,可执行文件无法写入文本文件

[英]Executable can't write in text file when launched from SQL trigger

I currently have a trigger named Writer set on my table DEF_TEST that, after an update or insert, runs an executable that writes to a text file. 当前,我在表DEF_TEST上设置了一个名为Writer的触发器,该触发器在更新或插入后运行写入文本文件的可执行文件。 It looks like this : 看起来像这样:

ALTER TRIGGER [dbo].[Writer] ON [dbo].[DEF_TEST]
AFTER INSERT, UPDATE
AS 
BEGIN
    BEGIN TRY
        DECLARE @cmd as varchar(500)
        SET @cmd = 'Path\Write_To_File.exe'
        EXEC dbo.RunCMD @cmd

        --Insert ok if no error are thrown
        INSERT INTO DEF_TEST(ErrorLine, ErrorMessage)
        VALUES('ok', 'ok')
    END TRY
    BEGIN CATCH
        INSERT INTO DEF_TEST(ErrorLine, ErrorMessage)
        VALUES(ERROR_LINE(), ERROR_MESSAGE())
    END CATCH
END

Write_To_file.exe is essentially this: Write_To_file.exe本质上是这样的:

Imports System.IO

Module Main
    Sub Main()
        Try
            File.WriteAllText(".\test.txt", "test", System.Text.Encoding.UTF8)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Module

This code works, because if I change the code of "Write_To_File.exe" to just print to a console, the trigger works perfectly. 该代码有效,因为如果我将“ Write_To_File.exe”的代码更改为仅打印到控制台,则触发器可以正常工作。 When I try to run "Write_To_File.exe" by double-clicking on it, it's able to create and write in "test.txt". 当我尝试通过双击运行“ Write_To_File.exe”时,它可以在“ test.txt”中创建和写入。 But when the trigger launches "Write_To_File.exe" and tries to write in "test.txt", it just does nothing. 但是,当触发器启动“ Write_To_File.exe”并尝试写入“ test.txt”时,它什么都不做。 No task launch in the task manager, no error thrown from the trigger. 任务管理器中没有任务启动,触发器没有引发错误。

What's weird is any code after trying to write in "test.txt" doesn't work. 奇怪的是,尝试写入“ test.txt”后的任何代码均无效。 For example, if I try to print to a console it doesn't work. 例如,如果我尝试打印到控制台,则无法正常工作。

I don't think the problem is with the file security as the SQL Server has total control on the entire folder. 我认为文件安全性不是问题,因为SQL Server可以完全控制整个文件夹。

Am I missing something obvious? 我是否缺少明显的东西? Is there a way to see what happens step by step when "Writer" triggers? 有没有办法查看“作家”触发时逐步发生的情况?

I found the problem! 我发现了问题!

I finally logged the .exe and he threw an exception, namely "Access to the path 'C:\\windows\\system32\\test.txt' is denied." 我终于登录了.exe,他抛出了一个异常,即“拒绝访问路径'C:\\ windows \\ system32 \\ test.txt'。” . It's because I was trying to write to ".\\test.txt", and because the sql server was executing the .exe from system 32 and not from the folder where the executable is, he was trying to write the test file in an unauthorized path (system32). 这是因为我试图写入“。\\ test.txt”,并且因为sql服务器是从系统32而不是从可执行文件所在的文件夹中执行.exe,所以他试图将测试文件写入未经授权的路径(system32)。 I just specified the exact path of the text file in my program (C:\\Path.....\\test.txt) and everything worked fine. 我只是在程序中指定了文本文件的确切路径(C:\\ Path ..... \\ test.txt),并且一切正常。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM