You are on page 1of 23

Accomplishing Complex

Data Access Tasks


Module 9: Accomplishing Complex Data Access
Tasks
• Overview of Stored Procedures

• Calling Stored Procedures

• Data Access with 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
• Programmatic access to a database
 Return records
 Return value
 Perform action

Stored
Stored
Procedure
Procedure
Web Form

Client Database
Web
WebServer
Server SQL
SQLServer
Server
Why Use Stored Procedures?
• Modular programming

• Distribution of work

• Database security

• Faster execution

• Reduces network traffic

• Provides 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

daCategory.Fill(ds, "Categories")

daCategory.Fill(ds, "Categories");
Parameters in Stored Procedures
• 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
Accessing Output Parameters

• Create parameter, set direction, add to the


Parameters collection
• Run stored procedure and store returned records

• Read output parameters

iTotal = da.Parameters("@ItemCount").Value

iTotal = da.Parameters("@ItemCount").Value;
Demonstration: Passing Parameters
Lesson: Data Access with 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

• Enables queries to be written by using Visual Basic or C#

LINQ Implementations
• LINQ to SQL

• LINQ to XML

• LINQ to DataSet
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
• LINQ to SQL class created by the O/R Designer

• Links a SQL Server database and the LINQ to SQL entity


classes mapped to that database
• Contains methods and properties for connecting to a
database and manipulating 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
Query Execution
• Deferred

• Immediate
Calling a Stored Procedure
• Create an instance of the DataContext class

• Create a variable to hold the results of the stored


procedure
• Call the appropriate method of the DataContext class that
maps to the stored procedure name
Binding a Data Model by Using the
LinqDataSource Control
• Enables the use of LINQ in an ASP.NET Web page

• Provides a ContextTypeName property that maps to the


DataContext class
• Provides properties to restrict and manipulate the data
returned
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_09
User name Student
Password Pa$$w0rd

Estimated time: xx minutes


Lab Scenario

Logon Page
Login.aspx
Benefits
Coho Home Page Page Header ASPState
Winery Default.aspx Header.ascx
Menu
Registration Component
Register.aspx Class1.vb or Class1.cs Web.
tempdb
config

Life Insurance Retirement Medical Dental


Life.aspx Retirement.aspx Medical.aspx Dental.aspx

Prospectus Doctors User Control XML Web


Lab Web Prospectus.aspx Doctors.aspx namedate.ascx Service
Application dentalService1.asmx

XML
Doctors Dentists
Files
Lab Review
Module Review and Takeaways
Review Questions
• What are the three steps in calling a stored procedure?

• What method of the Command object do you use when


calling a return value stored procedure?
• What are the four types of stored procedure parameters?
Review for Alpha
• Is there any topic or specific content item in the module
that seemed unclear or unnecessary?
• Is there any content item/related subject area that was
not covered and could be included?
• Did you observe any issues with the technical accuracy of
the content?
• Is the content in the module presented in a manner that
encourages learning? Did the flow of topics seem right?
• Does the lab outline indicate the expected scope of tasks
to be covered? Would you like to suggest any tasks that
could be removed or added?

You might also like