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.
- Launch VS 2012.
- Open "SQL Server Object Explorer" (can be opened from View -> SQL Server Object Explorer menu)
- 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.
- Rightclick on the connection and choose [Allow SQL/CLR debugging].
- Find your stored procedure (or function) you want to debug. This SP should be entry point of the SQLCLR.
- Rightclick on the SP and choose "Debug Procedure"
- 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.
- In SQLQuery document window, choose [Execute With Debugger]. This will start debugging at the first line.
- Click [Step Into] button at the SP call that invokes SQLCLR call.
- Now you are in SQLCLR C# code (or any other .NET code) and you can step into, step out in the C# code.