Chapter 10 - itt

Download Report

Transcript Chapter 10 - itt

Chapter 10
Working With Databases
Starting Out with Visual Basic .NET 2nd Edition
10.1
Introduction
Starting Out with Visual Basic .NET 2nd Edition
Chapter 10 Topics
• Basic database terminology
• Fundamental database concepts
• ADO .NET for accessing databases
Starting Out with Visual Basic .NET 2nd Edition
10.2
What Is a Database?
The Data in a Database Is Organized in
Tables, Rows, and Columns
Starting Out with Visual Basic .NET 2nd Edition
Terminology
• Database: a collection of tables which hold
related data
• Table: data organized in rows and columns
• Field: an individual piece of information
pertaining to an item
• Record: a complete set of data about a
single item (a row in a table)
Starting Out with Visual Basic .NET 2nd Edition
Database Table
Row
(Record)
Column
Field
Starting Out with Visual Basic .NET 2nd Edition
10.3
ADO .NET
ADO .NET Is a Database Technology That
Allows You to Connect Your Visual Basic .NET
Applications to a Variety of Data Sources
Starting Out with Visual Basic .NET 2nd Edition
Application
Database
1. Create a connection to a Data Source
2. Create a Data Adapter (Object with
Methods) to facilitate the transfer of data
from the data source to the application
3. Create a dataset to hold the data in
memory while the application works
with it
Starting Out with Visual Basic .NET 2nd Edition
Application
Database
Starting Out with Visual Basic .NET 2nd Edition
10.4
ADO .NET Introduction
In This Chapter You Develop an Application
That Performs Basic Operations on a
Database, Such As Adding, Changing, and
Deleting Records, and Searching for Data
Starting Out with Visual Basic .NET 2nd Edition
Data Aware Controls
• Data Aware Controls can automatically
display data from a database
• These controls have a DataBindings
property
• For example, a TextBox control can be
bound to a column
Starting Out with Visual Basic .NET 2nd Edition
Creating a New Empty Row
• First declare an object to reference the row
Dim ObjectVar As System.Data.DataRow
• Then create the row by referencing a
specific DataSet
ObjectVar = DataSet.Table.NewRow
Starting Out with Visual Basic .NET 2nd Edition
Populating the Columns of the New Row
• Use the Item method to assign a value to a
column
ObjectVar.Item(ColumnName) = NewValue
• For example:
newDataRow.Item("fldInvNumber") = newInvNumber
newDataRow.Item("fldTitle") = newTitle
newDataRow.Item("fldArtist") = newArtist
Starting Out with Visual Basic .NET 2nd Edition
Adding the New Row to a Table
Saving the Updated Table to the Data Source
• To add the new row to the table
DataSet.Table.Rows.Add(ObjectVar)
• Saving the updated table to the data source
DataAdapter.Update(DataSet)
Starting Out with Visual Basic .NET 2nd Edition
Removing Records
• The CurrencyManager has a RemoveAt
method that removes a specified row from
the table
ObjectVar.RemoveAt(Index)
Starting Out with Visual Basic .NET 2nd Edition
Accessing Fields within a Table
DataSet.Table(Index).Field
•
•
•
•
DataSet is the name of the dataset
Table is the name of a table
Index is a row number
Field is the name of a field
Starting Out with Visual Basic .NET 2nd Edition
Putting Database Values Into
a List Box or Combo Box
Dim i As Integer
With DsCompactDiscs1
For i = 0 To (.tblCompactDiscs.Count - 1)
lstRecords.Items.Add _
(.tblCompactDiscs(i).fldTitle)
Next
End With
Starting Out with Visual Basic .NET 2nd Edition
Putting Radio Button Information
Into a Database
If radDog.Checked = True Then
DsDataSet1.tblData(rowNum).fldPet
ElseIf radCat.Checked = True Then
DsDataSet1.tblData(rowNum).fldPet
ElseIf radBird.Checked = True Then
DsDataSet1.tblData(rowNum).fldPet
ElseIf radOther.Checked = True Then
DsDataSet1.tblData(rowNum).fldPet
ElseIf radNone.Checked = True Then
DsDataSet1.tblData(rowNum).fldPet
End If
daAdapter.Update(DsDataSet1)
= "Dog"
= "Cat"
= "Bird"
= "Other"
= "None"
Starting Out with Visual Basic .NET 2nd Edition