1 UCN Technology: Computer Science: Autumn 2012 ADO.NET 2.0 Architecture DataReader DataSet Connection factory Config file.

Slides:



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

Direct Data Access, Data Binding. Content Direct Data Access Data Binding Muzaffer DOĞAN - Anadolu University2.
By Chris Pascucci and FLF
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.
1 ADO.NET. 2.NET Framework Data Namespaces System.Data –Base set of classes and interfaces for ADO.NET System.Data.Common –Classes shared by the.NET Data.
C# Programming: From Problem Analysis to Program Design1 Working with Databases C# Programming: From Problem Analysis to Program Design 3 rd Edition 14.
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.
Objective In this session we will discuss about : What is ADO. NET ?
Lecture 11: Programmatic Database Access with ADO.NET.
Overview of Database Access in.Net Josh Bowen CIS 764-FS2008.
C# programming with database Basic guideline. First step Install SQL Server 2008/2010 (Professional edition if possible) Install Visual Studio 2008/2010.
ADO.NET – part II August 2004 [ Marmagna Desai]. CONTENTS ADO vs ADO.NET ADO.NET – Managed providers Connecting to Database SqlConnection Selecting Database.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
ODBC, OLE DB, and ADO Introduction Dr. Ron Eaglin.
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.
FEN Data connection DataReader DataSet Bonus info: Concurrency and Database Transactions Embedded SQL.
Developing Web Applications Using Microsoft ® Visual Studio ® 2008.
Accessing SQL Server and OLE DB from.NET Svetlin Nakov Telerik Corporation
A Simple Introduction. What is ADO.net? First the word ADO stands for ActiveX Data Objects And it is an integral part of.Net Framework of Microsoft hence.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
PART 1 CREATING THE PRODUCT CATALOG. ROADMAP FOR THIS CHAPTER To implement the departments list, you’ll start with the database and make your way to the.
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.
.NET Data Access and Manipulation ADO.NET. Overview What is ADO.NET? Disconnected vs. connected data access models ADO.NET Architecture ADO.NET Core Objects.
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.
Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010.
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL ADO.Net Basics Ruwan Wijesinghe Trainer.
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.
ASP.NET Rina Zviel-Girshin Lecture 5
Module 9: Accessing Relational Data Using Microsoft Visual Studio.NET.
Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET.
Module 7: Accessing Data by Using ADO.NET
Christopher M. Pascucci.NET Programming: Databases & ADO.NET.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Introduction to ADO.NET ADO.NET - Lesson 01  Training time: 10 minutes  Author:
1 UCN Technology: Computer Science: Autumn 2012 ADO.NET 2.0 Architecture DataReader DataSet Connection factory Config file.
1.Net programmingADO.NETNOEA / PQC ADO.NET Architecture Connection factory Config file DataReader DataSet DataGrid Autogeneration of SQL.
Presented by Joseph J. Sarna Jr. JJS Systems, LLC
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
An Introduction to ADO.Net Marmagna Desai.NET Seminar, Fall-2003.
 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.
C# Programming in Depth Prof. Dr. Bertrand Meyer March 2007 – May 2007 Chair of Software Engineering Lecture 10: Database Lisa (Ling) Liu.
The Problems HTTP is disconnected So many database vendors Create a simple consistent versatile interface on the data Look at ADO.NET classes OleDb SQL.
CIS 375—Web App Dev II ASP.NET 9 Database. 2 ADO.NET What is ADO.NET? ADO.NET is a part of the.NET Framework ADO.NET consists of a set of classes used.
COP2360 – C# Programming Chapter 14 – Nov 25, 2015.
HNDIT Rapid Application Development
Database Connectivity with ASP.NET. 2 Introduction Web pages commonly used to: –Gather information stored on a Web server database Most server-side scripting.
ADO.NET FUNDAMENTALS BEGINNING ASP.NET 3.5 IN C#.
Module 4 Introduction ADO.NET.
C# .NET Software Development
NOEA/IT - FEN: Databases/Transactions1 Transactions ACID Concurrency Control.
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.
Querying Information in a Database. CONTENTS Relational Database Systems Creating Database with SQL Server Reading Data with LINQ Requirements to Get.
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.
.NET Data Access and Manipulation
ASP.NET Programming with C# and SQL Server First Edition
Data Access with ADO.NET
Introduction to ADO.NET
Accessing Databases using Ado.net
Introduction to Database Processing with ADO.NET
Introduction to Database Processing with ADO.NET
ADO.NET Framework.
Programming the Web Using ASP.Net
Lecture 6 VB.Net SQL Server.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Chapter 10 ADO.
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

1 UCN Technology: Computer Science: Autumn 2012 ADO.NET 2.0 Architecture DataReader DataSet Connection factory Config file

2 UCN Technology: Computer Science: Autumn 2012 Data access Swap?

3 UCN Technology: Computer Science: Autumn 2012 Data Providers OLE DB: A COM object, that maps the OLE DB api to the DBMS' api Is Microsoft propritary. ODBC: Works similar to a printer driver undependent of DBMS and OS.NET Data provider: A set of classes that implements a set of interfaces and abstract classes Some are provided by Microsoft and are installed with the.Net framework. Others are written by the DBMS provider

4 UCN Technology: Computer Science: Autumn 2012 Microsoft Data Providers Data Provider Namespace Assembly OLE DBSystem.Data.OleDbSystem.Data.dll Microsoft SQL ServerSystem.Data.SqlClientSystem.Data.dll Microsoft SQL Server MobileSystem.Data.SqlServerCeSystem.Data.SqlServerCe.dll ODBCSystem.Data.OdbcSystem.Data.dll OracleSystem.Data.OracleClientSystem.Data.OracleClient.dll MS Access

5 UCN Technology: Computer Science: Autumn 2012 Other providers Data ProviderWebsite Firebird Interbase IBM DB2 Universal Databasehttp://www-306.ibm.com/software/data/db2 MySQLhttp://dev.mysql.com/downloads/connector/net/1.0.html PostgreSQLhttp:// Sybasehttp:// Oracle (proprietary)

6 UCN Technology: Computer Science: Autumn 2012 Classes and Interfaces ObjectBase ClassImplemented Interfaces ConnectionDbConnectionIDbConnection- Connection CommandDbCommandIDbCommand- SQL command DataReaderDbDataReaderIDataReader- Forward reader DataAdapter DbDataAdapter IDataAdapter- Used with Dataset ParameterDbParameterIDataParameter- Parameters for Command TransactionDbTransactionIDbTransaction- Transaction

7 UCN Technology: Computer Science: Autumn 2012 Two ways of DB access Connected: –Open connection. –Read/Write access (select, insert, update and delete) using a Command object. –When reading (select) a DataReader object is returned. A DataReader is an iterator (cursor) into the result table. –Close Connection. Disconnected: –Fill a DataSet object (a copy of a part of the database) using a DataAdapter. –DataAdapter wraps SQL-statement(s). –A DataSet object contains DataTable objects. –DataTable objects contain collections of rows and columns.

8 UCN Technology: Computer Science: Autumn 2012 Connection vs. Connectionless Connection: –Open Connection –Execute DB operations –Close Connection –Working on actual (live) data –Other applications can not access data.

9 UCN Technology: Computer Science: Autumn 2012 Connectionless: –Create a copy of a part of the database –Execute DB operations on the copy –Other applications may change date –The copy may be come inconsistent. Connection vs. Connectionless Data are changed in the local copy: –at update it is checked if the data in the database have been modified by others –in that case the update is rejected (ConcurrencyException).

10 UCN Technology: Computer Science: Autumn 2012 Overview of using databases 4 steps: 1.Open connection to database 2.Execute SQL for updating DB or fetching records 3.Handle data 4.Close connection

11 UCN Technology: Computer Science: Autumn 2012 Step 1: Open Connection Connection are opened according to connection string info –here is a connection to a MS Access database opened –The database is located means that escape characters are discarded string sConnection; sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data OleDbConnection dbConn; dbConn = new OleDbConnection(sConnection); dbConn.Open(); connection

12 UCN Technology: Computer Science: Autumn 2012 Open Connection Connections are opened using a connection string –May be found using Visual Studio: // Create and open a connection. SqlConnection cn = new SqlConnection(); cn.ConnectionString = " Data Source =PCM06073\\SQLEXPRESS;” +”Initial Catalog=vw;Integrated Security=True;"; cn.Open(); ShowConnectionStatus(cn); // or should it be? cn.ConnectionString = “ Server =PCM06073\\SQLEXPRESS;” +”Initial Catalog=vw;Integrated Security=True;"; cn.Open(); Data Source or Server? Server is preferable.

13 UCN Technology: Computer Science: Autumn 2012 Connection Strings Connection strings are product specific (DB specific) anf often very well-documented. Help may be found at: – – In many cases VS can help.

14 UCN Technology: Computer Science: Autumn 2012 Step 2: Get / fetch data Data are fetched by creating a Command object and use it to execute a SQL statement. Data can be stored in either a DataReader object or a DataSet object.

15 UCN Technology: Computer Science: Autumn 2012 Differences between DataReader and DataSet/DataAdapter DataReader can only be used for reading data. It can only be traversed once (forward). DBCommand can update the database by ExecuteNonQuery. This update is executed immediately. DataAdapter is the connection between DataSet and database. Data are fetched to the DataSet, might be modified and sent back to the database. Updates are executed on a local copy. Concurrency problems must be handled. Possible to traverse forward and backward. A DataSet can contain multiple tables.

16 UCN Technology: Computer Science: Autumn 2012 Step 2: Get records Method 1: DataSet Get records by SQL Select query and DataAdapter –Data can be read and changed string sql; sql = " Select lastname, firstname" + " From employee" + " Order By lastname Asc, firstname Asc;"; OleDbCommand dbCmd; dbCmd = new OleDbCommand(dbConn, sql); adapter.SelectCommand = dbCmd; dataset =new DataSet("MyTable"); adapter.Fill(dataset); dataset record

17 UCN Technology: Computer Science: Autumn 2012 Step 2: Get records Method 2: DataReader Get records via SQL Select query –read-only access to the database string sql; sql = " Select lastname, firstname" + " From employee" + " Order By lastname Asc, firstname Asc;"; OleDbCommand dbCmd; dbCmd = new OleDbCommand(sql, dbConn); OleDbDataReader dbReader; dbReader = dbCmd.ExecuteReader(); data reader record

18 UCN Technology: Computer Science: Autumn 2012 What is achieved? We have created a connection to a database. The connection is placed in the connection object. We have done a search by using a SQL-statement. The search was executed by using a command object. The result of the search was stored in a DataSet or as here a DataReader object. Now it is possible to get the data from this object for viewing, passing on to client or handle in other ways. Example:..\vwReader..\vwReader

19 UCN Technology: Computer Science: Autumn 2012 Factory Problem: Unlike ODBC, knowledge of the implementation might be spread to many places in the application. That's because of the set of dedicated classes The solution is to use a factory (from factory pattern) In ADO.NET 2.0, it is implementet in the framework. Use factory to create a Connection and a Command object Example: \ReaderWithConfig\ReaderWithConfig

20 UCN Technology: Computer Science: Autumn 2012 Use factory to create Connection and Command objects Here is no references in the source code to concrete classes provider and connectionString may be placed in the config file string provider = "System.Data.OleDb"; string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=firma.mdb"; DbProviderFactory factory= DbProviderFactories.GetFactory(provider); DbConnection conn=factory.CreateConnection(); conn.ConnectionString=connectionString; conn.Open(); Console.WriteLine(conn.State); DbCommand command = factory.CreateCommand(); command.CommandText = "SELECT * FROM employee"; command.Connection = conn; DbDataReader dataReader = command.ExecuteReader(); while (dataReader.Read()){ Console.WriteLine(dataReader[1]); }

21 UCN Technology: Computer Science: Autumn 2012 Connection strings Connection strings depends on the provider and are not well documented Where to get help? – – –Providers documentation Visual Studio can generate some of them

22 UCN Technology: Computer Science: Autumn 2012.Config fil Configuration files are used several places in.Net i.e in ASP.NET for setting site properties and security In Remoting for setting remote object properties In ADO.NET to set provider and connection string You might also use own application dependent properties In a.Net application the config file should be called, i.e test.exe.config or test.dll.config If you call it app.config in Visual Studio, it automatically copied and renamed when the application is built. In ASP.NET it must be named web.config. You might define one in different sub folders, but the security part (authentification e.g.) must be in the root folder of the site.

23 UCN Technology: Computer Science: Autumn 2012.config fil and ADO.NET Example of SQLExpress connection string: <add name="TestConnection" connectionString="Data Source=pcm06463\sqlexpress;Initial Catalog=dbtest; User ID=test;Password=" providerName="System.Data.SqlClient" /> Machine name Nessesary for sqlexpress Database Internal name

24 UCN Technology: Computer Science: Autumn 2012 Get provider and connection string From ConfigurationManager:.... string provider = ConfigurationManager.ConnectionStrings["TestConnection"].ProviderName; string connStr = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;..... Example: \ReaderWithConfig\ReaderWithConfig Example: \ReaderWithConfig2\ReaderWithConfig2

25 UCN Technology: Computer Science: Autumn 2012 Parameters in SQL statements Makes it possible to use the same SQL-statement in multible situations. But harder to debugge string sqlString = "INSERT INTO datotest Values SqlParameter sqlParam = new System.Data.SqlDbType.DateTime); sqlParam.Value = DateTime.Now; SqlCommand comm = _conn.CreateCommand(); comm.CommandText = sqlString; comm.Parameters.Add(sqlParam); int result = comm.ExecuteNonQuery(); ExecuteNonQuery is used for insert, delete and update

26 UCN Technology: Computer Science: Autumn 2012 Transaction - Definition September 2010 UCN T&B - CBH (FEN) Databases/Transactions 26 A transaction is an operation on data in the database. A transaction may be composed of several database operations, but is viewed as a logical unit of work A transaction must be done completely or not done at all A transaction must have the ACID properties: –A: Either it is done in total or it is not done at all (Atomicity) –C: The database moves from one consistent state to an other consistent state (Consistency) –I: If more operations are accessing the same data, they are not to disturb each other – they must execute as if they executed alone (Isolation) –D: When a transaction completes, its changes to the database are permanent (Durability)

27 UCN Technology: Computer Science: Autumn 2012 Transactions – example: T1 and T2 are executing concurrently T1: Transfers N DKKs from account X to account Y: read_item(X); X:= X-N; write_item(X); read_item(Y); Y:= Y+N; write_item(Y); T2: Deposits M DKK on account Y: read_item(Y); Y:= Y+M; write_item(Y); Any possible problems? time

28 UCN Technology: Computer Science: Autumn 2012 Transactions – Problems We want several transactions to execute concurrently (Why?) Three types of problems: –lost update –uncommitted dependency (temporary update) –inconsistent analysis (incorrect summary) Crash during execution of a transaction must be handled

29 UCN Technology: Computer Science: Autumn 2012 Lost Update

30 UCN Technology: Computer Science: Autumn 2012 Uncommitted Dependency

31 UCN Technology: Computer Science: Autumn 2012 Inconsistent Analysis

32 UCN Technology: Computer Science: Autumn 2012 Transactions In C#: Bank Transactions Example: demos\Transactions\BankTransaction1demos\Transactions\BankTransaction1

33 UCN Technology: Computer Science: Autumn 2012 Multiple Databases Moving from (badly designed) database to another (better designed) database: Example: demos\flytDataAppdemos\flytDataApp