Copyright ©2004 Virtusa Corporation | CONFIDENTIAL ADO.Net 2.0 - Basics Ruwan Wijesinghe Trainer.

Slides:



Advertisements
Similar presentations
Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control.
Advertisements

ADO vs ADO.NET ADOADO.NET Client/server coupledDisconnected collection of data from data server Uses RECORDSET object (contains one table) Uses DATASET.
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.
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.
Performed by:Gidi Getter Svetlana Klinovsky Supervised by:Viktor Kulikov 08/03/2009.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
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.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
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.
Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.
Overview of Data Access MacDonald Ch. 15 MIS 324 Professor Sandvig.
Developing Web Applications Using Microsoft ® Visual Studio ® 2008.
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.
ADO.Net CS795. What is ADO.Net? Database language spoken by managed applications ADO.net database accesses go through modules: data providers –SQL Server.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.
MySQL Connection using ADO.Net Connecting to MySQL from.NET Languages.
1 Introduction to ADO.NET Microsoft ADO.NET 2.0 Step by Step Rebecca M Riordan Microsoft Press, 2006.
Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010.
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.
10/26/20151 Chapter 3 Introduction to ADO.NET In this chapter, you will: Learn the basic classes in ADO.NET and its architecture Learn the different 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.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.
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.
JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.
Why ADO.NET Not your father’s Data Access.
Mauricio Featherman, Ph.D. Washington St. University
Why ADO.NET Not your father’s Data Access.
ADO.Net CS795. What is ADO.Net? Database language spoken by managed applications ADO.net database accesses go through modules: data providers –SQL Server.Net.
Module 3: Working with Local Data. Overview Using DataSets Using XML Using SQL Server CE.
HNDIT Rapid Application Development
Databases – Unit 3 & 4.  SQL is not restricted to a specific database, however in class we will be looking at the two major database programmes in use.
ADO.NET FUNDAMENTALS BEGINNING ASP.NET 3.5 IN C#.
Module 4 Introduction ADO.NET.
1 Database Programming with ADO.NET Kashef Mughal.
C# .NET Software Development
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.
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
C# 1 CSC 298 ADO.NET. C# 2 ADO.NET  A data access technology that maps very well to the world of the web (disconnected architecture)  data is retrieved.
 ADO.NET is an object-oriented set of libraries that allows you to interact with data sources  Commonly, the data source is a database, but it could.
Integrating Data Lesson 6.
ASP.NET Programming with C# and SQL Server First Edition
Data Access with ADO.NET
Introduction to ADO.NET
Introduction to Database Processing with ADO.NET
ADO.NET and Stored Procedures
Lecture 6 VB.Net SQL Server.
ADO.Net and Stored Procedures
An Introduction to Entity Framework
MySQL Connection using ADO.Net
Database Objects 1/12/2019 See scm-intranet.
Chapter 10 ADO.
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Presentation transcript:

Copyright ©2004 Virtusa Corporation | CONFIDENTIAL ADO.Net Basics Ruwan Wijesinghe Trainer

2 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Introduction ADO.NET is the library in.NET Framework, used to access Data. This is a fully object oriented, XML based library which provides a consistent user interface for any Data Source, including XML.

3 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Design Goals Use existing ADO experience. Support for N-Tire distributed applications (XML based serialization and disconnected datasets). Integrated XML support (Enables to access XML data using familiar Relational Database Structure).

4 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Architecture Separate Data Access from Data Manipulation. DataSet DbConnection DbParameters DbCommand DbDataReader DbDataAdapter Select Command Insert Command Delete Command Update Command Fill Update

5 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Data Access Classes DbConnection This is the link between the database and the application. A connection string is used to identify the database to connect. DbCommand This class is used to execute SQL commands, on the database. This class uses connection object to access the database. DbDataReader This class is used to retrieve data from a database. Data access is read-only and forward-only. DbDataAdapter This class is used to fill and update DataSets. This class uses Command objects to send and receive data.

6 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Connection String The connection string is a list of parameters, separated by “;” character. Some important parameters are given below. Provider – The Ole-DB database driver name (used only in Ole-DB connections. Data Source – The source of data. This value depends on the database server. This is the server name for SQL Server database and database file name for Microsoft Access. Initial Catalog – This is the initial database name. This parameter is not used with Microsoft Access connection. Connection Timeout / Connect Timeout – The duration which the connection should keep trying to establish the connection. The default value is 15s. User ID – The user account, which is used to access the database Password – The password for the above user account

7 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Database Connections Important facts about connections Number of connections that can be opened simultaneously is limited. Opening a connection is a quite expensive process. Therefore the connections that you made using the same connection string are pooled by ADO.NET. When we open a connection, ADO.NET will return a connection from the pool, if available. Since the number connections in the pool is limited, release the open connections by calling Close method, as soon as possible, so that they will be available for other work.

8 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Get Factory Class for the Desired Provider ADO.Net Support for writing Database independent Database Access Code This is achieved using Factory classes All these database independent data access classes are defined in "System.Data.Commen" namespace Creating the Factory DbProviderFactory factry = DbProviderFactories.GetFactory (stProvidername); Provider Name for SQL client is, "System.Data.SqlClient", this is similar to the namespace name in which SQL client classes are defined.

9 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Creating New Connections and Commands and Parameters Creating Connections DbConnection conn = factry.CreateConnection(); conn.ConnectionString = stConnectionString; Creating Commands DbCommand cmd = factry.CreateCommand(); cmd.Connection = conn; cmd.CommandText = stSQL; Creating Parameters DbParameter para = factry.CreateParameter(); para.ParameterName = para.DbType = DbType.Int32; para.Value = id; cmd.Parameters.Add(para);

10 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Execute SQL Command that does not Return any Value Execute an SQL Command that does not return any value try{ conn.Open(); cmd.ExecuteNonQuery(); } finally{ conn.Close(); }

11 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Execute an SQL Command that Returns Values 1 try { conn.Open(); DbDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { int id = reader.GetInt32(0); string name = reader.GetString(1); double height = reader.GetDouble(2); // Process the values } finally { conn.Close(); }

12 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Execute an SQL Command that Returns Values 2 try { conn.Open(); DbDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { int id = (int) reader["id"]; string name = (string) reader["name"]; double height = (double) reader["height"]; // Process the value } finally { conn.Close(); }

13 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Data Cache DataSets and DataTables are used to temporarily cache the data received from the database DataTable is used to hold the data received from a single SQL query. This corresponds to a table in a database. DataSet is a collection of DataTables. In addtion to that, dataset can contain relationships between tables and constraints. This corresponds the whole database.

14 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Fill a DataTable Fill a DataTable Directly DbDataAdapter adp = factory.CreateDataAdapter(); adp.SelectCommand = cmd; DataTable dt = new DataTable(); adp.Fill(dt); Fill a DataTable in a DataSet DbDataAdapter adp = factory.CreateDataAdapter(); DataSet ds = new DataSet(); adp.Fill(ds,"Students"); return ds;

15 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Using Typed DataSets and Typed DataTables We can use Visual Studio IDE to generate Typed DataSets, which can be used to do type verifications in compilation time. Typed DataSets generated using IDE has Typed DataTables to hold the data belongs to each table handled by the DataSet. Each Typed DataTable in a Typed DataSet has a separate typed DataAdapter class to operate on them. These Typed DataAdapters includes methods to fill and update data in this Typed DataTables In addition to the Fill and Update methods operate on DataTables, we can make IDE to include direct database access functions (GetData, Insert, Delete and Update) to the Typed DataAdapters

16 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Using Typed Adapters 1 Returning a Typed DataTable public DataSet.StudentsDataTable GetAllStudentsDT() { MyDataSetTableAdapters.StudentsTableAdapter adp = new MyDataSetTableAdapters.StudentsTableAdapter(); return adp.GetData(); } Filling a Typed DataTable in a Typed DataSet MyDataSetTableAdapters.StudentsTableAdapter adp = new MyDataSetTableAdapters.StudentsTableAdapter(); MyDataSet ds = new MyDataSet(); adp.Fill(ds.Students);

17 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Using Typed Adapters 2 Create an instance of the Typed Adapter MyDataSetTableAdapters.StudentsTableAdapter adp = new MyDataSetTableAdapters.StudentsTableAdapter(); Delete a Row int id = 5; adp.Delete(id); Update a Row int original_id = 50; //the current id in the database (before update) int new_id = 7; // the next value of the id (after update) string name = "aaa"; double? height = null; //Leave this field blank (unassigned) adp.Update(new_id, name, height, null, original_id);

18 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Processing the Values in a Typed DataSet Calculating the average Height MyDataSet ds = new MyDataSet(); adp.Fill(ds.Students); double total = 0; int count = 0; foreach (MyDataSet.StudentsRow srow in ds.Students) { if (! srow.IsHeightNull()) // This is important to prevent exceptions { total += srow.Height; count++; } double average = total / count;

19 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Reading XML Files DataSets can directly read XML files and map there hierarchical structure in to its relational tables. E.g DataSet ds = new DataSet("MyDS"); ds.ReadXml("E:\\Temp\\test1.xml",XmlReadMode.Auto); dataGrid1.DataSource = ds; Sample XML File These files will create, A table named “Book” (fields “id” and “name”) A table named “Author” (field “name”) A relationship between two tables The data-grid will display the data in a hierarchical way

20 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Write to XML File We can save the data in a DataSet into an XML file using WriteXML method. The DataSet Name (“MyDS” in this example”) will be the root element of the xml file In order to insure that the relationship is implemented as nested tags in xml file, make sure that the Nested property of the relationship object is set to true. E.g. DataSet ds = new DataSet("MyDs"); sqlDataAdapter1.Fill(ds,"tblStudents"); sqlDataAdapter2.Fill(ds,"tblMarks"); ds.Relations.Add( "MarksOfStudent", ds.Tables["tblStudents"].Columns["id"], ds.Tables["tblMarks"].Columns["stid"]).Nested=true; ds.WriteXml("E:\\Temp\\test2.xml"); We can write the xml file together with the xml Schema infomration like this, ds.WriteXml ("E:\\Temp\\test2.xml", XmlWriteMode.WriteSchema); However this method will not preserve the order of rows as in the database. To keep this information as well, we can save the file as a DiffGram ds.WriteXml ("E:\\Temp\\test2.xml", XmlWriteMode.DiffGram);