CHAPTER 21 ADO.NET PART I: THE CONNECTED LAYER
Figure 21-7. Populating the Inventory table
Authoring the GetPetName() Stored Procedure
Later in this chapter, you will learn how to use ADO.NET to invoke stored procedures. As you might
already know, stored procedures are routines stored within a particular database that operate often on
table data to yield a return value. You will add a single stored procedure that will return an automobile’s
pet name, based on the supplied CarID value. To do so, right-click the Stored Procedures node of the
AutoLot database within the Server Explorer and select Add New Stored Procedure. Enter the following
in the editor that pops up:
CREATE PROCEDURE GetPetName
@carID int,
@petName char(10) output
AS
SELECT @petName = PetName from Inventory where CarID = @carID
Note Stored procedures do not have to return data using output parameters, as shown here; however, doing
things this way sets the stage for talking about the Direction property of the SqlParameter, which we will cover
later in this chapter.
When you save your procedure, it will automatically be named GetPetName, based on your CREATE
PROCEDURE statement (note that Visual Studio automatically changes the SQL Script to “ALTER
PROCEDURE...” as soon as you save it for the first time). After you do this, you should see your new stored
procedure within the Server Explorer (see Figure 21-8).
819