==== DEVELOPER .NET PERSPECTIVES ====
(contributed by Bill Sheldon, bills@interknowlogy.com)
* DEBUGGING THE DATABASE
This week, I want to discuss a subject that's near and dear to every developer's heart: debugging. In general, debugging in Visual Studio .NET is similar to debugging in Visual Studio (VS) 6.0--you use a point-and-click interface to set breakpoints in your code. Although this basic interface hasn't changed dramatically from VS 6.0, Visual Studio .NET has one new debugging feature with which most developers aren't familiar: the ability to debug stored procedures in Microsoft SQL Server projects.
Were you ever certain that your SQL Server code would work because you used the debugger to step through all the code, but then the code failed when you executed your statement against the database? Unfortunately, all you have at that point is an error code returned from a stored procedure that has several dozen embedded commands. With the .NET debugger, you can now debug stored procedures from Visual Studio .NET. Just as you can step from Visual Basic .NET code to Visual C# .NET code, you can step from Visual Basic .NET code into the T-SQL commands that make up your stored procedures.
To debug stored procedures, you need to take a few steps beyond having your project in Debug mode and clicking in the margin to add the red circle that indicates the placement of a breakpoint in the code. The first step is to enable the SQL Server debugger within the project's properties. In Solution Explorer, right-click the project name and select Property Pages to open the project's Property Pages dialog box. On the right side is a list box that contains the major categories (e.g., Common Properties, Configuration Properties) of the properties for the project. Select Configuration Properties. The debugging properties appear by default. The properties include a variety of settings for how to start the project and a list of the available debuggers. By default, the SQL Server debugger in this list isn't active. To enable SQL Server debugging, select SQL Debugging and save your selection by clicking OK.
At this point, you can debug stored procedures. However, unlike the code debugger, which lets you step directly into another function even in a different implementation language, the SQL Server debugger requires you to have a breakpoint in the procedure. To set this breakpoint, you need to use Server Explorer, which you can access by selecting the Server Explorer tab on the left side of the Visual Studio .NET window. Server Explorer lets you define new data connections. Right-click Data Connections and select Add Connection. A good sample connection to create is to the sample Northwind database that ships with SQL Server. So, in the Connection dialog box, select your SQL Server machine, define an account, then select the Northwind database. Click OK to exit.
After you've added a new connection to Server Explorer, expand that new entry to access the database's details. Server Explorer lets you expand the list of available tables, examine and create database diagrams, and carry out various actions related to managing your database. For this exercise, expand the Stored Procedures category and double-click the stored procedure associated with the sample database. The stored procedure's text will appear in Visual Studio .NET's main edit window.
Once the stored procedure is open in the Visual Studio .NET editor, click in the margin to add a breakpoint. Then, execute your application (i.e., a Windows form or ASP.NET Web form). When the application executes the stored procedure, the debugger will stop in the stored procedure at your breakpoint. At that point, you can begin stepping through the statements that make up your stored procedure and even examine the local SQL variables in the Command window.
Visual Studio .NET provides both a powerful code editor and an integrated debugging environment for application code and database logic. The Visual Studio .NET debugger contains several powerful capabilities, including making breakpoints occur conditionally or only after a given number of executions if the breakpoint is within a loop. As a .NET developer, you'll want to investigate all of the .NET debugging capabilities so that you can take full advantage of them.