You are on page 1of 22

Developing Web Applications

Using
Microsoft® Visual
Studio® 2008
Module 9: Accomplishing Complex Data Access Tasks
• Overview of Stored Procedures

• Calling Stored Procedures

• Data Access by Using LINQ to SQL


Lesson: Overview of Stored Procedures
• What Is a Stored Procedure?

• Why Use Stored Procedures?


What Is a Stored Procedure?
• A common data procedure that can be called by many
Web applications
• Types of stored procedures:
 Return records
 Return value
 Perform action
Client Web Server SQL Server

Stored
Procedure

Web Form
Why Use Stored Procedures?
• Modular programming

• Distribution of work

• Database security

• Faster execution

• Reduced network traffic

• Flexibility
Lesson: Calling Stored Procedures
• Calling Stored Procedures

• Parameters in Stored Procedures

• Passing Input Parameters

• Accessing Output Parameters

• Demonstration: Passing Parameters


Calling Stored Procedures
• Identify the stored procedure

• Set up the SelectCommand property of the


DataAdapter
• Run the stored procedure and store returned records

[Visual C#]
categorySqlAdapter.Fill(ds, "Categories");

[Visual Basic]
categorySqlAdapter.Fill(ds, "Categories")
Parameters in Stored Procedures
• Identify the available parameters:
 Input
 Output
 InputOutput
 ReturnValue

• Select an option for the parameters:


 Include parameters in the parameters collection
 Include parameter values in the command string
Passing Input Parameters
• Create the parameter, set its direction and value,
and add it to the Parameters collection
• Run the stored procedure and store the returned
records

[Visual C#]
SqlParameter workParam1 = new SqlParameter
("@CategoryID", SqlDbType.Int);
workParam1.Direction = ParameterDirection.Input;
workParam1.Value =
Convert.ToInt16(categoriesTextBox.Text);

[Visual Basic]
Dim workParam1 As New SqlParameter _
("@CategoryID", SqlDbType.Int)
workParam1.Direction = ParameterDirection.Input
workParam1.Value = Cint(categoriesTextBox.Text)
Accessing Output Parameters
• Create the parameter, set its direction, and add it to
the Parameters collection
• Run the stored procedure and store the returned
records

• Read the output parameters

[Visual C#]
salesNumber = myCmd.Parameters["@ItemCount"].Value;

[Visual Basic]
salesNumber = _ myCmd.Parameters("@ItemCount").Value
Demonstration: Passing Parameters
• Call a stored procedure by using a DataAdapter

• Call a stored procedure by using ExecuteReader


Lesson: Data Access by Using LINQ to SQL
• What Is Language Integrated Query (LINQ)?

• Creating a LINQ to SQL Object Model

• The DataContext Class

• Querying a Database

• Calling a Stored Procedure

• Binding a Data Model by Using the LinqDataSource Control


What Is LINQ?
• Presents a programming model for querying data

• Provides unified syntax for querying any data source that


supports LINQ
• Enables queries to be written by using Visual C# or Visual
Basic
LINQ implementations
• LINQ to DataSet

• LINQ to Objects

• LINQ to SQL

• LINQ to XML
Creating a LINQ to SQL Object Model
Object Relational Designer (O/R Designer)
• Provides a visual design surface

• Creates an object model that maps to objects in a


database
• Maps stored procedures and functions to DataContext
methods
The DataContext Class
• The O/R Designer creates the class when you save
changes in the Designer

The DataContext class:


• Links a SQL Server database and the LINQ to SQL entity
classes mapped to that database
• Contains methods and properties that are used to connect
to a database and manipulate the data in the database
 Properties for each table added to the O/R Designer
 Methods for each stored procedure and function
Querying a Database
• Create an instance of the DataContext class

• Create a variable to hold the result of the query

• Write query syntax to specify the information to retrieve


from the database
• Execute the query:
 Deferred execution
 Immediate execution
Calling a Stored Procedure
• Create an instance of the DataContext class

• Call the appropriate method of the DataContext class


that maps to the stored procedure name
• Bind the results to a list-bound control

[Visual C#]
DoctorsDataContext context1 = new DoctorsDataContext();
GridView1.DataSource = context1.GetDoctorDetails("123");
GridView1.DataBind();

[Visual Basic]
Dim context1 As New DoctorsDataContext
GridView1.DataSource = context1.GetDoctorDetails("123")
GridView1.DataBind()
Binding a Data Model by Using the
LinqDataSource Control

11 Define the data model by using the O/R Designer

22 Add a LinqDataSource control to a Web page

Configure the data source of the LinqDataSource


33 control

Specify the entities within the DataContext


44 class that you want to use

Add a list-bound control such as a GridView to


55 the Web page

Set the data source of the list-bound control to


66 the LinqDataSource control
Lab: Accomplishing Complex Data Access Tasks
• Exercise 1: Get Unique City Names

• Exercise 2: Get Doctor Specialties

• Exercise 3: Get Doctor Specialties by Using LINQ to SQL

Logon information

Virtual machine 2310C-LON-DEV-09


User name Student
Password Pa$$w0rd

Estimated time: 45 minutes


Lab Scenario

Master Page
Logon Page benefitsMaster.master
login.aspx
Benefits Lab Web
Home Page Application
ASPState
Default.aspx Page Header
header.ascx
Menu Component
Registration Benefits.cs or Benefits.vb
register.aspx TempDB
Web.
config

Life Insurance Retirement Medical Dentists


life.aspx retirement.aspx medical.aspx dental.aspx

Prospectus
prospectus.aspx Doctors User Control XML Web
LINQ to SQL doctors.aspx nameDate.ascx Service
Classes DentalService1.asmx
Doctors.dbml

Doctors
Dentists
XML
Files
Lab Review
Review Questions
• What is the purpose of the getUniqueCities stored
procedure that you call in Exercise 1?
• What method can you use to fill a SqlDataReader object
from a SqlCommand object?
• What steps are required to call the getDrSpecialty stored
procedure by using LINQ to SQL?
Module Review and Takeaways
• Review Questions

• Real-World Issues and Scenarios

• Tools

You might also like