Developing Web Applications Using Microsoft Visual Studio 2008
Developing Web Applications Using Microsoft Visual Studio 2008
Developing Web Applications Using Microsoft Visual Studio 2008
Applications Using
Microsoft® Visual
Studio® 2008
Module 8: Accessing Data with Microsoft ADO.NET and
Visual Studio 2008
• Overview of ADO.NET
• Connecting to a Database
• Accessing Data
DataTable DataTable
DataSet DataReader
Database
11 Client makes request
22 Create a SqlDataSource
Web
Web SqlDataSource
server
server
33 Return the data to the client
GridView
GridView
Control
Control
Client
Client
Lesson: Connecting to a Database
• Generating a Connection by Using Server Explorer
• Generating a DataSet
DataAdapter
SelectCommand UpdateCommand InsertCommand DeleteCommand
Command
Command Command
Command Command
Command Command
Command
Connection
Connection
[Visual C#]
string connectionString = "data source=localhost; " +
"initial catalog=northwind; integrated security=true";
SqlConnection connection = new SqlConnection(connectionString);
[Visual Basic]
Dim connectionString As String = "data source=localhost; " & _
"initial catalog=northwind; integrated security=true"
Dim connection As New SqlConnection(connectionString)
• Setting connection string parameters
Connection timeout
Data source
Password
Initial catalog
Persist security info
Integrated security
Provider
User ID
Lesson: Accessing Data
• Binding Data to Controls by Using the IDE
• Creating a DataReader
• Creating a DataSet
• Handling Errors
Binding Data to Controls by Using the IDE
• Add a GridView control to the Web Form
• SqlDataSource properties:
ConnectionString. The connection string to connect to the
database.
ProviderName. The database type.
• GridView properties:
Columns. The set of columns to be shown in the control
DataSourceID. The control ID of a data source
Creating a Command Object
• ExecuteReader. Returns a DataReader object
[Visual C#]
myCommand.Connection.Open();
SqlDataReader myDataReader = myCommand.ExecuteReader();
// Process the results.
myCommand.Connection.Close();
[Visual Basic]
myCommand.Connection.Open()
Dim myDataReader As SqlDataReader = myCommand.ExecuteReader()
' Process the results.
myCommand.Connection.Close()
Creating a DataReader
• Access a DataTable
Property Description
DataSource The DataSet containing the data
DataMember The DataTable in the DataSet
The field in the DataTable that is
DataTextField
displayed
The field in the DataTable that becomes
DataValueField
the value of the selected item in the list
[Visual C#]
catch (System.Data.SqlClient.SqlException ex1)
[Visual
{ Basic]
Catch ex1 As System.Data.SqlClient.SqlException
switch(ex1.Number)
Select
{ Case ex1.Number
...
...
Case
case 18452
18452:
errorsLabel.Text
errorsLabel.Text =
= errorsLabel.Text
errorsLabel.Text &
+ _
("Invaliduser
("Invalid username");
name")
... break;
End Select
...
End Try
}
}
Lesson: Accessing Multiple Tables
• Storing Data From Multiple Tables
• Creating Relationships
[Visual Basic]
C#]
customersDataAdapter = new SqlDataAdapter _
("select * from Customers", connection1)
connection1);
customersDataAdapter.Fill(myDataSet, "Customers")
"Customers");
Customers
connection1 Orders
connection2
DataSet
Creating Relationships
• Identify parent column
parentColumn Customers table DataRelation
• Identify child column
• Create DataRelation
childColumn
DataSet Orders table
[Visual Basic]
C#]
Dim cORelation
DataRelation As DataRelation
coDataRelation;
Dim parentColumn
DataColumn As DataColumn,
parentColumn, childColumn As DataColumn
childColumn;
parentColumn = _
myDataSet.Tables("Customers").Columns("CustomerID")
myDataSet.Tables["Customers"].Columns["CustomerID"];
childColumn = _
myDataSet.Tables("Orders").Columns("CustomerID")
myDataSet.Tables["Orders"].Columns["CustomerID"];
cODataRelation = New
new DataRelation("CustomerOrders"
DataRelation("CustomerOrders",_
, parentColumn,
parentColumn, childColumn)
childColumn);
myDataSet.Relations.Add(cODataRelation)
myDataSet.Relations.Add(cODataRelation);
Programmatically Navigating Between Tables by
Using Relationships
[Visual Basic]
ds.Tables(index).Rows(index).GetChildRows("relation")
ds.Tables(index).Rows(index).GetParentRow("relation")
[Visual C#]
ds.Tables[index].Rows[index].GetChildRows("relation");
ds.Tables[index].Rows[index].GetParentRow("relation");
Customers Orders
GetChildRows
GetParentRow
DataSet
Lab: Accessing Data with Microsoft ADO.NET and
Visual Studio 2008
• Exercise 1: Connecting to the Doctors Database
Logon information
Virtual machine 2310C-LON-DEV-08
User name Student
Password Pa$$w0rd
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
• How can you add a connection to a database?
• Best Practices