Debugging SQL Server CLR Function In Visual Studio 2015

This is a step-by-step instruction of how to debug CLR code in Visual Studio. It is based on MSDN article Debugging CLR Database Objects. As stated in the comment there, the process can differ for different versions of Visual Studio. I’ve got Visual Studio 2015 Community Edition with Update 3.

1) Run Visual Studio with Administrator rights. It’s needed for VS to attach the SQL Server process.
If you skip this, you might see an error later:
Unable to debug .NET code. Could not attach to SQL Server process on 'VS2015'. Operation not supported. Unknown error: 0x80004005.
2) Open SQL Server Object Explorer in Visual Studio.
3) Right-click server and select Allow SQL/CLR Debugging.
4) Drill down to CLR function, right-click and select Debug Function…
5) In the Debug Function window enter value for input parameter and press OK.
6) Visual Studio will generate a test script and enter into the debug mode.
7) Now you can press F11 to step into the test script code and CLR function.

During the last step Windows Firewall could ask you to allow VS debugger to connect through network. Please allow it.

Also Visual Studio will always ask you to attach to SQL Server process in order to debug CLR code. Press Attach here.

3 Replies to “Debugging SQL Server CLR Function In Visual Studio 2015”

  1. Thanks!

    I didn’t bother using “Debug Function” (or similar) and instead (a) manually attached Visual Studio to the SQL Server process; (b) created a new query to run the same code I was running via SSMS; and (c) ran the new query with the *Execute with Debugger* option.

    I’m also (still) using Visual Studio 2012.

Leave a Reply

Your email address will not be published. Required fields are marked *