3

I have tried different ways to get this done but nothing works. VS seems to ignore all my breakpoints in the .cs files when I run the test script. I have included a breakpoint in the test script as well but still its not debugging.

This is the guide from Microsoft https://msdn.microsoft.com/en-us/library/ms165051(v=vs.100).aspx which I tried, but I don't have "a Set as Default Debug Script" option when I right click on my Script file.

Solomon Rutzky
  • 46,688
  • 9
  • 128
  • 171
Rukshan Hassim
  • 505
  • 6
  • 15

2 Answers2

12

You might need to also:

  1. Run Visual Studio as an Admin
  2. Open up additional ports that are, by default, blocked by Windows Firewall
  3. Set the project as a Startup Project.
  4. In Visual Studio: go to "SQL Server Object Explorer", right-click on the Instance this is deploying to in the "Debug" tab of "Project Properties", and select "Allow SQL/CLR Debugging", and click the "Yes" button in the pop-up dialog.
  5. If you want breakpoints to work, you cannot have the "Optimize Code" option checked in the "SQLCLR Build" tab of "Project Properties". By default it is unchecked for the "Debug" configuration and checked for the "Release" configuration. The configuration itself doesn't matter, just so long as that option is not checked. If it was, you need to uncheck it and re-publish and make sure that it actually updates the Assembly (sometimes it might not since there are no other changes to the Assembly).
  6. In Visual Studio: Open a new query from "SQL Server Object Explorer" and in the drop-down attached to the Execute ">" button, select "Execute With Debugger" (which is also ALT + F5). Sometimes breakpoints aren't trapped the first time around, so just execute with debugger again.
  7. Please note: the login that you connect to SQL Server as for debugging needs to be a sysadmin.

There might be some additional things, but I am pretty sure that you do not need a default debug script, even though some posts about this say that you do.

Please see my answer here and the links in comments on the Question:

Can't attach to SQL Server to debug SQLCLR Stored Procedure

Solomon Rutzky
  • 46,688
  • 9
  • 128
  • 171
  • Thank you for the reply. I tried all what you mentioned but I'm still facing the same problem. – Rukshan Hassim Jul 26 '17 at 09:47
  • 1
    @RukshanHassim, See this thread which shared the detailed steps: https://stackoverflow.com/questions/14180009/unable-to-debug-net-code-could-not-attach-to-process-sql-clr-remote-debugging, it seems that you refer to the old VS2010 document. – Jack Zhai Jul 27 '17 at 08:44
  • @RukshanHassim I just added steps 4, 5, and 6. Please make sure that you are also following those and try again. If I do all 6 of these steps then it works (I am using VS 2015, if that matters, though I seem to recall that these steps worked on VS 2012 and/or VS 2013). – Solomon Rutzky Jul 28 '17 at 20:54
1

If you have Visual Studio and SQL Server in the same machine, simply attach the sqlservr.exe to Visual Studio debugger, and execute the stored procedure. It should hit the SP method.

Babu James
  • 2,740
  • 4
  • 33
  • 50