overview compsci 111 111g - auckland · s2 2008 compsci 111/111g - lecture 20 2 ado .net ... s2...
TRANSCRIPT
COMPSCI 111 / 111GM t i C bMastering Cyberspace:
An introduction to practical computing
ADO .NET
S2 2008 1COMPSCI 111/111G - Lecture 20
Outline• Introduction• ADO NET Overview• ADO.NET Overview• Data Providers• DataSets• DataSets• Examples
Birthdays Reminder– Birthdays Reminder– Course Administration
• DemonstrationsDemonstrations– Demo 1: Customers– Demo 2: Customers‐Orders
S2 2008 COMPSCI 111/111G - Lecture 20 2
ADO .NET• is a set of computer software components that can be used by programmers to accessthat can be used by programmers to access data
i f h b l lib h i• is a part of the base class library that is included with the Microsoft .NET Framework.
• is commonly used to access and modify data stored in relational database systems
S2 2008 COMPSCI 111/111G - Lecture 20 3
Overview• ADO.NET consists of two primary parts
D t P id– Data Provider
– DataSetData SourceData SourceDataSetDataSetDataSetDataSet
DataTableDataTableDataTableDataTableDataColumnDataColumn .Net Data Provider.Net Data Provider C
oC
o
Co
Co
DataConstraintDataConstraint
DataRowDataRow
DataRelationCollectionDataRelationCollectionDataRelationCollectionDataRelationCollection
DataAdapterDataAdapterSelectCommandSelectCommandUpdateCommandUpdateCommandInsertCommand
omm
andom
mand
onnectiononnection
DataRelationCollectionDataRelationCollectionDataRelationCollectionDataRelationCollection InsertCommandDeleteCommandDeleteCommand
DataReaderDataReader
S2 2008 COMPSCI 111/111G - Lecture 20 4
DataReaderDataReader
ADO.NET Architecture ‐ Context
ADO.NET Consumer
D t S t
Other .NET Data ProviderOLE DB Data Provider
DataSet
Other .NET Data Provider
ODBC/SQL File
OLE DB Data Provider
Other Data Source
SQL Server Data Provider
SQL Server DBS2 2008 5COMPSCI 111/111G - Lecture 20
Data Provider• These classes provide access to a data source
– such as a Microsoft SQL Server or Oracle database QAccess Database, and OLEDB data provider
– Each data source has its own set of provider objects, but they each have a common set of utility classes:but they each have a common set of utility classes:
• Connection– Provides a connection used to communicate with the data source
– CommandCommand• Used to perform some action on the data source, such as reading, updating, or deleting relational data.
– DataAdapter– DataAdapter• A bridge used to transfer data between a data source and a DataSet object
S2 2008 COMPSCI 111/111G - Lecture 20 6
DataSets• DataSets objects, a group of classes describing a simple in‐memory relational databasep y– Disconnected from data source– Contain tables and relationships between those tablestables.
• A DataTable object represents a single table in the database. It has a name, rows, and columns.
– A DataColumn represents a column of the table, including itsA DataColumn represents a column of the table, including its name and type.
– A DataRow object represents a single row in the table, and allows reading and updating of the values in that row
• A DataRelation is a relationship between tables• A DataRelation is a relationship between tables
S2 2008 COMPSCI 111/111G - Lecture 20 7
Example: Birthday Reminderp y• Reminds you of upcoming birthdays on startup• Calendar View: Birthdays in BOLDy• Normal View: Arrange birthdays in Group
S2 2008 COMPSCI 111/111G - Lecture 20 8
Example – Course Administrationp• Organizes Marks• Sends Emails
UPI Lists
• Calculates Final Grade Details
Final dgrade
S2 2008 COMPSCI 111/111G - Lecture 20 9
Demo 1• Create a new Visual Studio Project
dd S j• Add a Data Source to a Project
• Add a Data‐Bound Control to a Form
• Run the project
S2 2008 COMPSCI 111/111G - Lecture 20 10
Create a New Projectj• Start Visual Studio
– Choose All Programs | Compsci | Microsoft Visual Studio | f2008 | Microsoft Visual Studio 2008
• Create a new Project– File | New ProjectFile | New Project– Choose Visual Basic/Visual C# in Project types– Select Windows Forms Application in Templates
The entire project is saved in Documents | Visual Studio
S2 2008 COMPSCI 111/111G - Lecture 20 11
|2008 | Projects folder
Add a Data Source• Choose Data | Add New Data Source menu commandcommand
• Choose the data connection– Choose Database– Click New Connection– Select Microsoft Access Database File (OLE DB)– Select Northwind.mdb
• Choose the Database objects– Select Customers table
• Demo: 01AddDataSource.wmv
S2 2008 COMPSCI 111/111G - Lecture 20 12
Add a Data‐Bound Control• Open the Data Source Window
h C bl f h• Drag the Customers table from the Data Source window onto the form
• Demo: 02AddDataGridViewControl.wmv
S2 2008 COMPSCI 111/111G - Lecture 20 13
Run the projectp j• Choose Debug | Start Debugging
S2 2008 COMPSCI 111/111G - Lecture 20 14
Demo 2• Customers and Orders table( One to many)
C li d i h l ( l )• Customers are listed in the top panel (Panel1)
• Orders of the selected Customer are listed in the bottom panel (Panel2)
S2 2008 COMPSCI 111/111G - Lecture 20 15
Stepsp• Create a New Project• Add a Data Source to the projectp j
– Select both Customers and Orders table• Drag a SplitContainer from Toolbox | Containers tab
Set Orientation to Horizontal– Set Orientation to Horizontal• Drag tables on to the form
– Drag the Customers table onto the Panel1 of splitContainer1
– Drag the Customers‐Orders table onto the Panel2 of SplitContainer1
• Choose Debug | Start Debugging
S2 2008 COMPSCI 111/111G - Lecture 20 16