Collections and ADO.NET Lecture PDF
Document Details
Uploaded by IncredibleRetinalite2381
University of Cape Town (UCT)
Tags
Summary
This document is a lecture on collections and ADO.NET. It explains different types of collections and how to use them for data storage and retrieval within an application, specifically discussing how ADO.NET manages data communication between applications and databases.
Full Transcript
COLLECTIONS AND ADO.NET Week 5 - Tuesday Lecture For many applications, you want to create and manage groups of related objects. COLLECTIONS MSDN There are two ways to group objects: by creating arrays of obj...
COLLECTIONS AND ADO.NET Week 5 - Tuesday Lecture For many applications, you want to create and manage groups of related objects. COLLECTIONS MSDN There are two ways to group objects: by creating arrays of objects, and by creating collections of objects. Collections provide a more flexible way to work with groups of objects Collections are classes, specialized classes for data storage and retrieval. Collection classes serve various purposes, such as COLLECTIONS allocating memory dynamically to elements and accessing a list of items on the basis of an index etc. A collection class contains methods to: Add; Remove; Navigate; Search for objects… To use collections, you can use one of the classes in the System.Collections.Generic namespace. These classes contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections. You will also need the System.Collections.ObjectModel namespace IMPLEMENTING A which contains classes that can be used as collections in the object model of a reusable library. Use these classes when COLLECTION properties or methods return collections. ------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System; using System.Collections.Generic; using System.Collections.ObjectModel; public class Demo { public static void Main() { Collection dinosaurs = new Collection(); dinosaurs.Add("Psitticosaurus"); dinosaurs.Add("Caudipteryx"); dinosaurs.Add("Compsognathus"); dinosaurs.Add("Muttaburrasaurus"); Console.WriteLine("{0} dinosaurs:", dinosaurs.Count); Display(dinosaurs); https://docs.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.collection-1.add?view=netcore-3.1 Looks like Arrays right: - But... Arrays are fixed in size. Collections are variable in size, thereby providing a more flexible way of working with a group of objects. In arrays, you would have noticed that you need to define the number of elements in an array COLLECTIONS beforehand. This had to be done when the array was declared. But in a collection, you don't need to define the size of the collection beforehand. You can add elements or even remove elements from the collection at any point of time. COMMUNICATING WITH THE DATABASE CRUD Commands RULES FOR AUTOMATICALLY GENERATED COMMANDS Command Rule InsertCommand Inserts a row at the data source for all rows in the table with a RowState of Added. Inserts values for all columns that are updateable (but not columns such as identities, expressions, or timestamps). UpdateCommand Updates rows at the data source for all rows in the table with a RowState of Modified. Updates the values of all columns except for columns that are not updateable, such as identities or expressions. Updates all rows where the column values at the data source match the primary key column values of the row, and where the remaining columns at the data source match the original values of the row. For more information, see "Optimistic Concurrency Model for Updates and Deletes," later in this topic. DeleteCommand Deletes rows at the data source for all rows in the table with a RowState of Deleted. Deletes all rows where the column values match the primary key column values of the row, and where the remaining columns at the data source match the original values of the row. For more information, see "Optimistic Concurrency Model for Updates and Deletes," later in this topic. CRUD Commands InsertCommand Inserts a row at the data source for all rows in the table with a RowState of Added. Inserts values for all columns that are updateable (but not columns such as identities, expressions, or timestamps). Gets the state of a DataRow object. Commits all the changes made to this row since the last time AcceptChanges() was called. BUILD PARAMETERS, CREATE COMMANDS & UPDATE DATABASE We will use command objects to talk to our data source Command objects use parameters to pass values to SQL statements, providing type checking and validation. The syntax for parameter placeholders depends on the data source. The.NET Framework data providers handle naming and specifying parameters and parameter placeholders differently. This syntax is customized to a specific data source, as described in theParameter Data provider following table. naming syntax System.Data.SqlClient Uses named parameters in the format @parametername. System.Data.OleDb Uses positional parameter markers indicated by a question mark (?). System.Data.Odbc Uses positional parameter markers indicated by a question mark (?). System.Data.OracleClient Uses named parameters in the format :parmname (or parmname). BUILD PARAMETERS, CREATE COMMANDS & UPDATE DATABASE See Configuring parameters and parameter data types BUILD PARAMETERS, CREATE COMMANDS & UPDATE DATABASE