Download presentation
Presentation is loading. Please wait.
Published byAllen Flowers Modified over 9 years ago
1
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
2
Slide 2 Objectives Understand database processing using ADO.NET Perform specialized database processing tasks Work with database data programmatically using either DataReaders (connected mode) DataSets (disconnected mode) We discuss DataReaders here and will DataSets in Lecture Set 14C
3
Slide 3 DataReader The DataReader is a component used for read-only and forward-only connection to a database. Results are returned as a query executes and stored in the network buffer on the client until you request them using the Read method of the DataReader. Using the DataReader can increase application performance both by retrieving data as soon as it is available, and (by default) storing only one row at a time in memory, reducing system overhead. It is used to execute a query via a connection to a database and iterate through the data returned. The abstraction provided here is that of a connected data architecture.
4
Slide 4 DataReader
5
Slide 5 Wizards and Bound Data vs DataReaders and DataSets Wizard and data bound fields are useful for the rapid development of forms and displays based on content of a database They are not useful for most transaction processing systems because of inflexibility Use of code to control database access is more powerful because the programmer can manipulate data before it is stored or after it is retrieved I prefer to avoid wizards where possible
6
Slide 6 The Processing Sequence Holds for both connected and disconnected architectures Establish a connection to your data source using a Connection Object These objects are simple to construct Create an SQL statement (a string) and wrap it in a Command object These strings are harder to build – more detailed Execute the Command object within the context of the Connected DB – there are methods for this Process (retrieve or store) results of command if there are any Use a DataReader object to scan through records [Use a combination of a DataAdapter and DataSet (or DataTable) objects for storage and retrieval] Close all objects you opened to process data
7
Slide 7 The Primary Objects The Connection Object – Directs communication between your program and Data Source. Handles location and connection parameters for the data source. The Command Object – Takes an SQL statement you provide (as a string) and prepares it for transport through the Connect- ion Object and subsequent processing in the specified DBMS. The DataReader Object – Provides a simple and efficient way to retrieve results from an SQL query. It is used by other objects in ADO.NET to retrieve and redirect data within your program. You can use this reader directly to process the result of a SELECT or other retrieval action. [The DataAdapter Object – Enables communication between a DataSet and the rest of the Provider. Modifies SELECT, DELETE, INSERT, and UPDATE statements for us by related data source (see Ch 11 Part C -- once it is done)].
8
Slide 8 DataReader Example
9
Slide 9 Example: Insert a Record
10
Slide 10 Example: Delete a Record
11
Slide 11 Example: Update a Record
12
Slide 12 ADO.NET
13
Slide 13 DataReaders – main use DataReaders Simple and efficient way to retrieve results from a query Can use directly to process results of a SELECT or to provide direct access INSERTs, DELETEs, and UPDATEs A query gives you access to one row of table information at a time Your connection must be open before a command is executed and should be closed when you are done
14
Slide 14 Using DataReaders
15
Slide 15 The OleDbCommand Class The OleDbCommand class stores SQL statements The Connection property stores a reference to an OleDbConnection The CommandText property stores an SQL statement The CommandType should be set to Text
16
Slide 16 DataSets vs DataReaders 1 DataReaders retrieve data in read only form. Data in a DataSet may be modified in memory and updated in one step DataReaders allocate memory to one record (one table row) of data at a time. Efficient. Little overhead. DataSets are less efficient. Space must be allocated for entire table involved. Only one record at a time can be processed. Random access through entire DataSet possible. Only one DataReader can be open at a time. Multiple DataSets can be open at once.
17
Slide 17 DataSets vs DataReaders 2 Live connection to the database exists as long as the DataReader is open. Data connections maintained only long enough to transfer data between DataSet and database. With DataReaders you spend a lot of time working with strings (for SQL statements) and raw data fields (You supply all SQL statements). DataSets and DataAdapters assist in crafting SQL statements for you. All fields in a DataSet have the same logical organization as in the actual database.
18
Slide 18 DataSets
19
Slide 19 DataSets Each DataSet contains one or more Tables Table[0], Table[1], Table[2] etc Each DataTable contain one or more rows Row[0], Row[1], Row[2] etc Each Row contains one or more fields These can be access by index [0]or by field name [“StudentName”]
20
Slide 20 Examples (Finally) Illustrations of various DB commands Pattern of use of methods that are part of the collection of class libraries that support “Providers” technology is complete consistent Create a connection, dbCon, between your (client) code and the database Build a database command (SQL select, update, insert, or delete ) as a VB string s Send the string and connection information via a command to the provider
21
Slide 21 Example 1 – Insert (plus a simple select) (Uses Insert and DataReader commands to effect data transmission)
22
Slide 22 Example 2 – Saving a Current Record
23
Slide 23 Example 3 – Using Max Transaction ID Commit list of transactions to a DB
24
Slide 24 Example 4 – Delete (remove) a record
25
Slide 25 Example 5 – Update a Record
26
Slide 26 The Plan of Action The goal is to have you work on Phase 2 of your lab using DataReaders Then – as time permits, we will migrate over to the ASP.NET (client-server world) using Datasets REMEMBER with DataReaders (in connected mode) all commands are executed on the database itself – not on any internal representation of the database. To better understand the connected mode, study Your HW #10 VB Employee-Manager code as examined in class Pascucci’s Small DB Example
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.