Professional Documents
Culture Documents
Using
Microsoft® Visual
Studio® 2008
Module 9: Accomplishing Complex Data Access Tasks
• Overview of Stored Procedures
Stored
Procedure
Web Form
Why Use Stored Procedures?
• Modular programming
• Distribution of work
• Database security
• Faster execution
• Flexibility
Lesson: Calling Stored Procedures
• Calling Stored Procedures
[Visual C#]
categorySqlAdapter.Fill(ds, "Categories");
[Visual Basic]
categorySqlAdapter.Fill(ds, "Categories")
Parameters in Stored Procedures
• Identify the available parameters:
Input
Output
InputOutput
ReturnValue
[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
[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
• Querying a Database
• 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
[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
Logon information
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
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
• Tools