Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSDT SQL Server Debugging Doesn't Hit CLR Breakpoints

I applied the SQL Server Data Tools patch to Visual Studio 2012 (Premium) and created a SQL Server CLR user-defined function project in C#:

public partial class UserDefinedFunctions
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlInt32 Add42(SqlInt32 in_param)
    {
        SqlInt32 retval = in_param + 42;  // Set break point here.
        return retval;
    }
}

In the SQL Server Object Explorer pane, I right-click on the newly published UDF and select "Execute Function..." I am prompted to supply a sample input value, and Visual Studio then publishes the function (again) to my local 2012 SQL Server and generates a script that looks like this:

DECLARE    @return_value Int

EXEC    @return_value = [dbo].[Add42] @in_param = 5

SELECT    @return_value as 'Return Value'

GO

... and executes it, returning the expected result of 47.

If I now put a break point on an executable line in my CLR UDF C# code, right-click the UDF function in SQL Server Object Explorer, and this time select "Debug Function...", I land in a debugger for the generated SQL test script. I can step through the SQL statements to the end of the script, which returns the correct result, but the breakpoint in my C# code is never reached in the C# debugger.

The terminology for this feature seems misleading. To any programmer, "debugging" a function means stepping through the executable lines in the code of the function itself. Simply generating a SQL test harness that calls my compiled function and gets back the result is just "testing" the function. At most, the only thing being "debugged" is the tool-generated test itself, because you can't "Step Into" the CLR code. The only option is to "Step Over" it.

So how do I get Visual Studio to actually debug, and hit the breakpoint in my UDF C# code?

like image 815
Buggieboy Avatar asked Oct 29 '13 19:10

Buggieboy


People also ask

How do I debug CLR?

Right-click on a line of code in the text editor on the function or routine that you want to debug. Select Breakpoint, Insert Breakpoint. The breakpoint is added, highlighting the line of code in red. In the Debug menu, select Start Debugging to compile, deploy, and test the project.

What is CLR debugger in C#?

NET Framework to create a full-featured CLR debugger that allows you to set breakpoints, view call stacks, browse variables, view processes, enumerate threads, and perform other important debugging tasks.

How do I enable breakpoints in Visual Studio?

To set a breakpoint in source code, click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.


1 Answers

Okay, I finally figured this out. To debug SQL CLR code in VS 2012:

  1. Create a SQL test script that calls the UDF, sproc, or other CLR object. (You can do this by using the "Execute Function" or "Debug Function" options in the Server Object Explorer, as described in the question.)

  2. Save the generated script. (It will be called something like "SQLQuery1.sql" by default. You may wish to give it a more meaningful name.)

  3. In Solution Explorer, right-click the UDF (or other CLR type) project, and select "Properties".

  4. The project's properties tab will open. On the left, select the "Debug" category.

  5. In the "Start Action" subcategory of the Debug panel, select the "Startup script:" radio button. This will enable the associated dropdown so that you can specify the .sql script created in Step 1.

  6. Save all, toggle a breakpoint on an executable line of your C# or other .NET language code, and press the debug button.

NOTE: You may now get a dialog telling you that "Windows Firewall has blocked some features of this program." I checked the boxes to allow access to domain and private networks.

Proceeding now should cause your breakpoint to be reached.

like image 157
2 revs Avatar answered Oct 18 '22 15:10

2 revs