Execute Parameterized Stored Procedures in ADO.NET

Chia sẻ: Bui Tuan | Ngày: | Loại File: PDF | Số trang:4

0
62
lượt xem
5
download

Execute Parameterized Stored Procedures in ADO.NET

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Thực hiện tham số lưu trữ 4,3 Thủ tục trong ADO.NET Để tận dụng thủ tục lưu trữ cho toàn bộ sức mạnh của mình, bạn cần để có thể vượt qua các thông số để các tiêu chí cụ thể có thể được sử dụng.

Chủ đề:
Lưu

Nội dung Text: Execute Parameterized Stored Procedures in ADO.NET

  1. 4.3 Execute Parameterized Stored Procedures in ADO.NET To take advantage of stored procedures to their full power, you need to be able to pass parameters so that specific criteria can be used. This How-To describes how to create parameters off the OleDbCommand objects to pass parameters to SQL Server. You need to execute a parameterized stored procedure in your application. How do you do this using Visual Basic .NET and ADO.NET? Technique In ADO, you have a Command object to execute stored procedures, among other tasks. In ADO.NET, you also have a Command object that performs basically the same task. In fact, many of the properties and methods that you use are the same. You can see a list of those in Table 4.4. Table 4.4. Objects That Are Used for This Technique, with Properties and Methods Object Property Description/Method Connection ConnectionString Contains the connection string that is used. Connection Open Opens the connection that the Command object uses. Command cmdText Specifies the SQL statement to use. Can be SQL statement or names of objects such as tables or stored procedures. Command Connection Uses the Connection object. Command CommandType Specifies the type of command you want to execute. Can be one of the following command types: StoredProcedure, DirectTable, or Text. Command Parameters Parameters to pass to the stored procedure. Command ExecuteReader Creates a DataReader object with the data that the command object specifies. DataReader Read Reads the next record in the DataReader, and also tests the end of the data returned.
  2. DataReader GetString Returns the current record, getting the column specified, and returns it as string. DataReader GetInt32 Returns the current record, getting the column specified, and returns it as 32-bit integer. You will use these objects and their properties and methods for the following steps. Steps Open and run the VB.NET-Chapter 4 solution. From the main form, click on the command button with the caption How-To 4.3. When the form loads, click on the View button to display the orders for the customer ID that is specified. By default, this is ALKI. A TextBox control is then displayed on the bottom of the form. You can see the form in Figure 4.2. 1. Create a new Windows Form. 2. Add the following controls, setting the properties as listed in Table 4.5. Table 4.5. Controls Property Settings Object Property Setting Label Name Label1 Caption Products and Quantities Ordered By: TextBox Name txtCustID Text ALFKI Button Name btnView Text &View TextBox Name txtResults MultiLine True 3. Enter the following code to the Click event btnView. This code takes the connection and creates the command object. The name of the stored procedure is passed, and the command type is specified, which is CommandType.StoredProcedure. Next, parameters and the DataReader are
  3. created. The last task is to iterate through the data and add it to the display text box. 4. Private Sub btnView_Click(ByVal sender As System.Object, _ 5. ByVal e As System.EventArgs) Handles btnView.Click 6. 7. Dim ocnn As New OleDb.OleDbConnection(BuildCnnStr("(local)", 8. "Northwind")) 9. Dim ocmdCustHist As New OleDb.OleDbCommand("CustOrderHist", ocnn) 10. Dim odrCustHist As OleDb.OleDbDataReader 11. 12. Try 13. '-- Specify the name of the stored procedure 14. ocmdCustHist.CommandType = CommandType.StoredProcedure 15. 16. '-- Specify the parameters. 17. ocmdCustHist.Parameters.Add("@CustomerID", Me.txtCustID.Text) 18. 19. '-- Open the connection object. 20. ocnn.Open() 21. 22. '-- Establish the DataRead object. 23. odrCustHist = _ 24. ocmdCustHist.ExecuteReader(CommandBehavior.SequentialAccess) 25. 26. '-- Iterate through the data loaded, building the results string. 27. Do While odrCustHist.Read 28. Me.txtResults.Text &= odrCustHist.GetString(0) & _ 29. ", " & odrCustHist.GetInt32(1) & vbCrLf 30. Loop 31. 32. Catch excpData As Exception 33. MessageBox.Show("Error Occurred: " & excpData.Message) 34. 35. End Try 36. End Sub Figure 4.2. This form uses the Command object with a stored procedure to populate the TextBox control.
  4. How It Works When the user clicks on the View button with Customer ID filled in, the text box below is filled in, displaying order information for that customer. Comments Using the technique presented here, you can pretty well perform the majority of the tasks you need to by using Command objects and stored procedures.
Đồng bộ tài khoản