0

I'm working on a project which requires execution of C# method / function (coded on a web page) using a trigger or stored procedure in SQL Server.

I found this Execute-NET-Code-under-SQL-Server

But as stated on this link, I have to change the database property TRUSTWORTHY to ON.

Is there any way I can do this without changing database properties?

Thanks in advance

3
  • 3
    Almost certainly not. Commented Apr 24, 2015 at 15:25
  • You most definitely don't have to change this database property. You need it only when your assembly is unsigned and needs to access external resources Commented Apr 24, 2015 at 15:39
  • 1
    @PanagiotisKanavos Since OP need to somehow invoke external webpage (if I got his point correctly) - he need TRUSTWORTHY ON and assembly with EXTERNAL_ACCESS permission. The only alternative - is running external program, as I've stated in my answer. Commented Apr 24, 2015 at 15:44

2 Answers 2

1

Well, if you don't want (or can) use SQLCLR - you can create some standalone console application containing all c# code you need and then execute this application with some parameters using xp_cmdshell.

But note: xp_cmdshell is disabled by default, you have to enable it using

EXEC sp_configure 'xp_cmdshell', 1

Since xp_cmdshell is server option - you don't need to change database properties, but you have to change server options to enable it in the case it is disabled. Choose yourself - what to change. ;)

Sign up to request clarification or add additional context in comments.

1 Comment

This is far worse from a security viewpoint than changing a database option - xp_cmdshell can easily execute malicious scripts using SQL Server's service account
0

Do you need to execute the external code synchronously? In other words, must the external code start and run to completion before your trigger ends?

If the answer is yes, then I suggest changing your design. You're using triggers incorrectly and making whatever DML operation fires the trigger tremendously slow.

Assuming the answer is no then, you have options to queue up whatever work needs to be done and have that work executed asynchronously. For example, your trigger could create a record in a work-queue table when it needs this external code to run. Some other process (e.g. a scheduled task, SQL Agent, something in your application, SQL Server Broker) can monitor this table for new records and fire off the appropriate calls as needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.