06 ddcauado.net cg session01

Upload: jai-deep

Post on 14-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 06 Ddcauado.net Cg Session01

    1/18

    Instructor InputsSess

    ion1

  • 7/30/2019 06 Ddcauado.net Cg Session01

    2/18

  • 7/30/2019 06 Ddcauado.net Cg Session01

    3/18

    NIIT Instructor Inputs 1.3

    This session covers Chapter 1.

    To understand the concepts covered in the session, the students should be aware of the

    following concepts/skills:

    Understanding the .NET architecture Understanding the various controls of a Windows form

    This session introduces ADO.NET. This session further discusses the ADO.NET object

    model. In addition, it explains how to create and manage connections to a data source.

    To enable students to better understand the preceding concepts, encourage them to

    provide their own examples, wherever required. Use the animated slides, wherever

    provided, to conduct the session effectively.

    Slide 1

    Slide 1 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    Business applications need to manage voluminous data.Data is generally stored in a relational database in the formof related tables or is stored in text format in XMLdocuments. Most business applications allow users toretrieve the data stored in a database and present it in auser-friendly interface without writing the databasecommands. ADO.NET is a model used by .NET applicationsto communicate with a database for retrieving, accessing,and updating data. This module will provide the necessaryskills to the students to work as a database application

    developer in the industry.

    Rationale

    Start the session by sharing the Rationale with the students.

    Session Overview

    Prerequisite Knowledge for the Session

    Tips for Handling the Session

  • 7/30/2019 06 Ddcauado.net Cg Session01

    4/18

    1.4 Instructor Inputs NIIT

    Slide 2

    Slide 2 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    A student registering for this module should be able toperform the following tasks:

    Work with XML

    Work with SQL queries

    Prerequisites

    Share the Entry Profile forDeveloping Data-Centric Applications Using ADO.NET coursewith the students.

    Slide 3

    Slide 3 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    Brief History:

    Tebisco is a leading producer and distributor of snacks in theU.S., as well as in most of the companys 23 internationalmarkets. In 1998, consumers spent $9.2 billion on Tebisco ssnacks, $1.4 billion more than in the previous year.

    Tebisco started as a small bakery in Round Rock, Texas in1978. In a short time, its gingersnaps, macaroons, shortbreadand other cookies were popular all over the U.S. Three yearsago, the management embarked on a rapid expansion plan.They set up offices in Asia and Europe, in addition tostrengthening their U.S. operations.

    Tebisco has got a centralized database management systemwhereby the information about all the HR activities ismaintained.

    Case Study - Tebisco

    Share the case study of Tebisco with the students.

  • 7/30/2019 06 Ddcauado.net Cg Session01

    5/18

    NIIT Instructor Inputs 1.5

    Slide 4

    Slide 4 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    In this session, you will learn to:

    Understand the ADO.NET object model

    Create and manage connections

    Objectives

    Share the objectives of the sessions with the students.

    Slide 5

    Slide 5 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    Business applications allow users to retrieve data from adatabase by presenting data in a user-friendly interface.

    User need not remember the database commands forretrieving or updating data in the database.

    Microsoft has created a family of data access technologiesto help programmers build efficient applications to accessdata, regardless of its source.

    The guidelines that can be followed for selecting anappropriate data access technology are:

    Understanding ADO.NET

    Use ADO.NET for writing a managed codetargeting the .NET Framework in Visual Basic,C#, and C++.

    Use Microsoft ODBC for writing a native codetargeting Windows by using C or C++.Use (OLE) DB for writing a Microsoft

    based application, a VB 6 COM application, or

    a C++ application using COM.

    Use JDBC for writing a Java code targetingSQL Server.

    Introduce ADO.NET to the class and explain it by providing a real-world example, such

    as the railway reservation or airline reservation systems.

  • 7/30/2019 06 Ddcauado.net Cg Session01

    6/18

    1.6 Instructor Inputs NIIT

    Slide 6

    Slide 6 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    ADO.NET is a part of the .NET Framework architecture.

    Understanding ADO.NET (Contd.)

    With help of the figure given on the slide, explain to students that ADO.NET is part of.NET Framework.

    Slide 7

    Slide 7 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    ADO.NET is based on an object model that is based on thestandards laid down by W3C.

    The following figure shows the ADO.NET object model.

    The ADO.NET Object Model

    Explain the ADO.NET Object Model with help of the figure given on the slide.

  • 7/30/2019 06 Ddcauado.net Cg Session01

    7/18

    NIIT Instructor Inputs 1.7

    Slide 8

    Slide 8 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    The two key components of ADO.NET Object model are:

    Data provider

    Dataset

    The ADO.NET Object Model (Contd.)

    Is required for:

    Connecting to a database.

    Retrieving data.

    Storing the data in a dataset.

    Reading the retrieved data.

    Updating the database.

    Has four key components:

    Connection

    Command

    DataReader

    DataAdapter

    Slide 9

    Slide 9 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    The two key components of ADO.NET Object model are:Data provider

    Dataset

    The ADO.NET Object Model (Contd.)

    Is a disconnected, cached set ofrecords that are retrieved from adatabase.

    Is present in the Dataset class inthe System.Data namespace.

    Has the following keycomponents:

    DataTableCollection

    DataRelationCollection

    DataTable

    DataRowCollection

    DataColoumnCollection

    Explain two components of ADO.NET:

    1. DataSet2. Data Provider

  • 7/30/2019 06 Ddcauado.net Cg Session01

    8/18

    1.8 Instructor Inputs NIIT

    While discussing data provider, demonstrate the activity.

    You can access the solution of this activity from the following path in the TIRM CD:

    Datafiles_For_Faculty\ADO.NET\Chapter 01\Activity1.

    Slide 10

    Slide 10 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    Which of the following components of a data provider isused to retrieve, insert, delete, or modify data in a datasource?

    1. Connection

    2. Command

    3. DataReader

    4. DataAdapter

    Just a minute

    Answer:

    2. Command

    Reiterate the concepts taught earlier by asking the given question.

    Slide 11

    Slide 11 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    The key features of ADO.NET are:

    Disconnected data architecture

    Data cached in datasets

    Scalability

    Data transfer in XML format

    Features of ADO.NET

    Applications connect tothe database only whileretrieving and updatingdata.

    Connection with thedatabase is closed,once the data isretrieved.

    Connection isre-established whenthe data needs to beupdated.

    The data is retrievedand stored in datasets.

    You can work with therecords stored in adataset as you workwith real data.

    The dataset isindependent of datasource and you remaindisconnected from thedata source.

    Database operations

    are performed on thedataset instead of onthe database.

    As a result, resourcesare saved and thedatabase can meet theincreasing demands ofusers more efficiently.

    XML is the fundamentalformat for data transferin ADO.NET.

    Because a dataset isstored in the XMLformat, you cantransmit it betweendifferent types ofapplications.

  • 7/30/2019 06 Ddcauado.net Cg Session01

    9/18

    NIIT Instructor Inputs 1.9

    Discuss the features of ADO.NET.

    Slide 12

    Slide 12 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    To create and manage connections, you need to:

    Create a connection object.

    Create a command object.

    Open the connection object.

    Execute the SQL statement in the command object.

    Close the connection object.

    Creating and Managing Connections

    Discuss the steps for creating and managing connections.

    Slide 13

    Slide 13 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    SqlConnection connection =

    new SqlConnection();

    connection.ConnectionString =

    "Data Source=SQLSERVER01;

    Initial Catalog=HR;

    User ID=sa;

    Password=password";

    Create a SqlConnection object.

    SqlConnection class is used to

    connect to a SQL Server.The ConnectionStringproperty

    provides information, such as thedata source and database name,that is used to establish aconnection with a database.

    Name of the Server to be usedwhen a connection is open.Name of the database.

    Used to specify the Server loginaccount.Login password for the Serveraccount.

    Creating a Connection Object

    Execute the following steps to create a connection to thedatabase:

    Discuss how to create a connection object with help of the code snippet given in the SG.

  • 7/30/2019 06 Ddcauado.net Cg Session01

    10/18

    1.10 Instructor Inputs NIIT

    Slide 14

    Slide 14 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    Which of the following parameters of ConnectionString isused to specify the name of the database?

    1. Provider

    2. Initial Catalog

    3. Data source

    4. Database

    Just a minute

    Answer:

    2. Initial Catalog

    Reiterate the concepts taught earlier by asking the given question.

    Slide 15

    Slide 15 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    SqlCommand cmd = new

    SqlCommand

    (SELECT * FROM

    monthlysalary

    ,connection);

    Creating a Command Object

    To execute an SQL statement, you need to

    create an instance of the SqlCommand

    class.The two parameters that are passed to theSqlCommnad object are, the SQL query tobe executed and the SqlConnection

    object.

    Execute the following steps to create a command object:

    Discuss how to create a command object with help of the code snippet given in the SG.

  • 7/30/2019 06 Ddcauado.net Cg Session01

    11/18

    NIIT Instructor Inputs 1.11

    Slide 16

    Slide 16 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    //SqlConnection connection

    connection.Open();

    Opening the Connection Object

    It opens a database connectionwith the property settingsspecified by the

    ConnectionStringproperty.

    Execute the following steps to open a connection:

    Discuss how to open a connection object with help of the code snippet given in the SG.

    Slide 17

    Slide 17 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    To execute the query passed in the Command object, youcan call one of the following methods:

    Executing SQL Statements in the Command Object

    //Creating a SqlConnection object

    SqlConnection connection = new

    SqlConnection();

    //Creates a connection string to the HR

    database

    connection.ConnectionString = "Data Source=

    SQLSERVER01; Initial Catalog=HR; User

    ID=sa; Password=niit#1234";

    connection.Open();

    //Creating a SqlCommand object

    SqlCommand cmd = new SqlCommand("select *

    from monthlysalary", connection);

    //Creating SqlReader object

    SqlDataReader myReader =

    cmd.ExecuteReader();

    Discuss how to execute the SQL statement in a command object with help of the code

    snippet given in the SG.

  • 7/30/2019 06 Ddcauado.net Cg Session01

    12/18

    1.12 Instructor Inputs NIIT

    Slide 18

    Slide 18 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    //SqlConnection connection

    connection.Close();

    Closing the Connection Object

    It closes the connection to thedatabase.

    Execute the following steps toclose a connection:

    Discuss how to close a connection object with help of the code snippet given in the SG.

    Slide 19

    Slide 19 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    Handling Connection Events:

    The two key events for the SqlConnection class are:

    StateChange event

    InfoMessage event

    Closing the Connection Object (Contd.)

    This event occurs when the stateof the connection changes.

    It receives an argument of typeStateChangeEventArgs.

    StateChangeEventArgshas the

    following properties:CurrentState

    OriginalState

    This event occurs when aninformational message orwarning is returned from a datasource.

    It receives an argument of typeSqlInfoMessageEventArgs.

    SqlInfoMessageEventArgs

    has the following properties:Errors

    Message

    Source

    Explain the events associated with the connection object.

  • 7/30/2019 06 Ddcauado.net Cg Session01

    13/18

    NIIT Instructor Inputs 1.13

    Slide 20

    Slide 20 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    Connection pooling enables a data source to reuseconnections for a particular user.

    Connection pooling is controlled by certain parameters thatare placed into the connection string.

    Connectiontimeout

    Min pool size

    Max pool size

    Pooling

    Connectionreset

    Load balancing

    timeout, connection

    lifetime

    Enlist

    Implementing Connection Pooling

    It is the time in seconds to waitwhile a connection to the datasource is attempted. Thedefault value is 15 seconds.

    It is used to mention theminimum number ofconnections maintained in thepool. The default value is 0.

    It is used to mention themaximum number ofconnections allowed in the pool.The default value is 100.

    When true, it causes therequest for a new connection tobe drawn from the pool.It indicates that the databaseconnection will be reset whenthe connection is removed from

    the pool.

    It specifies the maximum timein seconds that a pooledconnection should live.

    When the value is true, theconnection is automatically enlistedinto the creation threads current

    transaction context.

    Explain the concept of connection pooling. Discuss the parameters associated withconnection pooling.

    Slide 21

    Slide 21 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    A request for a connection ismade by the application usingthe Open()method.

    If the Pooling property is setto true, the pooler attempts toacquire a connection from thepool otherwise a newconnection is created.

    Close the connection bycalling the close()method.

    Implementing Connection Pooling (Contd.)

    Explain the life cycle of a connection with the help of the diagram given in the SG.

  • 7/30/2019 06 Ddcauado.net Cg Session01

    14/18

    1.14 Instructor Inputs NIIT

    Slide 22

    Slide 22 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    Problem Statement:

    Tebisco is a leading producer and distributor of snacks in theUnited States. It is planning to start its annual appraisalprocess. Before starting up with the appraisal process, thesenior management requires a list of all employees. Thedetails include employee name, employee code, currentposition, designation, and joining date.

    As a member of the development team, you have been askedto develop an application that will display the employee details.

    Hint: You need to refer to the Employee table of the HRdatabase.

    Demo: Retrieving Data from a SQL Database

    Use this slide to conduct the activity.

    Slide 23

    Slide 23 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    In this session, you learned that:

    ADO.NET is a data access programming model for accessingthe data stored in a database from a .NET application.

    The ADO.NET object model consists of two main components,data provider and dataset.

    A data provider is used for connecting to a database, retrievingdata, storing the data in a dataset, reading the retrieved data,and updating the database.

    The various types of data providers are:

    .NET Framework data provider for SQL Server

    .NET Framework data provider for OLEDB

    .NET Framework data provider for ODBC

    .NET Framework data provider for Oracle

    Summary

    Use this and the following slides to summarize the sessions.

  • 7/30/2019 06 Ddcauado.net Cg Session01

    15/18

    NIIT Instructor Inputs 1.15

    Slide 24

    Slide 24 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    The four key components of a data provider are:

    Connection

    Commnd

    DataReader

    DataAdapter

    The dataset is memory-based relational representation of data.

    The main features of ADO.NET are:

    Disconnected data architecture

    Data cached in datasets

    Scalability

    Data transfer in XML format

    Summary (Contd.)

    Slide 25

    Slide 25 of 25Ver. 1.0

    Developing Data-Centric Applications Using ADO.NET

    Session 1

    In order to create and manage connection to the database, youneed to perform the following steps:

    1. Create a connection object.

    2. Create a command object.

    3. Open the connection object.

    4. Execute the SQL statement in the command object.

    5. Close the connection object.

    The two key events for the SqlConnection class are:

    StateChange event

    InfoMessage event

    Connection pooling enables a data source to reuseconnections for a particular user.

    Summary (Contd.)

  • 7/30/2019 06 Ddcauado.net Cg Session01

    16/18

    1.16 Instructor Inputs NIIT

    The following questions can be asked by the student in this session:

    1. When is the connection pool created?Ans. When a connection is opened for the first time, a connection pool is created and the

    pool is determined by the exact match of the connection string in the connection. Eachconnection pool is associated with a distinct connection string. When a new connection is

    requested, if the connection string is not an exact match to an existing pool, a new pool iscreated.

    2. When is the connection pool destroyed?Ans. The connection pool is destroyed when the last connection in the pool is closed the

    pool is destroyed.

    3. Why should you use a specialized data provider when the data can be accessed withgeneral data providers?

    Ans. The specialized data providers, such as SQL Server and Oracle, are built specially

    for a particular kind of DBMS and work more efficiently than the general data providers,

    such as OLEDB and ODBC. In practice, the specialized data providers are more efficient

    than the general data providers.

    4. How do different components of ADO.NET interact with each other in disconnectedarchitecture?

    Ans. The data adapter of ADO.NET contains the Command and Connection object. It

    uses the connection object to connect to the database, execute the command, retrieve the

    result, and update the dataset.

    5. When would you select a dataset and when would you select a data reader?Ans. A data reader is more useful when you need to work with a large number of tables

    and database in non-uniform pattern and you need not execute a large number of queries

    on few tables. When you need to work on few tables, you should use a dataset.

    The nature of an application should also be taken into consideration while selecting a

    dataset or a data reader. If multiple users are using the database and the database needs to

    be updated every time, you must not use a dataset. For this, .NET provides the

    FAQs

  • 7/30/2019 06 Ddcauado.net Cg Session01

    17/18

    NIIT Instructor Inputs 1.17

    connection-oriented architecture. However, in scenarios where an instant update of thedatabase is not required, a dataset provides optimal performance by making the changes

    locally and connecting to database later to update a whole batch of data. This also reducesthe network bandwidth if the database is accessed through a network.

    Disconnected data access is most suited for read-only services. However, thedisconnected data access architecture is not designed to be used in the networked

    environment where multiple users are updating data simultaneously and each of themneeds to be aware of current state of the database at any time For example, for an Airline

    Reservation System, you should select a connected data-access.

  • 7/30/2019 06 Ddcauado.net Cg Session01

    18/18

    1 18 Instructor Inputs NIIT