2310 b 11


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

2310 b 11

  1. 1. Module 11: Calling Stored Procedures with Microsoft ADO.NET
  2. 2. Overview <ul><li>Overview of Stored Procedures </li></ul><ul><li>Calling Stored Procedures </li></ul>
  3. 3. Lesson: Overview of Stored Procedures <ul><li>What Is a Stored Procedure? </li></ul><ul><li>Why Use Stored Procedures? </li></ul><ul><li>Practice: Select the Correct Stored Procedure </li></ul>
  4. 4. What Is a Stored Procedure? <ul><li>A common data procedures that can be called by many Web applications </li></ul><ul><li>Programmatic access to a database </li></ul><ul><ul><li>Return records </li></ul></ul><ul><ul><li>Return value </li></ul></ul><ul><ul><li>Perform action </li></ul></ul>Client SQL Server Web Form Stored Procedure Web Server Database
  5. 5. Why Use Stored Procedures? <ul><li>Modular programming </li></ul><ul><li>Distribution of work </li></ul><ul><li>Database security </li></ul><ul><li>Faster execution </li></ul><ul><li>Reduces network traffic </li></ul><ul><li>Provides flexibility </li></ul>
  6. 6. Practice: Select the Correct Stored Procedure <ul><li>Students will: </li></ul><ul><ul><li>Given scenarios, decide what type of stored procedure needs to be used </li></ul></ul><ul><li>Time: 5 Minutes </li></ul>
  7. 7. Lesson: Calling Stored Procedures <ul><li>Calling Stored Procedures </li></ul><ul><li>Demonstration: Calling a Stored Procedure </li></ul><ul><li>Practice: Displaying Data from a Stored Procedure </li></ul><ul><li>Using Parameters </li></ul><ul><li>Passing Input Parameters </li></ul><ul><li>Using Output Parameters </li></ul><ul><li>Demonstration: Passing Parameters </li></ul>
  8. 8. Calling Stored Procedures <ul><li>Identify the stored procedure </li></ul><ul><li>Set up the SelectCommand property of the DataAdapter </li></ul><ul><li>Run the stored procedure and store returned records </li></ul>Dim daCategory As New SqlDataAdapter() daCategory.SelectCommand = New SqlCommand() daCategory.SelectCommand.Connection = conn daCategory.SelectCommand.CommandText = &quot;ProductCategoryList&quot; daCategory.SelectCommand.CommandType = CommandType.StoredProcedure daCategory.Fill(ds, &quot;Categories&quot;) SqlDataAdapter daCategory = new SqlDataAdapter(); daCategory.SelectCommand = new SqlCommand(); daCategory.SelectCommand.Connection = conn; daCategory.SelectCommand.CommandText = &quot;ProductCategoryList&quot;; daCategory.SelectCommand.CommandType = CommandType.StoredProcedure; daCategory.Fill(ds, &quot;Categories&quot;);
  9. 9. Demonstration: Calling a Stored Procedure
  10. 10. Practice: Displaying Data from a Stored Procedure <ul><li>Students will: </li></ul><ul><ul><li>Drag a stored procedure from Server Explorer onto an ASP.NET Web form </li></ul></ul><ul><ul><li>Create a DataReader to read data From the stored procedure </li></ul></ul><ul><ul><li>Bind a DataGrid to the DataReader </li></ul></ul><ul><li>Time: 15 Minutes </li></ul>
  11. 11. Using Parameters <ul><li>Identify the available parameters </li></ul><ul><ul><li>Input </li></ul></ul><ul><ul><li>Output </li></ul></ul><ul><ul><li>InputOutput </li></ul></ul><ul><ul><li>ReturnValue </li></ul></ul><ul><li>Include parameters in the parameters collection </li></ul><ul><li>or </li></ul><ul><li>Include parameter values in the command string </li></ul>
  12. 12. Passing Input Parameters <ul><li>Create parameter, set direction and value, add to the Parameters collection </li></ul><ul><li>Run stored procedure and store returned records </li></ul>SqlParameter param = new SqlParameter (&quot;@Beginning_Date&quot;, SqlDbType.DateTime); param.Direction = ParameterDirection.Input; param.Value = Convert.ToDateTime (txtStartDate.Text); da.SelectCommand.Parameters.Add(param); ds = New DataSet(); da.Fill(ds, &quot;Products&quot;); Code Examples ds = New DataSet() da.Fill(ds, &quot;Products&quot;) param = New SqlParameter _ (&quot;@Beginning_Date&quot;, SQLDbType.DateTime) param.Direction = ParameterDirection.Input param.Value = CDate(txtStartDate.Text) da.SelectCommand.Parameters.Add(param)
  13. 13. Using Output Parameters <ul><li>Create parameter, set direction, add to the Parameters collection </li></ul><ul><li>Run stored procedure and store returned records </li></ul><ul><li>Read output parameters </li></ul>param = New SqlParameter(&quot;@ItemCount&quot;, SQLDbType.Int) param.Direction = ParameterDirection.Output da.SelectCommand.Parameters.Add(param) ds = new DataSet() da.Fill(ds) iTotal = da.Parameters(&quot;@ItemCount&quot;).Value param = new SqlParameter(&quot;@ItemCount&quot;, SqlDbType.Int); param.Direction = ParameterDirection.Output; da.SelectCommand.Parameters.Add(param); ds = new DataSet(); da.Fill(ds); iTotal = da.Parameters(&quot;@ItemCount&quot;).Value;
  14. 14. Demonstration: Passing Parameters
  15. 15. Review <ul><li>Overview of Stored Procedures </li></ul><ul><li>Calling Stored Procedures </li></ul>
  16. 16. Lab 11: Calling Stored Procedures with Microsoft ADO.NET Medical Medical.aspx Benefits Home Page Default.aspx Life Insurance Life.aspx Retirement Retirement.aspx Dental Dental.aspx Dentists Doctors Doctors.aspx Doctors Logon Page Login.aspx Registration Register.aspx Coho Winery Prospectus Prospectus.aspx XML Web Service dentalService1.asmx Page Header Header.ascx ASPState tempdb Lab Web Application User Control namedate.ascx Menu Component Class1.vb or Class1.cs XML Files Web. config