Tuesday, December 16, 2014

How to debug SQLCLR in VS 2012

Here is how to debug your SQLCLR code in Visual Studio.
I assume that you already have your SQLCLR DLL and its related stored procedure set up in your SQL Server.

  1. Launch VS 2012.
  2. Open "SQL Server Object Explorer" (can be opened from View -> SQL Server Object Explorer menu)
  3. In SQL Server Object Explorer, add connection to target SQL Server (ex: local SQL server) where SQLCLR is located. Local SQL instance will be best for debugging.
  4. Rightclick on the connection and choose [Allow SQL/CLR debugging].

  5. Find your stored procedure (or function) you want to debug. This SP should be entry point of the SQLCLR.
  6. Rightclick on the SP and choose "Debug Procedure"
  7. Enter input value in the "Debug Stored Procedure" dialog and click OK. This will open new SQLQuery window and automatically generate TSQL script to start debugging.
  8. In SQLQuery document window, choose [Execute With Debugger]. This will start debugging at the first line.

  9. Click [Step Into] button at the SP call that invokes SQLCLR call.
  10. Now you are in SQLCLR C# code (or any other .NET code) and you can step into, step out in the C# code.