Accessing Data With Visual Studio 2008
Accessing Data With Visual Studio 2008
• Connecting to a Database
• Accessing Data
DataSet
DataTable
DataTable
SqlDataAdapter
OleDbDataAdapter
DataSet DataReader
Disconnected Connected
Bind to multiple controls Bind to one control only
Forward and backward
Forward-only
scanning of data
Slower access Faster access
Supported by Visual
Manually coded
Studio 2008 tools
Accessing Data with ADO.NET
11
1. Client makes request
Database
222. Create the SqlConnection and SqlDataAdapter objects
5.
55 Client manipulates the data
6.
66 Update the DataSet
7.
77 Use the SqlDataAdapter to open
the SqlConnection, update the
database, and close the connection DataSet
List-Bound
List-Bound
Control
Control
Client
Client
Lesson: Connecting to a Database
• Generating a Connection by Using Server Explorer
• Generating a DataSet
DataSet
DataSet
DataAdapter
SelectCommand UpdateCommand InsertCommand DeleteCommand
DataReader
DataReader
Command
Command Command
Command Command
Command Command
Command
Connection
Connection
DataAdapter1.Fill(ds)
DataAdapter2.Fill(ds)
DataAdapter1.Fill(ds);
DataAdapter2.Fill(ds);
Creating a Connection Programmatically
• Using SqlConnection
Dim strConn As String = "data source=localhost; " & _
"initial catalog=northwind; integrated security=true"
Dim conn As New SqlConnection(strConn)
• Handling Errors
Accessing Data by Using the Visual Studio 2008 IDE
• To create a DataReader:
1
1
1. Create and open the database connection
2
2
2. Create a Command object
3
3
3. Create a DataReader from the Command object
4
4
4. Call the ExecuteReader method
5
5
5. Use the DataReader object
6
6
6. Close the DataReader object
7
7.
7 Close the Connection object
Reading Data from a DataReader
• Access a DataTable
Displaying DataSet Data in List-Bound Controls
• Set the properties
Property
Property Description
Description
DataSource
DataSource The
TheDataSet
DataSetcontaining
containingthe
thedata
data
DataMember
DataMember The
TheDataTable
DataTableininthe
theDataSet
DataSet
DataTextField
DataTextField The
Thefield
fieldininthe
theDataTable
DataTablethatthatisisdisplayed
displayed
The
Thefield
fieldininthe
theDataTable
DataTablethatthatbecomes
becomesthe the
DataValueField
DataValueField value
valueofofthe
theselected
selecteditem
itemininthe
thelist
list
DataAdapter1.Fill(ds)
lstEmployees.DataBind()
DataAdapter1.Fill(ds)
lstEmployees.DataBind();
Handling Errors
Code Example
Lesson: Accessing Multiple Tables
• Storing Data From Multiple Tables
• Creating Relationships
Customers
conn1 conn2
DataSet Orders
Creating Relationships
childCol
DataSet Orders table
Programmatically Navigating Between Tables
Using Relationships
ds.Tables(index).Rows(index).GetChildRows("relation")
ds.Tables(index).Rows(index).GetParentRow("relation")
ds.Tables[index].Rows[index].GetChildRows("relation");
ds.Tables[index].Rows[index].GetParentRow("relation");
Customers Orders
GetChildRows
GetParentRow
DataSet
Accessing Data with Microsoft ADO.NET and
Visual Studio 2008
• Exercise 1: Connecting to the Doctors Database
Logon information
Virtual machine 2310C_08
User name Student
Password Pa$$w0rd
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
XML
Doctors Dentists
Files
Lab Review
Module Review and Takeaways
Review Questions
• How do you create a connection to a database?