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.

Slides:



Advertisements
Similar presentations
ADO.Net Modelo para acesso a dados nas aplicação.Net Componentes: DataSet.Net Data providers: Connection Command DataReader DataAdapter.
Advertisements

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.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
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.
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 Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
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.
Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.
ADO.NET – part II August 2004 [ Marmagna Desai]. CONTENTS ADO vs ADO.NET ADO.NET – Managed providers Connecting to Database SqlConnection Selecting Database.
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
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.
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)
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.
1 Introduction to ADO.NET Microsoft ADO.NET 2.0 Step by Step Rebecca M Riordan Microsoft Press, 2006.
Chapter 3 Introduction to ADO.NET 3.1 The ADO and ADO.NET ActiveX Data Object (ADO) is developed based on Object Linking and Embedding (OLE) and Component.
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.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.
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.
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.
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.
ADO.NET in VB.NET 2005 ITE 370 4/26/2017.
C# .NET Software Development
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
Active Data Objects Using.Net ADO.Net Farooq Ahmed Amna Umber Summayya Shehzad.
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
Introduction to Database Processing with ADO.NET
Introduction to Database Processing with ADO.NET
ADO.NET Framework.
Lecture 6 VB.Net SQL Server.
Active Data Objects Binding ASP.NET Controls to Data
VB.NET Using Database.
Bài 5: ADO.NET 5.1. Khái niệm 5.2. Các lớp và không gian tên cơ bản của ADO.NET 5.3. Thao tác với dữ liệu kết nối – Connected 5.4. Thao tác với dữ liệu.
Browser (Client Side) 瀏覽器 (客戶端)
Chapter 10 ADO.
Active Data Objects Binding ASP.NET Controls to Data
Introduction to Database Programming through ADO. NET
Presentation transcript:

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 Providers System.Data.OleDb –Classes that make up the.NET Data Provider for OLEDB System.Data.SqlClient –Classes that make up the.NET Data Provider for SQL Server System.Data.SqlTypes –Classes that represent the SQL data types System.XML –Classes for working with XML data

3.NET Data Providers (1) A Data Provider supports data access through its own specific implementation that supports a common set of interfaces Two standard.NET Data Providers SQL provider – for MS SQL Server 7.0 or later OLE DB provider – for OLE DB support, e.g. Oracle, OLE DB.NET Data Provider as a wrapper around an OLE DB provider Each.NET Data Provider is implemented as a group of types that reside in System.Data.SqlClient and System.Data.OleDb namespaces

4.NET Data Providers (2) OLE DB Provider ODBC Provider SQL.NET Data Provider Client OLE DB.NET Data Provider Other DBMS SQL Server Managed Code

5.NET Data Providers (3) Some fundamental classes supported by any.NET Data Provider Connection – allows establishing and releasing connections, and to begin transactions Command – allows storing and executing a command (SQL query, stored procedure) DataReader – provides direct, sequential (forward-only), read-only access to data in a database DataAdapter – built on DataReader, this class creates and populates instances of the class DataSet. DataSets allow more flexible access to data than using just DataReader

6.NET Data Providers (4) Clients can access data through a DataReader (straightforward one- row-at-a-time) or by using a DataSet (more complex requirements such as ordering, filtering, sending results across a network etc.) Rows DataSet Connection Command DataReader DataAdapter DBMS Client.NET Data Provider

7 Accessing Data with Reader 1.Create a Connection object (of class SqlConnection or OleDbConnection) set ConnectionString property 2.Create a Command object (of class SqlCommand and OleDbCommand) by Connection object’s CreateCommand method Set CommandText property 3.Call the Open() method of the Connection object 4.Declare a DataReader (if need) 5.Executing the query by calling methods of the Command object ExecuteReader(): returns a DataReader, can be accessed one row at a time ExecuteScalar(): returns a single value, e.g. result of SUM function ExecuteNonQuery(): returns the number of rows affected 6.Process the result 7.Close the DataReader (if using ExecuteReader) 8.Close the Connection with the Close() method

8 AdoExample1 Sub Main() Dim query As String = "SELECT StudentID, Name, Sex FROM Students" Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=school.mdb" Dim conn As OleDbConnection = New OleDbConnection(connStr) Dim command As OleDbCommand = New OleDbCommand(query, conn) Dim reader As OleDbDataReader Try conn.Open() ' open connection reader = command.ExecuteReader() ' execute the command While reader.Read()‘ process records Console.WriteLine("ID: {0}, Name: {1}", reader.GetString(0), reader.GetString(1)) End While Catch ex As OleDbException Console.Out.WriteLine(ex.Message) Finally reader.Close() conn.Close() End Try End Sub

9 Accessing Data with DataSets (1) A DataSet is an in-memory cache for data Disconnected: manipulate data without connecting the database, allow you to move data across a network DataSets allow much more flexible access to data, can examine data in an arbitrary way, scrolling back and forth Datasets are used in conjunction with DataAdapters. DataAdapters populate DataSets with data from data stores. Useful for combining data from different data sources, and for data transfer across a network (as DataSets are serializable)

10 Accessing Data with DataSets (2) DataTable DataRelation Each DataSet can contain zero or more DataTable objects. Each DataTable can contain the result of some query A DataSet can also maintain relationships among DataTables using DataRelation objects Each DataSet has a schema, describing the tables, columns, data types

11 Creating and Using DataSets 1.Create a Connection object set ConnectionString property 2.Create a Command object Set CommandText property 3.Create a DataAdapter object (SqlDataAdapter, OleDbDataAdapter) Set the SelectCommand, InsertCommand, UpdateCommand, DeleteComand property to the Command object created in last step 4.Create a DataSet object 5.Call the Open() method of the Connection object 6.Use the Fill() method of the DataAdapter object to fill the DataSet 7.Close the Connection with the Close() method 8.You may then update/delete rows in DataSet and use the Update() method of the DataAdapter to update the database

12 AdoExample3 Sub Main() Dim query As String = "SELECT StudentID, Name, Sex FROM Students" Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=school.mdb" Dim conn As OleDbConnection = New OleDbConnection(connStr) Dim command As OleDbCommand = New OleDbCommand(query, conn) Dim da As OleDbDataAdapter = New OleDbDataAdapter(command)‘ set the SelectCommand as well Dim ds As DataSet = New DataSet Dim table As DataTable Dim row As DataRow Dim rowIndex As Integer Try conn.Open() ' open connection da.Fill(ds, “Students”)‘ fill the dataset conn.Close()‘ connection can be closed (disconnected) table = ds.Tables.Item(“Students”)‘ select the DataTable For rowIndex = 0 To table.Rows.Count - 1 row = table.Rows.Item(rowIndex) Console.Out.WriteLine("ID: {0}, Name: {1}", _ row("StudentID"), row("Name")) Next Catch ex As OleDbException Console.Out.WriteLine(ex.Message) End Try End Sub

13 DataAdapter Properties SelectCommand –contains a Command object that can be used to populate a DataTable within DataSet –Command object typically references a SQL SELECT statement InsertCommand –to insert rows added to a DataTable into an underlying database –Command object typically references a SQL INSERT statement UpdateCommand –to update a database based on changes made to a DataTable –Command object typically references a SQL UPDATE statement DeleteCommand –to delete rows in a database based on deletions made to a DataTable –Command object typically references a SQL DELETE statement

14 DataAdapter Methods Fill –Used to execute a query (in the SelectCommand) and store the result in a DataSet –Da.Fill(Ds, “TableName”) –Note that the connection can be closed once the Fill method is done Update –Used to modify data in the database based on changes made to the DataTables

15 DataSet Contents Contents of a DataSet are grouped into collections DataSet has a Tables property –a collection of DataTable –table = ds.Tables.Item(“Students”) –table = ds.Tables.Item(0) DataTable has a Rows property –a collection of DataRow –row = table.Rows.Item(rowIndex) –Column values can be obtained by row("StudentID") or row(0) Relations collection

16 Basic Objects Dim query As String = "SELECT * FROM Students" Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=school.mdb" Dim conn As OleDbConnection = New OleDbConnection(connStr) Dim command As OleDbCommand = New OleDbCommand(query, conn) Dim da As OleDbDataAdapter = New OleDbDataAdapter(command) ' commandBuilder is used create the Commands automatically! Must be for UPDATE Dim autogen As New OleDbCommandBuilder(da)

17 Adding Data Using a DataSet Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Dim ds As DataSet = New DataSet Dim table As DataTable Dim row As DataRow Try da.Fill(ds, "Students") ' fill the dataset table = ds.Tables.Item("Students") ' select the DataTable row = table.NewRow() row("StudentID") = "A90001" row("Name") = "Chan Chan" row("Sex") = "M" row("DateOfBirth") = #10/31/2005# row("PhoneNo") = " " row("Class") = "41111" table.Rows.Add(row) da.Update(ds, "Students") Catch ex As OleDbException Console.Out.WriteLine(ex.Message) End Try End Sub

18 Updating Data Using a DataSet Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Dim ds As DataSet = New DataSet Dim table As DataTable Dim row As DataRow Dim rowIndex As Integer Try da.Fill(ds, "Students") ' fill the dataset table = ds.Tables.Item("Students") ' select the DataTable For rowIndex = 0 To table.Rows.Count - 1 row = table.Rows.Item(rowIndex) If row("StudentID") = "A90001" Then row("Sex") = "F" End If Next Catch ex As OleDbException Console.Out.WriteLine(ex.Message) End Try da.Update(ds, "Students") End Sub

19 Deleting Data Using a DataSet Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click Dim ds As DataSet = New DataSet Dim table As DataTable Dim row As DataRow Dim rowIndex As Integer Try da.Fill(ds, "Students") ' fill the dataset table = ds.Tables.Item("Students") ' select the DataTable For rowIndex = 0 To table.Rows.Count - 1 row = table.Rows.Item(rowIndex) If row("StudentID") = "A90001" Then row.Delete() End If Next Catch ex As OleDbException Console.Out.WriteLine(ex.Message) End Try da.Update(ds, "Students") End Sub

20 AdoExample4 Add/Update/Delete

21 Binding a DataSet to a Control Setup the connection and dataadapter Select the DataAdapter, generate the DataSet Set the DataSource property of the control –e.g. datagridview1.DataSource = myDataTable AdoExample5

22 Using SQL to Query Data (1) ‘ Assume connection object is setup already Dim sql As String = “SELECT * FROM STUDENTS” Try conn.Open() Dim da As New OleDbDataAdapter(sql, conn) Dim ds As New DataSet da.Fill(ds, "table") DataGrid1.DataSource = ds DataGrid1.DataMember = "table" Catch e As Exception MsgBox("Error in executing the following SQL statement:" & vbCrLf & vbCrLf & sql & vbCrLf & vbCrLf & e.Message) Finally conn.Close() End Try

23 Using SQL to Query Data (2) Dim sql As String = "SELECT StudentID, Name, Sex FROM Students“ Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=school.mdb" Dim conn As OleDbConnection = New OleDbConnection(connStr) Dim command As OleDbCommand = New OleDbCommand(query, conn) Dim reader As OleDbDataReader Try conn.Open() ' open connection reader = command.ExecuteReader() ' execute the command While reader.Read()‘ process records Console.WriteLine("ID: {0}, Name: {1}", reader.GetString(0), reader.GetString(1)) End While Catch ex As OleDbException Console.Out.WriteLine(ex.Message) Finally reader.Close() conn.Close() End Try

24 Using SQL to Update Data ‘ Assume connection object is setup already Dim sql As String = “UPDATE STUDENTS SET Sex = “F” Dim command As OleDbCommand = New OleDbCommand(sql, conn) Dim rowAffected As Integer Try conn.Open() rowAffected = command.ExecuteNonQuery() MsgBox("Query executed, " & rowAffected & " rows affected") Catch e As Exception MsgBox("Error in executing the following SQL statement:" & vbCrLf & vbCrLf & sql & vbCrLf & vbCrLf & e.Message) Finally conn.Close() End Try

25 Using SQL Aggregate Functions Dim sql As String = "SELECT COUNT(*) FROM Students“ Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=school.mdb" Dim conn As OleDbConnection = New OleDbConnection(connStr) Dim command As OleDbCommand = New OleDbCommand(query, conn) Dim result As Integer Try conn.Open() ' open connection result = command.ExecuteScalar() ' execute the command Console.WriteLine(“The count is {0} ", result) Catch ex As OleDbException Console.Out.WriteLine(ex.Message) Finally conn.Close() End Try

26 DataReader Vs DataSet DataReader –Less memory consumed, fast access –Simple –One row at a time (forward sequential access) –Connection cannot be closed before finishing access DataAdapter + DataSet –More flexible, can examine data in an arbitrary way, scrolling back and forth –Connection can be closed and accessing the DataSet afterwards (i.e. DataSet can be de-linked with the connection)