BUILDING APPLICATIONS USING C# AND.NET FRAMEWORK (OBJECT-ORIENTED PROGRAMMING, X428.6)

Similar documents
ADO.NET.NET Data Access and Manipulation Mechanism. Nikita Gandotra Assistant Professor, Department of Computer Science & IT

B Nagaraju

An Introduction to ADO.Net

CMPT 354 Database Systems I

ADO.NET Overview. Connected Architecture. SqlConnection, SqlCommand, DataReader class. Disconnected Architecture

.NET Connector. (MS Windows)

Industrial Programming

ADO.NET. Two Providers ADO.NET. Namespace. Providers. Behind every great application is a database manager

It is the primary data access model for.net applications Next version of ADO Can be divided into two parts. Resides in System.

Programming in Visual Basic with Microsoft Visual Studio 2010

Mobile MOUSe ADO.NET FOR DEVELOPERS PART 1 ONLINE COURSE OUTLINE

PROGRAMMING IN VISUAL BASIC WITH MICROSOFT VISUAL STUDIO Course: 10550A; Duration: 5 Days; Instructor-led

.NET FRAMEWORK. Visual C#.Net

Program Contents: DOTNET TRAINING IN CHENNAI

C# Syllabus. MS.NET Framework Introduction

About the Authors Introduction p. 1 Exploring Application Architectures p. 9 Introduction p. 9 Choosing the "Right" Architecture p.

ADO.NET in Visual Basic

DOT NET Syllabus (6 Months)

Migrate Your Skills to Microsoft.NET Framework 2.0 and 3.0 using Visual Studio 2005 (C#)

ADO.NET from 3,048 meters

Saikat Banerjee Page 1

Data Access Standards. ODBC, OLE DB, and ADO Introduction. History of ODBC. History of ODBC 4/24/2016

.NET and DB2 united with IBM DB2.NET Data Provider Objectives :.NET ADO.NET DB2 and ADO.NET DB2 - ADO.NET applications

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Contents. Chapter 1 Introducing ADO.NET...1. Acknowledgments...xiii. About the Authors...xv. Introduction...xix

Mastering VB.NET using Visual Studio 2010 Course Length: 5 days Price: $2,500

6 Microsoft.Data.Odbc

C#.NET TRAINING / /

ADO.NET for Beginners

Visual Basic.NET Complete Sybex, Inc.

.Net Interview Questions

PERSİSTENCE OBJECT RELATİON MAPPİNG

SECURED PROGRAMMING IN.NET DETAILED TRAINING CONTENT INDUSTRIAL TRAINING PROGRAM ( )

overview of, ASPNET User, Auto mode, 348 AutoIncrement property, 202 AutoNumber fields, 100 AVG function, 71

INTRODUCTION TO.NET. Domain of.net D.N.A. Architecture One Tier Two Tier Three Tier N-Tier THE COMMON LANGUAGE RUNTIME (C.L.R.)

.Net. Course Content ASP.NET

Saikat Banerjee Page 1

ASP.NET Web Forms Programming Using Visual Basic.NET

C# is intended to be a simple, modern, general-purpose, objectoriented programming language. Its development team is led by Anders Hejlsberg.

Accessing Databases 7/6/2017 EC512 1

Oracle Rdb Technical Forums

Learning C# 3.0. Jesse Liberty and Brian MacDonald O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

Introduction to Programming Microsoft.NET Applications with Visual Studio 2008 (C#)

EF6 - Version: 1. Entity Framework 6

OVERVIEW ENVIRONMENT PROGRAM STRUCTURE BASIC SYNTAX DATA TYPES TYPE CONVERSION

.NET data providers 5.1 WHAT IS A DATA PROVIDER?

AUTHENTICATED WEB MANAGEMENT SYSTEM

Introduction to Programming Using Java (98-388)

C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies. Objectives (1 of 2)

10265: Developing Data Access Solutions with Microsoft Visual Studio 2010 Duration: 5 Days Method: Instructor-Led

Create a Windows Application that Reads- Writes PI Data via PI OLEDB. Page 1

Developing Data Access Solutions with Microsoft Visual Studio 2010

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 14 Database Connectivity and Web Technologies

Apex TG India Pvt. Ltd.

Lecture 10: Database. Lisa (Ling) Liu

Beginning ASP.NET. 4.5 in C# Matthew MacDonald

Programming in C# for Experienced Programmers

PLATFORM TECHNOLOGY UNIT-4

ALPHAPRIMETECH 112 New South Road, Hicksville, NY 11801

Getting Started with Oracle and.net

DOT NET COURSE BROCHURE

Contents. LINQ for Visual C# 2008 i

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Pro ODP.NET for Oracle. Database 11 g. Edmund Zehoo. Apress

Introducing.NET Data Management

.NET Advance Package Syllabus

Windows Database Applications

Microsoft. Microsoft Visual C# Step by Step. John Sharp

2. A GUI A. uses buttons, menus, and icons B. should be easy for a user to manipulate C. both (a) and (b) D. stands for Graphic Use Interaction

Microsoft.NET Programming (C#, ASP.NET,ADO.NET, VB.NET, Crystal Report, Sql Server) Goal: Make the learner proficient in the usage of MS Technologies

Copy Datatable Schema To Another Datatable Vb.net

PESIT- Bangalore South Campus Hosur Road (1km Before Electronic city) Bangalore

> ADO.NET: ActiveX Data Objects for.net, set of components used to interact with any DB/ XML docs

M4.1-R4: APPLICATION OF.NET TECHNOLOGY

MCSA Universal Windows Platform. A Success Guide to Prepare- Programming in C# edusum.com

iseries Access in the.net World

Active Server Pages Architecture

Volume CREATIVE DATA TECHNOLOGIES, INC. DATALAYER.NET. Getting Started Guide

Building Datacentric Applications

PrepKing. PrepKing

.NET 4.6 MICROSOFT CURRICULUM MICROSOFT.NET 2015 (4.6)

Integration Services. Creating an ETL Solution with SSIS. Module Overview. Introduction to ETL with SSIS Implementing Data Flow

.Net Job Assured Course (3 in 1)


COPYRIGHTED MATERIAL. Contents. Part I: C# Fundamentals 1. Chapter 1: The.NET Framework 3. Chapter 2: Getting Started with Visual Studio

(C#) - Pro: Designing and Developing Windows Applications Using the Microsoft.NET Framework 3.5

2.1 Read and Write XML Data. 2.2 Distinguish Between DataSet and DataReader Objects. 2.3 Call a Service from a Web Page

C# Programming Syllabus

The course introduces many of the techniques and technologies employed by modern desktop and enterprise applications, including:

EVALUATION COPY. Unauthorized reproduction or distribution is prohibited. Table of Contents (Detailed)

Microsoft ASP.NET Whole Course Syllabus upto Developer Module (Including all three module Primary.NET + Advance Course Techniques+ Developer Tricks)

Course Description. Audience. Module Title : 20483B: Programming in C# Duration : 5 days. Course Outline :: 20483B ::

Microsoft Programming in C#

Upgrade: Transition Your MCPD Windows Developer Skills to MCPD Windows Developer 3.5 Instructor: Peter Thorsteinson (VB)

Course Outline. Developing Data Access Solutions with Microsoft Visual Studio 2010 Course 10265A: 5 days Instructor Led

"Charting the Course... MOC A Developing Data Access Solutions with Microsoft Visual Studio Course Summary

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

1 Copyright 2011, Oracle and/or its affiliates. All rights reserved.

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

C++ (Non for C Programmer) (BT307) 40 Hours

Transcription:

Technology & Information Management Instructor: Michael Kremer, Ph.D. Class 7 Professional Program: Data Administration and Management BUILDING APPLICATIONS USING C# AND.NET FRAMEWORK (OBJECT-ORIENTED PROGRAMMING, X428.6)

AGENDA 17. Introduction to Database Access 18. Database Programming, Connected Architecture 19. Using Stored Procedures

Introduction to Database Access 17.

17.1 INTRODUCTION TO ADO.NET 209 Namespace ADO.NET to interact directly with machine local and remote databases. ADO (COM technology) and ADO.NET have little in common. ADO was designed for tightly coupled client/server, ADO.NET is designed with the disconnected world in mind (DataSets). Using a DataSet, the calling assembly is able to manipulate and update a DataSet s content while disconnected from the data source. Types in ADO.NET use CLR memory management protocol, adhere to the same type system (classes, interfaces, enums, structures, and delegates), ADO.NET can be accessed by any.net language.

17.1 INTRODUCTION TO ADO.NET 210 Core assembly System.Data.dll Most VS project templates automatically reference this key data access assembly. Individual sub namespaces need to be referenced individually. You can use ADO.NET libraries in three unique manners: Connected Disconnected Entity Framework Connected: Code explicitly connects and disconnects, use connection, command, and data reader objects. Disconnected: In-memory representation of data queried from database.

17.1 INTRODUCTION TO ADO.NET 211 Within DataSet, you can traverse and manipulate contents without incurring the cost of network traffic. ADO.NET Changes can be sent Disconnected Data Set back to the database Connected Data Adapter Consumers using the data adapter. Data Table WinForms Data Relation Entity Framework (EF), Connection Data Reader WebForms using LINQ many low- Server Command level db specifics (SQL Other Data Providers queries) are hidden Database Entity Framework from your view. You can use EF in a connected or disconnected fashion. ADO.NET EF is an ORM (object relational mapping) which creates a higher abstract object model over ADO.NET components (rather than working with command and connection, you work with customers and supplier objects, for example)

17.1 INTRODUCTION TO ADO.NET 212 No single set of classes exists to interact with different types of datasources. Different datasources expose different protocols communication using the appropriate protocol. Provider Namespace Description ADO.NET provides a relatively common way to interact with datasources, but it comes in different sets of libraries. SQL Server System.Data.SqlClient Access to SQL Server databases OLE DB System.Data.OleDb Access to databases that support OLE DB ODBC System.Data.Odbc Access to databases that support ODBC Oracle System.Data.OracleClient Access to Oracle databases There are two central components of ADO.NET classes: the DataSet, and the.net Framework Data Provider. The data provider classes can be used for a connected architecture directly or for the disconnected architecture either through the DataAdapter to fill a DataSet or the DataReader to fill a DataTable object.

Database Programming, Connected Architecture 18.

18.1 DATA PROVIDER OBJECTS 213 Most important ADO.NET objects are connection and command object. Connection Object The connection helps identify the database server, the database name, user name, password, and other parameters that are required for connecting to the data base. A connection object is used by command objects so they will know which database to execute the command on. Command Object You use a command object to send SQL statements to the database. A command object uses a connection object to figure out which database to communicate with. You can use a command object alone, to execute a command directly, or assign a reference to a command object to an SqlDataAdapter, which holds a set of commands that work on a group of data as described below.

18.1 DATA PROVIDER OBJECTS 214 DataReader Object The data reader object allows you to obtain the results of a SELECT statement from a command object. For performance reasons, the data returned from a data reader is a fast forward-only stream of data. This means that you can only pull the data from the stream in a sequential manner. This is good for speed, but if you need to manipulate data, then a DataSet is a better object to work with. DataAdapter Object Data adapter makes it easy for you to work with primarily readonly data or caching data in memory by helping to manage data in a disconnected mode. Data Adapter fills DataSet object when reading and writes in a single batch when persisting changes back to DB.

18.2 CONNECTION OBJECT 215 Instantiate an object from the connection class and provide certain parameters to establish a connection. Connection is a valuable resource,. Stand-alone client does not represent a problem, but when building large enterprise systems or web applications it will create substantial overhead. Syntax: Connection string:

18.2 CONNECTION OBJECT 216 Most modern OLE DB providers (including SQL Server provider) implement connection pooling. If you create database connections, they are held in a pool. When you want a connection for an application, the provider extracts the next available connection from the pool. When your application closes the connection, it returns connection to the pool and makes itself available for the next application that wants a connection. It is recommended not to keep the connections longer than you need to. Therefore, you should: Open a connection when you need it Close it as soon as you have finished with it. Instantiate connection Open connection Pass connection to other ADO.NET objects Perform DB operations Close connection.

18.3 COMMAND OBJECT 217 A SqlCommand object allows you to specify what type of interaction you want to perform with a database. Syntax: Three additional, overloaded constructors: Property Description Method Description Connection The connection used to connect to the database. ExecuteScalar() Executes the query identified by the CommandText CommandText CommandType Parameters A SQL statement or the name of a stored procedure. A member of the CommandType enumeration. Valid values are: Text, StoredProcedure, TableDirect. The collection of parameters for the command object. ExecuteReader() ExecuteNonQuery() property and returns the first column of the first row. Executes the query identified by the CommandText property and returns the result as a SqlDataReader object. Executes the query identified by the CommandText property and returns an integer that indicates the number of rows affected by the operation.

18.3 COMMAND OBJECT 218

18.3 COMMAND OBJECT 219

18.3 COMMAND OBJECT 220 When working with resources, such as the connection object, it is advisable to use the using resource acquisition statement. This construct ensures automatically that the object is closed and disposed of at the end of the using block. Syntax: Internally in C# the using block is transformed into a Try Finally structure where in the Finally block the Dispose method is automatically called.

18.3 COMMAND OBJECT 221 ExecuteScalar method retrieves only the first column of the first row as specified by the SQL statement. If no sort order is specified when retrieving multiple rows, the first row is a random row.

18.3 COMMAND OBJECT 222

18.3 COMMAND OBJECT 223

18.3 COMMAND OBJECT 224

18.4 DATAREADER OBJECT 225 Data readers are objects that implement the System.Data.IDataReaderinterface. A data reader is a fast, unbuffered, forward-only, read-only connected stream that retrieves data on a per-row basis. It reads one row at a time as it loops through a result set. You cannot directly instantiate a data reader; instead, you create one with the ExecuteReader method of a command. Syntax: Loop through the returned results by using Read method. Access columns using various methods, such as ordinal indexer (like in arrays). Close datareader:

18.4 DATAREADER OBJECT 226 Number of methods to retrieve individual column values: Ordinal Indexer method Column Name Indexer method Typed Accessor method Ordinal Indexer Method Use an ordinal indexer to retrieve specific column from result set. Faster than column name indexer Column Name Indexer Method We do not really keep track of column numbers and prefer retrieving values by their respective column names. Typed Accessor Method Data reader stores data locally in.net type rather than original DB type In-place conversion.

18.4 DATAREADER OBJECT 227 In-place type conversion feature is a tradeoff between consistency and speed. To give some control over the data being retrieved, the data reader exposes typed accessor methods that you can use if you know the specific type of the value being returned. Syntax: SQL Server Data Types.NET Type.NET Typed Accessor bigint Int64 Getlnt64 binary Byte[] GetBytes bit Boolean GetBoolean char String or Char[] GetString or GetChars datetime DateTime GetDateTime decimal Decimal GetDecimal float Double GetDouble imageor long varbinary Byte[] GetBytes int Int32 Getlnt32

Using Stored Procedures 19.

19.1 CODING STORING PROCEDURES 228 Not a good idea to write any SQL into your application code: Security concerns (possible SQL injection attacks) Maintenance issues when performing database schema changes SQL Server s compilation and performance-based reuse All Data operations should be performed in stored procedures. For each table, you may need the following operations: List all data (SELECT * FROM table ORDER BY..) List a specific record (SELECT * FROM table WHERE primary key = parameter) Count of records (Select count(*) FROM table) Insert operation (INSERT INTO table Values(parameters) ) Update operation (UPDATE table Set col1=param1, col2=param2, ) Delete operation (DELETE FROM table WHERE primary key = parameter) Additionally, special business rules data processing.

19.1 CODING STORING PROCEDURES 229

19.1 CODING STORING PROCEDURES 230

19.1 CODING STORING PROCEDURES 231

19.2 PARAMETERS AND RETURNING DATA 232 When calling stored procedures from ADO.NET, provide the required parameters. This is done through the parameters collection of the command object. parameters collection is also used for parameterized queries as shown: Instead: Syntax to add parameters to parameters collection: Multiple parameters: Repeat the steps for each parameter. Good idea to abstract this code out into a method and call it repeatedly.

19.2 PARAMETERS AND RETURNING DATA 233 Call this method every time you need to add one or more parameters to the command object.

19.3 EXECUTING STORED PROCEDURES USING ADO.NET 234 Command object s command type by default is Text. When calling stored procedure, set CommandType property to CommandType Description StoredProceure Text The command will execute a direct SQL statement. The SQL statement is provided in CommandText the CommandText property. This is the default value. StoredProcedure The command will execute a stored procedure in the data source. The CommandText property now property provides the name of the stored procedure. must contain TableDirect The command will query all the records in the table. The CommandText is the name of stored backward compatibility with certain OLE DB drivers only. It is not supported by the SQL procedure name Server data provider, and it will not perform as well as a carefully targeted query.) (rather than SQL statement). Command type values are encapsulated in a C# enumeration object, also called enum. Syntax for calling stored procedure: If you need to pass parameters into a stored procedure, you need to add them to the command object as shown in the previous chapter. Finally, you call the ExecuteNonQuery method on the command object. the table from which the command will retrieve the records. (This option is included for

19.3 EXECUTING STORED PROCEDURES USING ADO.NET 235

19.4 RETURNING DATA FROM A STORED PROCEDURE A stored procedure in SQL Server can return data in three different ways: Through an In/Out or Out parameter Through the return variable (using RETURN keyword) Through a result set object (SELECT statement) 236 For out parameters in a stored procedure, adjust the method for adding parameters to the command object. In addition to parameter name, value and data type you need parameter direction. Parameter Direction Input InputOutput Output ReturnValue Description The parameter is an input parameter. (default) The parameter is capable of both input and output. The parameter is an output parameter. The parameter represents a return value from an operation such as a stored procedure, built-in function, or user-defined function using the RETURN keyword. This is an integer data type only!

19.4 RETURNING DATA FROM A STORED PROCEDURE To assign the value of an output/return type parameter back into a variable in C#, you need to cast the value into the correct data type since the parameter.value is of type object. Syntax: 237

238 19.4 RETURNING DATA FROM A STORED PROCEDURE

239 19.4 RETURNING DATA FROM A STORED PROCEDURE

240 19.4 RETURNING DATA FROM A STORED PROCEDURE

241 19.4 RETURNING DATA FROM A STORED PROCEDURE