Download presentation
1
Module 7: Accessing Data by Using ADO.NET
2
Module Overview Overview of Data Access
Reading and Writing Relational Data Reading and Writing XML Data
3
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
4
Data provider-specific classes Data provider-independent classes
ADO.NET Object Model ADO.NET Connected model vs. disconnected model Data provider interfaces Data provider-specific classes Data provider-independent classes SqlConnection SqlCommand SqlDataReader SqlDataAdapter DataSet DataTable
5
XML Data Access XmlReader 1 XmlWriter 2 XmlDocument DOM 3 XmlElement
6
Data Access Namespaces
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.SqlClient Classes for SQL Server version 7.0 and later data sources System.Xml Classes for XML data sources …
7
Choosing a .NET Framework 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 and later
8
AdventureWorks Database AdventureWorks Database
What Are Connected and Disconnected Operations? Connected model Disconnected model DataSet .NET application .NET application Open connection Open connection Run commands Create DataSet Retrieve results Close connection Close connection Manipulate data Open connection AdventureWorks Database AdventureWorks Database Update tables Close connection
9
Accessing Data by Using the Visual Studio 2005 IDE
1 On the Data menu, click Add New Data Source 2 Define a new database connection 3 Save the connection string in the app.config file 4 Specify the tables you want in the DataSet 5 Specify a name for the DataSet
10
Binding Data to Controls
1 Add a control to a Windows form 2 Choose the data source for the control 3 Add queries to the table adapter, if necessary 4 Write code to fill the table in the DataSet 5 Write code to save the table in the DataSet
11
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
12
Lab: Accessing Data by Using a DataGridView Control
Exercise 1: Binding Data to Controls in a Form (15 minutes)
13
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
14
Connecting to a Data Source
Database 1. Import namespaces 2. Create a Connection object 3. Call the Open method to open the connection 4. Call the Close method to close the connection .NET Application
15
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
16
How to Retrieve Data by Using a Data Reader
1 Create a connection object 2 Create a command object and specify an SQL query 3 Open the database connection 4 Invoke the ExecuteReader method and obtain a data reader object 5 Use the data reader object to iterate through the result set 6 Close the database connection .NET Application Database
17
What Is a Data Adapter? DataSet Database DataAdapter
Invoke Fill method to populate DataSet 1 Invoke Update method to merge changes 2 DataSet Database DataTable Object DataAdapter Fill Method Update Method
18
How to Fill and Access DataSet Data
1 Import required namespaces 2 Create a SqlDataAdapter object 3 Set the SelectCommand property to an SQL query 4 Create a DataSet object 5 Open the database connection 6 Invoke the Fill method 7 Close the database connection
19
How to Save a DataSet to a Database
1 Import required namespaces 2 Create a SqlDataAdapter object 3 Set the command property to an SQL query 4 Create a DataSet object 5 Open the database connection 6 Invoke the update method 7 Close the database connection
20
Guidelines for Secure Coding with ADO.NET
Do not echo user security credentials when opening a connection Use Windows authentication if possible Store connection strings in the application configuration file Encrypt connection strings Validate user input Prevent SQL injection attacks Stored procedures . . .
21
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)
22
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
23
How to Read and Write a DataSet as XML Data
Save a DataSet as XML DiffGram IgnoreSchema WriteSchema Load XML into a DataSet Auto Fragment InferSchema InferTypeSchema ReadSchema DataSet
24
How to Read XML Data by Using XmlReader
1 Create an XmlReaderSettings object 2 Create an XmlReader object 3 Iterate through the nodes in the XML document 4 Retrieve the value of nodes 5 Close the document
25
How to Write XML Data by Using XmlWriter
1 Create an XmlWriterSettings object 2 Create an XmlWriter object 3 Write the start of the XML document 4 Write nodes to the XML document 5 Write the end of the XML document
26
How to Process XML Data by Using the Document Object Model
DOM: A standard API for reading and writing XML data Node types include: root, element, child element, and attribute Operations: Read the content of XML data Move randomly around the XML document Add, modify, clone, and delete content in the XML document Create a new XML document
27
Lab: Reading and Writing XML Data
Exercise 1: Reading and Writing a DataSet as XML Data (15 minutes)
28
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?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.