Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Executing SQL Server Stored Procedures In Chapter 4, you saw how to create and execute SQL Server stored procedures using TSQL. You execute a stored procedure using the T-SQL EXECUTE statement. | Executing SQL Server Stored Procedures In Chapter 4 you saw how to create and execute SQL Server stored procedures using T-SQL. You execute a stored procedure using the T-SQL EXECUTE statement. In this section you ll see how to execute SQL Server procedures using ADO.NET. In Table 8.1 shown earlier in this chapter I mentioned the CommandType of StoredProcedure. Although you can use this CommandType to indicate that a command is to execute a stored procedure you re actually better off using the T-SQL EXECUTE command to execute a stored procedure. This is because you can read values that are returned from a stored procedure through a RETURN statement which you can t do when setting the CommandType to StoredProcedure. Also it s a lot easier to understand your code when you use the EXECUTE command. There are a couple of ways you can execute a stored procedure depending on whether your procedure returns a result set a result set is one or more rows retrieved from a table by a SELECT statement . You ll learn these two ways to execute a stored procedure next. Executing a Stored Procedure That Does Not Return a Result Set If your procedure does not return a result set then you use the following steps to execute it 1. Create a Command object and set its CommandText property to an EXECUTE statement containing your procedure call. 2. Add any required parameters for the procedure call to your Command object remembering to set the Direction property for any output parameters to ParameterDirection.Output. These output parameters can be defined using the T-SQL OUTPUT keyword in your procedure call or returned using a RETURN statement in your actual procedure. 3. Execute your Command object using the ExecuteNonQuery method. You use this method because the procedure doesn t return a result set. 4. Read the values of any output parameters. You ll see how to use these four steps to call the following two SQL Server stored procedures The first procedure AddProduct will return an .