To debug a stored procedure in vb.net

Source: Internet
Author: User
Tags continue execution connect new features variables requires visual studio
Stored procedure debugging is a major part of writing applications. Visual Studio. NET provides a great deal of enhanced performance for your own debugger, including a unified debugging interface, WEB service debugging, and Cross-language debugging. One of the most useful new features of this debugger is embodied in the debugging performance of stored procedures. In this article I'll describe several options for debugging SQL Server 2000 stored procedures in Vs.net, and some configuration issues that you might encounter.

Many businesses use SQL Server as a database, and stored procedures as a mechanism for returning data to VB, so the ability to progressively debug stored procedures is the key to development. Transact-SQL (T-SQL) debugging is provided in Visual Basic 6, but it requires a number of steps to complete the configuration of the debugging process. In addition, the debugger is a separate program and is not as full-featured as the VB debugger. Vb. NET provides debugging for stored procedures, although the settings for remote debugging require some extra work, but the same kind of debugger you use in VB code. In addition to vb.net and SQL Server 2000, you can use other versions of VB and SQL Server to debug stored procedures. However, there may be many configurations in OS, VB, and SQL Server, and each of them has its own configuration problem, so I'll focus on SQL Server 2000 and vb.net.

All required files are placed in the right place to debug stored procedures (you can see a list of files that contain the debug method and its location in table 1). If you have SQL Server loaded in your machine, there should be no configuration problem with debugging stored procedures in this machine. You must ensure that the server and client configurations are correct for remote debugging of stored procedures, and that you must install the debug components on the server to ensure the proper operation of remote debugging.

You can use three ways to set up debugging components: You can install vs.net on the server, you can run Vs.net Setup and select Remote Components setup, which is used only to install files you need to debug, or you can manually copy and record those required files. If you do not choose to fully install a vs.net on the server, remote Components Setup will appear as a link in the first display of the installation vs.net (see Resources For more remote debugging setup issues).

After installing the remote debugging components, VS. NET upgrades the MSSDI98.dll file, but does not copy it to the SQL Server \binn directory, and you need to replicate it manually. Users who want to debug T-SQL also need to set execute permissions on the server-side sp_sdidebug. This is an extended stored procedure located in the master database, and only the system administrator has execute permission to access the stored procedure by default. If these permissions are set appropriately, the user can debug the stored procedure in any database on the server. If the debugger interrupts when it is correct, or if there is a problem during progressive debugging, you need to check the application Errors in the transaction log.

Debugging with SQL Explorer
SQL Explorer is part of the vs.net, which is used to connect and apply the database. To debug a stored procedure directly from SQL Explorer, you first have to establish a data connection with the database. Open SQL Explorer and select Data connections to connect to the Northwind database. Right-click and select Add Connection, enter the server name and registration information for SQL Server in the Data Link Properties window, and select Northwind as the default database.

After you have established the database connection, you need to expand SQL Server and find the sales by year stored procedures. When you expand the stored procedure, you will find that it uses two parameters: @Beginning_Date and @ending_date. The four items below the parameter are the columns returned by the stored procedure.

Right-click the stored procedure and select the Step into Stored procedure option, where a Run Stored Procedure dialog box appears, prompting you to enter two dates because the procedure contains parameters. The advantage of this screen is that it tells you the data type of each parameter and whether it is an input or output parameter. Enter 1/1/96 and 1/1/97 two values.

A yellow arrow appears on the left side of the first line of the stored procedure, which indicates that it is now in a debug state. From this point of view, debugging stored procedures and debugging VB code is the same. You can use the standard VB debugging keys to step through the code or continue running. As in VB code, you can use bookmarks and set breakpoints.

The Locals window is used to display the values of any local variables (here refers to @beginning_date and @ending_date). You can also check the values of variables in the command window. By selecting Debug | Windows found the debugging window in Vs.net. If a stored procedure executes correctly, this message appears in the Output window: "The program ' SQL Debugger:t-sql ' has exited with code 0 (0x0)." "By selecting database output in the combo box at the top of the Output window, you can see the execution result of the SELECT statement in the stored procedure."

A trigger (trigger) is the SQL code that fires when an insert, update, or DELETE statement is executed, because the triggers are in T-SQL code, so you can debug them, but you cannot debug them individually, but you must debug the stored procedures that trigger the triggers. And then step through the trigger. You can find triggers and stored procedures in the Call Stack window.

You can also debug a user-defined function (UDF) by progressively debugging a stored procedure that invokes it. As long as the stored procedure calls the function, the debugger will debug it gradually. You will find the stored procedure and UDF in the Call Stack window. A stored procedure with a yellow arrow next to it indicates that the current code is in a debug state. For example, you can create a stored procedure that uses a EmployeeID and returns a string of employee names and a comma-separated list of employee distributions (see Listing a). Call Ufn_getemployeeregionstring with the SELECT statement to return to the zone. As soon as the SELECT statement is executed, the debugger starts debugging the UDF code. You can find the stored procedure and function in the Call Stack window.

Debug SQL from your code
To debug a stored procedure called from a VB program, you can open the stored procedure in SQL Explorer and set a breakpoint in it. As soon as the program runs to the point where the breakpoint is set, the debugger starts debugging it. This is useful for a stored procedure that uses a large number of parameters, because it avoids the manual entry of each parameter.

You can step through debugging by using the Debug sample program that invokes the Usp_getemployee stored procedure. For example, you can view the code for the display Regions button in the sample program (see Listing 1 and download the sample program). When the ExecuteReader method of the command is invoked, you can begin to gradually debug the invoked stored procedure.

Click on the items in Solution Explorer to start debugging your code. Select Project | Properties, expand the Configuration Properties folder and click Debugging. View the SQL Server debugging checkbox at the bottom of the screen.

Go to SQL Explorer, view the Usp_getemployee in the Northwind database, and double-click enter edit mode. Set a breakpoint on one line on the set NOCOUNT. After calling the ExecuteReader method you will also need to set a breakpoint on this line in the VB code. It displays "Do While Dr." Read. "Here you need to set breakpoints, because the debugger will not return to the VB code that invokes the stored procedure for step-by-step debugging; It will continue to execute." Run the application, enter EmployeeID and click the Display Regions button. The debugger stops at the first breakpoint in the stored procedure.

Although the same vs.net debugger is used, it has some limitations when it comes to debugging stored procedures. For example, you cannot terminate execution in an SQL statement. Also, it cannot edit and continue execution. If you use the PRINT statement, the output will not appear in any debugger windows. Also, if SQL Server already caches the original values, the change in the debugger may not be reflected in the T-SQL code.

As you can see, VS. NET not only provides T-SQL debugging capabilities, but also provides a single debugging interface for all code. Unfortunately, running remote debugging requires a lot of configuration and testing work. However, the benefits of debugging stored procedures are entirely worth it.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.