View
231
Download
8
Category
Tags:
Preview:
Citation preview
Module 7: Accessing Data by Using ADO.NET
Module Overview
Overview of Data Access
Reading and Writing Relational Data
Reading and Writing XML Data
Lesson 1: Overview of Data Access
ADO.NET Object Model
XML Data Access
Data Access Namespaces
Choosing a .NET Framework Data Provider
What Are Connected and Disconnected Operations?
Accessing Data by Using the Visual Studio 2005 IDE
Binding Data to Controls
Demonstration: Accessing Data by Using the Visual Studio 2005 IDE
ADO.NET Object Model
ADO.NETADO.NET
Connected model vs. disconnected modelConnected model vs. disconnected model
Data provider interfacesData provider interfaces
Data provider-specific classesData provider-specific classes
SqlConnection
SqlCommand
SqlDataReader
SqlDataAdapter
SqlConnection
SqlCommand
SqlDataReader
SqlDataAdapter
Data provider-independent classesData provider-independent classes
DataSet
DataTable
DataSet
DataTable
XML Data Access
XmlReaderXmlReader11
XmlWriterXmlWriter22
DOMDOM33 XmlDocument
XmlElement
Data Access Namespaces
Namespace Description
System.Data Core ADO.NET architecture
System.Data.Common Shared classes
System.Data.OracleClient Classes for Oracle
System.Data.OleDb Classes for OLE-DB-compatible data sources
System.Data.Odbc Classes for ODBC data sources
System.Data.Sql Core SQL Server classes
System.Data.SqlClientClasses for SQL Server version 7.0 and later
data sources
System.Xml Classes for XML data sources
…
Choosing a .NET Framework Data Provider
Data Provider Database
.NET Framework Data Provider for Microsoft SQL Server
SQL Server 7.0 and later
Microsoft Data Engine (MSDE)
.NET Framework Data Provider for OLE DB
SQL Server (SQLOLEDB)
Oracle (MSDAORA)
Microsoft Jet (Microsoft.Jet.OLEDB.4.0)
.NET Framework Data Provider for ODBC
SQL Server
Microsoft ODBC for Oracle
Microsoft Access Driver (*.mdb)
.NET Framework Data Provider for Oracle Oracle client software version 8.1.7 and later
What Are Connected and Disconnected Operations?
.NET application.NET application
Connected model Disconnected model
Open connection
Run commands
Retrieve results
Close connection
Create DataSet
Manipulate data
AdventureWorks Database
.NET application.NET application
AdventureWorks Database
Open connection
Close connection
Open connection
Close connection
Update tables
DataSet
Accessing Data by Using the Visual Studio 2005 IDE
On the Data menu, click Add New Data Source On the Data menu, click Add New Data Source 11
Define a new database connection Define a new database connection 22
Save the connection string in the app.config fileSave the connection string in the app.config file33
Specify the tables you want in the DataSetSpecify the tables you want in the DataSet44
Specify a name for the DataSetSpecify a name for the DataSet55
Binding Data to Controls
Add a control to a Windows form Add a control to a Windows form 11
Choose the data source for the control Choose the data source for the control 22
Add queries to the table adapter, if necessaryAdd queries to the table adapter, if necessary33
Write code to fill the table in the DataSetWrite code to fill the table in the DataSet44
Write code to save the table in the DataSetWrite code to save the table in the DataSet55
Demonstration: Accessing Data by Using the Visual Studio 2005 IDE
Create a new data source
Add a DataGridView control and set properties
Bind the DataGridView control to a data source
Update the data source query
Add code to save changes to the data on the FormClosed event
Build and run the application
Lab: Accessing Data by Using a DataGridView Control
Exercise 1: Binding Data to Controls in a Form (15 minutes)
Lesson 2: Reading and Writing Relational Data
Connecting to a Data Source
Creating and Running ADO.NET Commands
How to Retrieve Data by Using a Data Reader
What Is a Data Adapter?
How to Fill and Access DataSet Data
How to Save a DataSet to a Database
Guidelines for Secure Coding with ADO.NET
Connecting to a Data Source
.NET Application.NET Application
2. Create a Connection object
3. Call the Open methodto open the connection
4. Call the Close methodto close the connection
DatabaseDatabase1. Import namespaces
Creating and Running ADO.NET Commands
Method Description
ExecuteScalar Executes a query and returns a single result
ExecuteReader Executes a query and retrieves rows from a data source
ExecuteNonQuery Executes a query that performs an action in the database,
and returns the number of rows affected
How to Retrieve Data by Using a Data Reader
Create a connection object Create a connection object 11
Create a command object and specify an SQL query Create a command object and specify an SQL query 22
Open the database connectionOpen the database connection33
Invoke the ExecuteReader method and obtain a data reader objectInvoke the ExecuteReader method and obtain a data reader object44
Use the data reader object to iterate through the result setUse the data reader object to iterate through the result set55
Close the database connectionClose the database connection66
.NET Application.NET Application
Database
What Is a Data Adapter?
DatabaseDataSet
DataAdapter
Fill MethodFill Method
Update MethodUpdate Method
DataTable Object
Invoke Fill method to populate DataSetInvoke Fill method to populate DataSet11
Invoke Update method to merge changesInvoke Update method to merge changes22
Import required namespacesImport required namespaces11
Create a SqlDataAdapter objectCreate a SqlDataAdapter object22
Set the SelectCommand property to an SQL querySet the SelectCommand property to an SQL query33
Create a DataSet objectCreate a DataSet object44
Open the database connectionOpen the database connection55
Invoke the Fill methodInvoke the Fill method66
Close the database connectionClose the database connection77
How to Fill and Access DataSet Data
How to Save a DataSet to a Database
Import required namespacesImport required namespaces
Create a SqlDataAdapter objectCreate a SqlDataAdapter object
Set the command property to an SQL querySet the command property to an SQL query
Create a DataSet objectCreate a DataSet object
Open the database connectionOpen the database connection
Invoke the update methodInvoke the update method
Close the database connectionClose the database connection
11
22
33
44
55
66
77
Do not echo user security credentials when opening a connectionDo not echo user security credentials when opening a connection
Use Windows authentication if possibleUse Windows authentication if possible
Store connection strings in the application configuration fileStore connection strings in the application configuration file
Encrypt connection stringsEncrypt connection strings
Validate user inputValidate user input
Prevent SQL injection attacksPrevent SQL injection attacks
. . .. . .
Guidelines for Secure Coding with ADO.NET
Stored proceduresStored procedures
Lab: Accessing Data by Using ADO.NET Commands
Exercise 1: Creating and Running Simple Commands (30 minutes)
Exercise 2: (OPTIONAL) Creating and Running Query Commands (30 minutes)
Lesson 3: Reading and Writing XML Data
How to Read and Write a DataSet as XML Data
How to Read XML Data by Using XmlReader
How to Write XML Data by Using XmlWriter
How to Process XML Data by Using the Document Object Model
How to Read and Write a DataSet as XML Data
Save a DataSet as XML
DiffGram
IgnoreSchema
WriteSchema
Load XML into a DataSet
Auto
DiffGram
Fragment
IgnoreSchema
InferSchema
InferTypeSchema
ReadSchema
DataSet
How to Read XML Data by Using XmlReader
Create an XmlReaderSettings object Create an XmlReaderSettings object 11
Create an XmlReader object Create an XmlReader object 22
Iterate through the nodes in the XML document Iterate through the nodes in the XML document 33
Retrieve the value of nodes Retrieve the value of nodes 44
Close the document Close the document 55
How to Write XML Data by Using XmlWriter
Create an XmlWriterSettings object Create an XmlWriterSettings object 11
Create an XmlWriter object Create an XmlWriter object 22
Write the start of the XML documentWrite the start of the XML document33
Write nodes to the XML document Write nodes to the XML document 44
Write the end of the XML document Write the end of the XML document 55
How to Process XML Data by Using the Document Object Model
Read the content of XML data Read the content of XML data
Move randomly around the XML document Move randomly around the XML document
Add, modify, clone, and delete content in the XML document Add, modify, clone, and delete content in the XML document
Create a new XML document Create a new XML document
Operations:
DOM:
A standard API for reading and writing XML data A standard API for reading and writing XML data
Node types include: root, element, child element, and attributeNode types include: root, element, child element, and attribute
Lab: Reading and Writing XML Data
Exercise 1: Reading and Writing a DataSet as XML Data (15 minutes)
Lab Discussion
What are the four core data providers in the .NET Framework?
What classes can you use to establish a connection to a SQL Server database?
What are the advantages and disadvantages of connected and disconnected operations?
What are the available options for reading and writing XML data?
Recommended