Download presentation
Presentation is loading. Please wait.
Published byCarmel Lambert Modified over 9 years ago
1
Session 8: Data Management (with Stored Procedures)
2
Data Management (Using Stored Procedures) What Is a Stored Procedure? Why Use Stored Procedures? Calling Stored Procedures Using Parameters Input Parameters Output Parameters
3
What Is a Stored Procedure? A common data procedures that can be called by many Web applications Programmatic access to a database Return records Return value Perform action Client SQL Server Web Form Stored Procedure Web Server Database
4
Why Use Stored Procedures? Modular programming Distribution of work Database security Faster execution Reduces network traffic Provides flexibility
5
Calling Stored Procedures Identify the stored procedure Set up the SelectCommand property of the DataAdapter Run the stored procedure and store returned records Dim daCategory As New SqlDataAdapter() daCategory.SelectCommand = New SqlCommand() daCategory.SelectCommand.Connection = conn daCategory.SelectCommand.CommandText = "ProductCategoryList" daCategory.SelectCommand.CommandType = CommandType.StoredProcedure Dim daCategory As New SqlDataAdapter() daCategory.SelectCommand = New SqlCommand() daCategory.SelectCommand.Connection = conn daCategory.SelectCommand.CommandText = "ProductCategoryList" daCategory.SelectCommand.CommandType = CommandType.StoredProcedure daCategory.Fill(ds, "Categories")
6
Example: Calling a Stored Procedure
7
Using Parameters Identify the available parameters Input Output InputOutput ReturnValue Include parameters in the parameters collection or Include parameter values in the command string
8
Passing Input Parameters Create parameter, set direction and value, add to the Parameters collection Run stored procedure and store returned records SqlParameter param = new SqlParameter ("@Beginning_Date", SqlDbType.DateTime); param.Direction = ParameterDirection.Input; param.Value = Convert.ToDateTime (txtStartDate.Text); da.SelectCommand.Parameters.Add(param); SqlParameter param = new SqlParameter ("@Beginning_Date", SqlDbType.DateTime); param.Direction = ParameterDirection.Input; param.Value = Convert.ToDateTime (txtStartDate.Text); da.SelectCommand.Parameters.Add(param); ds = New DataSet(); da.Fill(ds, "Products"); ds = New DataSet(); da.Fill(ds, "Products"); ds = New DataSet() da.Fill(ds, "Products") ds = New DataSet() da.Fill(ds, "Products") param = New SqlParameter _ ("@Beginning_Date", SQLDbType.DateTime) param.Direction = ParameterDirection.Input param.Value = CDate(txtStartDate.Text) da.SelectCommand.Parameters.Add(param) param = New SqlParameter _ ("@Beginning_Date", SQLDbType.DateTime) param.Direction = ParameterDirection.Input param.Value = CDate(txtStartDate.Text) da.SelectCommand.Parameters.Add(param)
9
Using Output Parameters Create parameter, set direction, add to the Parameters collection Run stored procedure and store returned records Read output parameters param = New SqlParameter("@ItemCount", SQLDbType.Int) param.Direction = ParameterDirection.Output da.SelectCommand.Parameters.Add(param) param = New SqlParameter("@ItemCount", SQLDbType.Int) param.Direction = ParameterDirection.Output da.SelectCommand.Parameters.Add(param) ds = new DataSet() da.Fill(ds) ds = new DataSet() da.Fill(ds) iTotal = da.Parameters("@ItemCount").Value
10
Example: Passing Parameters
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.