session 8: data management (with stored procedures)
DESCRIPTION
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 DatabaseTRANSCRIPT
Session 8: Data Management
(with Stored Procedures)
Data Management (Using Stored Procedures)
What Is a Stored Procedure? Why Use Stored Procedures? Calling Stored Procedures Using Parameters
Input Parameters
Output Parameters
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
StoredProcedure
Web Server
Database
Why Use Stored Procedures?
Modular programming Distribution of work Database security Faster execution Reduces network traffic Provides flexibility
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 = conndaCategory.SelectCommand.CommandText = "ProductCategoryList"daCategory.SelectCommand.CommandType = CommandType.StoredProcedure
daCategory.Fill(ds, "Categories")
Example: Calling a Stored Procedure
Using Parameters
Identify the available parameters Input Output InputOutput ReturnValue
Include parameters in the parameters collection
or Include parameter values in the command string
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);
ds = New DataSet();da.Fill(ds, "Products");ds = New DataSet()da.Fill(ds, "Products")
param = New SqlParameter _ ("@Beginning_Date", SQLDbType.DateTime)param.Direction = ParameterDirection.Inputparam.Value = CDate(txtStartDate.Text)da.SelectCommand.Parameters.Add(param)
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.Outputda.SelectCommand.Parameters.Add(param)
ds = new DataSet()da.Fill(ds)
iTotal = da.Parameters("@ItemCount").Value
Example: Passing Parameters