Module 3: Performing Connected Database Operations.

Slides:



Advertisements
Similar presentations
17. Data Access ADO.Net Architecture New Features of ADO.NET
Advertisements

ADO.NET: Working in Disconnected Environment Sergey Baidachni MCT, MCSD, MCDBA.
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Chapter 10 ADO. What is ADO? ADO is a Microsoft technology ADO stands for ActiveX Data Objects ADO is a programming interface to access data in a database.
Introduction to Database Processing with ADO.NET.
VB.NET Database Tools ISYS Net Applications OLE DB Provider OLE DB Data Source OLE DB Provider ODBC Data Source SQL Server Data Source SQL Server.Net.
Coding ADO.NET Objects: Connection, Command, DataReader.
ADO. NET. What is “ADO.Net”? ADO.Net is a new object model for dealing with databases in.Net. Although some of the concepts are similar to the classical.
VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Coding ADO.NET Objects: Connection, Command, DataReader.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
Module 5: Data Access. Overview Introduce database components involved in data access Introduce concepts of Transact -SQL and Procedural SQL as tools.
In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
Developing Web Applications Using Microsoft ® Visual Studio ® 2008.
Accessing SQL Server and OLE DB from.NET Svetlin Nakov Telerik Corporation
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
Databases and Data Access  Introduction to ADO.NET  ADO.NET objects  ADP.NET namespaces  Differences between ADO and ADO.NET.
1 Introduction to ADO.NET Microsoft ADO.NET 2.0 Step by Step Rebecca M Riordan Microsoft Press, 2006.
 It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Objectives In this lesson, you will learn to: *Identify the need for ADO.NET *Identify the features of ADO.NET *Identify the components of the ADO.NET.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
Module 9: Accessing Relational Data Using Microsoft Visual Studio.NET.
Module 2: Connecting to Data Sources. Overview Choosing a.NET Data Provider Defining a Connection Managing a Connection Handling Connection Exceptions.
Module 7: Accessing Data by Using ADO.NET
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
ADO.NET Part 2. Slide 2 Overview Slide 3 Introduction to the DataGridView Control It’s a two-dimensional grid containing rows and columns Its use in.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Module 8: Implementing Stored Procedures. Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans.
An Introduction to ADO.Net Marmagna Desai.NET Seminar, Fall-2003.
ADO.NET Objects – Data Providers Dr. Ron Eaglin. Requirements Visual Studio 2005 Microsoft SQL Server 2000 or 2005 –Adventure Works Database Installed.
 It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data.
1 11/10/05CS360 Windows Programming ADO.NET. 2 11/10/05CS360 Windows Programming ADO.NET  Behind every great application is a database manager o Amazon.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
Accessing Data with Microsoft Visual C# Applications.
Building Data-Centric Smart Client Applications Rajiv Sodhi Microsoft India.
HNDIT Rapid Application Development
Coding ADO.NET Objects: Connection, Command, DataReader.
Module 4 Introduction ADO.NET.
Active Data Objects Using.Net ADO.Net Farooq Ahmed Amna Umber Summayya Shehzad.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Module 2: Using ADO.NET to Access Data. Overview ADO.NET Architecture Creating an Application That Uses ADO.NET to Access Data Changing Database Records.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 350.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 The SqlCommand Object ADO.NET - Lesson 03  Training time: 15 minutes  Author:
Data Access. ADO.NET ADO.NET is the primary library for building database solutions within the.NET Framework. ADO.NET does not replace ADO. ADO and OLEDB.
ADO .NET from. ADO .NET from “ADO .Net” Evolution/History of ADO.NET MICROSOFT .NET “ADO .Net” Evolution/History of ADO.NET History: Most applications.
Coding ADO.NET Objects: Connection, Command, DataReader.
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Database Processing with ADO.NET
Introduction to Database Processing with ADO.NET
ADO.NET Framework.
The Basics of Data Manipulation
Programming the Web Using ASP.Net
How to Create Login Form using vb.net and SqlServer Database
10 | Programming with Transact-SQL
Active Data Objects Binding ASP.NET Controls to Data
Tonga Institute of Higher Education
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
The Basics of Data Manipulation
مقدمة في قواعد البيانات
SQL .. An overview lecture3.
Coding ADO.NET Objects: Connection, Command, DataReader
Active Data Objects Binding ASP.NET Controls to Data
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Introduction to Database Programming through ADO. NET
Presentation transcript:

Module 3: Performing Connected Database Operations

Overview Working in a Connected Environment Building Command Objects Executing Command Objects That Return a Single Value Executing Commands That Return Rows Executing Commands That Do Not Return Rows Using Transactions

Lesson: Working in a Connected Environment Object Model for Connected Applications Typical scenarios for a connected environment.NET Framework classes used in a connected environment application

Object Model for Connected Applications Data Source XxxConnection XxxParameter XxxDataReader XxxCommand XxxParameter XmlReader Classes in a Connected Application

Lesson: Building Command Objects What Is a Command Object? How to Create a Stored Procedure How to Create a Command Object Demonstration: Creating a Command Object Graphically What Are Command Parameters? How to Create Parameters for a Command Object

What Is a Command Object? A command object is a reference to a SQL statement or stored procedure Properties (Name), Connection, CommandType, CommandText, Parameters Methods ExecuteScalar, ExecuteReader, ExecuteNonQuery ExecuteXmlReader (SqlCommand only)

How to Create a Stored Procedure Server Explorer On the View menu, click Server Explorer, or press Ctrl+Alt+S Create a data connection Click New Stored Procedure Insert SQL Demonstration Creating a stored procedure Testing a stored procedure

How to Create a Command Object Programmatically Server Explorer On the View menu, click Server Explorer, or press Ctrl+Alt+S Drag stored procedure onto form or component Toolbox Use SqlConnection or OleDbConnection Use SqlCommand or OleDbCommand

Demonstration: Creating a Command Object Graphically In this Demonstration, you will see how to use a stored procedure that will return all products in the Northwind database that have not been discontinued

What Are Command Parameters? Introduction SQL statements and stored procedures can have input and output parameters, and a return value Command parameters allow these parameters to be set and retrieved SqlParameter, OleDbParameter Properties ParameterName, DbType, Size, Direction Visual Basic Example

How to Create Parameters for a Command Object Programmatically Code example Dim prmName As New SqlParameter( _ SqlDbType.NChar, 15) prmName.Direction = ParameterDirection.Output cmdCountProds.Parameters.Add(prmName) Using the Visual Studio.NET graphical tools Visual Basic Example

Lesson: Executing Command Objects That Return a Single Value Why Return a Single Value in a Command? How to Execute a Command That Returns a Single Value How to Retrieve Output and Return Values

Why Return a Single Value in a Command? ADO.NET is more efficient than ADO, where a complete record set is returned Examples Units in stock for a particular product How many products? COUNT, MAX, MIN, AVERAGE

How to Execute a Command That Returns a Single Value Call the ExecuteScalar method ExecuteScalar returns a value of the type Object Use CType or a cast, to convert into appropriate type Microsoft Visual Basic ® code example: = 42 cnNorthwind.Open() Dim qty As Integer = _ CType(cmProducts.ExecuteScalar(), Integer) cnNorthwind.Close() Visual Basic Example

How to Retrieve Output and Return Values How to get output parameters from a command How to get the return value from a stored procedure Code examples Stored Procedure and Visual Basic Example

Lesson: Executing Commands That Return Rows Returning Rows DataReader Properties and Methods How to Use a DataReader to Process Rows How to Execute Multiple SQL Statements

Returning Rows DataReader Read-only, forward-only, stream of rows The ExecuteReader method Returns a DataReader For example, SqlDataReader, OleDbDataReader

DataReader Properties and Methods Read method Loads the next row Returns true if a row exists, false if at end of rows Item property GetXxx methods – for example, GetString, GetInt32 GetValues method IsDbNull method Close method

How to Use a DataReader to Process Rows Using a DataReader object to process a result set Code example Dim cmProducts As New SqlCommand( _ "SELECT ProductName, UnitsInStock " & _ "FROM Products", cnNorthwind) cnNorthwind.Open() Dim rdrProducts As SqlDataReader rdrProducts = cmProducts.ExecuteReader() Do While rdrProducts.Read() ListBox1.Items.Add(rdrProducts.GetString(0)) Loop rdrProducts.Close() Visual Basic Example

How to Execute Multiple SQL Statements A stored procedure can contain multiple SQL statements Group-related tasks Encapsulate business rules If the stored procedure returns multiple result sets Call NextResult to move to the next result set To determine how many rows were affected by the stored procedure Use the RecordsAffected property

Lesson: Executing Commands That Do Not Return Rows What Are DDL and DCL Statements? How to Execute DDL and DCL Statements What Are DML Modification Statements? How to Execute DML Modification Statements Troubleshooting Data Modification

What Are DDL and DCL Statements? Definition Automate database administration tasks DDL and DCL statements CREATE, ALTER, DROP, GRANT, DENY, REVOKE Code example CREATE PROCEDURE dbo.SummarizeProducts AS CREATE TABLE ProductSummary ( ProductName nvarchar(40), CategoryName nvarchar(15) )

How to Execute DDL and DCL Statements ExecuteNonQuery method Returns count of rows affected Code example cnNorthwind.Open() Dim affected As Integer = _ cmSummarizeProducts.ExecuteNonQuery() cnNorthwind.Close() MessageBox.Show("Records affected: " & _ affected) Visual Basic Example

What Are DML Modification Statements? Definition Modify data in the database DML Statements INSERT, UPDATE, DELETE Code example CREATE PROCEDURE dbo.InsertRegion nchar(50) ) AS INSERT INTO Region

How to Execute DML Modification Statements To execute a DML statement ExecuteNonQuery method Code example cnNorthwind.Open() Dim affected As Integer = _ cmPrices.ExecuteNonQuery() cnNorthwind.Close() MessageBox.Show("Records affected: " & _ affected) Visual Basic Example

Troubleshooting Data Modification Common errors Incorrect object names Server unavailability Data integrity issues Using connection before it is open Invalid data types

Lesson: Using Transactions What Is a Transaction? How to Manage Transactions Using SQL Statements How to Manage Transactions Using ADO.NET What Are Isolation Levels?

What Is a Transaction? A transaction is a set of related tasks that either succeed or fail as a unit Two types of transactions Local transactions Distributed transactions

How to Manage Transactions Using SQL Statements SQL transaction statements BEGIN TRANS, COMMIT TRANS, ROLLBACK TRANS Code example BEGIN TRANS int DELETE FROM "Order Details" WHERE ProductID=42 = DELETE FROM Products WHERE ProductID=42 = = 0 = 0 COMMIT TRANS ELSE ROLLBACK TRANS

How to Manage Transactions Using ADO.NET XxxConnection – for example, SqlConnection BeginTransaction XxxTransaction – for example, SqlTransaction Commit Rollback Code examples Visual Basic Example

What Are Isolation Levels? Examples of concurrency problems Guidelines for setting the isolation level Code example trans = cnNorthwind.BeginTransaction( _ IsolationLevel.Serializable) Support for isolation levels is dependent on which database you use

Review Working in a Connected Environment Building Command Objects Executing Commands That Return a Single Value Executing Commands That Return Rows Executing Commands That Do Not Return Rows Using Transactions

Lab 3.1: Performing Connected Database Operations Executing a Command Object That Returns a Single Value Executing a Command Object That Returns Records Executing a Command Object That Returns Multiple Results Executing a Command Object That Modifies the Database