ASP.NET Rina Zviel-Girshin Lecture 5

Slides:



Advertisements
Similar presentations
17. Data Access ADO.Net Architecture New Features of ADO.NET
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.
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
.NET Mobile Application Development Data in Distributed Systems Accessing Data with.NET.
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
Programming in ADO.NET: Data-Centric Applications and ADO.NET Original copyright by Microsoft, 2002, with minor modifications by Dan Eaves, 2005.
Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.
Chapter 9 Using the SqlDataSource Control. References aspx.
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.
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.
1 ASP.NET ASP.NET Rina Zviel-Girshin Lecture 4. 2 Overview Data Binding Data Providers Data Connection Data Manipulations.
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)
CIS 375—Web App Dev II ASP.NET 10 Database 2. 2 Introduction to Server-Side Data Server-side data access is unique in that Web pages are basically ___________.
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.
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.
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
© 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:
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.
1 Data Classes- DataView DataGridView Control. Objectives 2  Data Class  DataAdapter  DataReader  DataSet  DataTable  DataView  DataGridView Control.
C# Programming in Depth Prof. Dr. Bertrand Meyer March 2007 – May 2007 Chair of Software Engineering Lecture 10: Database Lisa (Ling) Liu.
Why ADO.NET Not your father’s Data Access.
Mauricio Featherman, Ph.D. Washington St. University
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.
1 11/15/05CS360 Windows Programming ADO.NET Continued.
HNDIT Rapid Application Development
2314 – Programming Language Concepts Introduction to ADO.NET.
ADO.NET FUNDAMENTALS BEGINNING ASP.NET 3.5 IN C#.
Module 4 Introduction ADO.NET.
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 5 Data Classes DataView – DataGridView Control 1.
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
Common SQL keywords. Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
Introduction to ADO.NET
Introduction to Database Processing with ADO.NET
Introduction to Database Processing with ADO.NET
ADO.NET and Stored Procedures
ADO.NET Framework.
Lecture 6 VB.Net SQL Server.
Active Data Objects Binding ASP.NET Controls to Data
VB.NET Using Database.
برنامه نویسی سیستم های شی گرا
Chapter 10 ADO.
PROG Advanced Web Apps 4/13/2019 Programming Data Pages Wendi Jollymore, ACES.
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:

ASP.NET Rina Zviel-Girshin Lecture 5

Rina Zviel-Girshin @Paralex Overview DataBase connection overview DataSet DataAdapter DataReader vs. DataSet Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex ADO.NET classes ADO.NET classes are separated into 2 major categories: datasource-specific and non-datasource-specific. Datasource-specific classes work with a specific data provider: System.Data.SqlClient – Microsoft SQL Server 7.0 or higher System.Data.OleDb – OLE DB provider, such as Microsoft Access. Non-datasource-specific (DataSet) work without knowledge of data provider: XML file or result of a query. In memory representation of the data. Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex System.Data.SqlClient Database SqlCommand SqlDataReader SqlConnection System.Data.SqlClient namespace includes 3 classes to perform data connection and manipulation: SqlConnection SqlCommand SqlDataReader Rina Zviel-Girshin @Paralex

SqlQueryBox.aspx Example Write a query Query result is presented to the TextBox Rina Zviel-Girshin @Paralex

A more complex example SqlQueryBox.aspx <%@ Page Language="C#"%> <%@ Import Namespace="System.Data.SqlClient" %> <script runat=“server”> void Button_Click(Object sender , EventArgs e) { int intField; txtResults.Text = ""; SqlConnection con = new SqlConnection( "Server=localhost;uid=sa; database=pubs" ); con.Open(); SqlCommand cmdSelect = new SqlCommand( txtQuery.Text, con ); Rina Zviel-Girshin @Paralex

SqlQueryBox.aspx (cont) SqlDataReader dr = cmdSelect.ExecuteReader(); while (dr.Read()) { for ( intField = 0 ; intField < dr.FieldCount; intField++) txtResults.Text += dr[ intField ].ToString().PadRight( 15 ); txtResults.Text += "\n"; } dr.Close(); con.Close(); </script> No data binding – writing directly to the textbox Rina Zviel-Girshin @Paralex

SqlQueryBox.aspx (cont) <html><head><title>SqlQueryBox.aspx</title></head> <body> <form Runat="Server"> <asp:TextBox ID="txtQuery" TextMode="MultiLine" Columns="80" Rows="4" Runat="Server" /><br> <asp:Button Text="Execute Query" OnClick="Button_Click" Runat="Server" /> <p> <asp:TextBox ID="txtResults“ TextMode="MultiLine" Columns="80" Rows="15" Wrap="False" ReadOnly="True" Runat="Server" /> </form> </body></html> Rina Zviel-Girshin @Paralex

SqlQueryBox.aspx Output Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex DataReader All DataReaders (SqlDataReader, OleDbDataReader) are connected to the database. Reads only one record at a time. Does not remember previous record. Forward-only stream reading. Can’t perform sorting, filtering or counting of the resulting set. A minimal server memory usage. Rina Zviel-Girshin @Paralex

Query’s Result Storage After opening a connection and execution of the command a result set or so-called record set is returned in the form of data stream. This stream can be read by a DataReader object or can be pushed into a DataSet object DataReader – reads DataSet – holds. Rina Zviel-Girshin @Paralex

DataSet class The DataSet class of .NET presents a set of data separated and distinct from any data stores. You can think of the DataSet as an always disconnected recordset that knows nothing about the source or destination of the data it contains - non-datasource-specific entity. DataSet works with all models of data storage: flat, relational, and hierarchical (since it does not have any 'knowledge' of the source of its data). To use DataSets the System.Data namespace should be imported: <%@ Import Namespace="System.Data" %> Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex DataAdapter A DataAdapter is the object that connects to the database or file to fill the DataSet. Therefore it is a provider specific: SqlDataAdapter,OleDbDataAdapter. DataAdapter object works as a bridge between the DataSet and the source of data You can think of the DataAdapter as a bridge between the connected and disconnected objects DataAdapter is a link between the DataSet and the Database Used to fill DataSets and update the database. DataSet Data Source DataAdapter Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex SqlDataAdapter Represents a set of data commands and a database connection that are used to fill the DataSet and update a SQL Server database. SqlDataAdapter Fill method adds or refreshes rows in the DataSet to match those in the data source using the DataSet name and creates a DataTable named "Table“ . Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex Example <%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <script runat=server> void Page_Load(Object sender , EventArgs e) { DataSet ds = new DataSet(); SqlConnection con = new SqlConnection( "Server=localhost;uid=sa; database=pubs" ); SqlDataAdapter da = new SqlDataAdapter( "Select au_lname From authors where au_lname like '%D%'", con); da.Fill( ds, “DlastName" ); //name of DataMember // Bind dataset to the DataGrid dgrdProducts.DataSource = ds; dgrdProducts.DataBind(); }</script> Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex Example <html> <head><title>DataSetwithDataGrid.aspx</title></head> <body> <asp:DataGrid ID="dgrdProducts" Runat="Server" /> </body></html> Rina Zviel-Girshin @Paralex

Fill() method Fill() method of DataAdapter is used to populate a DataSet object with the results from the query: da.Fill( ds, “DlastName" ); The Fill method implicitly opens the Connection that the DataAdapter is connected to and closes it after the execution. SqlDataAdapter da = new SqlDataAdapter( SQLQuery, con); Fill method adds or refreshes rows in the DataSet by using the DataSet name (ds) and creates or updates a DataTable named (DlastName) with the result. Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex DataSet Structure DataSet object is very complex. It represents an in-memory cache of data with a database-like structure (disconnected from DB). Inside a DataSet object there are tables, columns, constraints, relationships, views and more. A specific DataSet object contains all of the tables (DataTable objects) drawn from the database a DataRows collection (of DataRow objects) and a DataColumns collection (of DataColumn objects) is provided for each table for manipulation with table data also DataViews collection (of DataView objects) is provided and gives various ways to arrange the table information for display. Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex The general structure of a DataSet is shown below:                                                              Rina Zviel-Girshin @Paralex

DataAdapter and DataSet System.Data DataSet System.Data.SqlClient SqlConnection SqlDataAdapter SqlDataReader SqlCommand SqlParameter SqlParameters Rina Zviel-Girshin @Paralex

The DataAdapter Commands To perform data retrieval or storage from/to DB You can use DataAdapter constructor with a command and connection object. SqlDataAdapter da = new SqlDataAdapter( "Select au_lname From authors where au_lname like '%D%'", con); Or you can use DataAdapter commands Contains four command objects SelectCommand InsertCommand UpdateCommand DeleteCommand Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex Example Example: SqlConnection myConnection = new SqlConnection(connection); SqlDataAdapter myAdapter = new SqlDataAdapter(); myAdapter.InsertCommand = new SqlCommand("Insert INTO Table (UserID, PSW) VALUES (‘rina’,’1234’)", myConnection); Or SqlConnection conn = new SqlConnection(connection); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = new SqlCommand("Select au_lname From authors where au_lname like '%D%'", con, conn); adapter.Fill(dataset); Rina Zviel-Girshin @Paralex

DataAdapter connection to DB The DataAdapter connects back to the database to update the data there if any data manipulations were performed on DataSet object that held the data. The DataAdapter provides a bridge not only to retrieve data but also to save data between a DataSet and its source data store. It accomplishes this by means of requests to the appropriate SQL commands made against the data store. Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex Data Updates The DataAdapter object uses commands to update the data source after changes have been made to the DataSet. Using the Fill method of the DataAdapter calls the SELECT command. Using the Update method calls the INSERT, UPDATE or DELETE command for each changed row. Only changed data is sent back to DB. Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex Filling and Updating The cycle: DataAdapter.Fill(DataSet) user modifies the data in DataSet DataAdapter.Update(DataSet) When you update, by calling an Update method , the DataAdapter takes modified rows from the DataSet one by one: If added, calls configured INSERT command If modified, calls configured UPDATE command If deleted, calls configured DELETE command Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex DataSet DataSet object does not interact directly with the source of data. This allows the developer to work with a programming model that is always consistent, regardless of where the source data resides. As changes are made to the DataSet they can be tracked and verified before updating the source data. The GetChanges method of the DataSet object actually creates a second DatSet that contains only the changes to the data. This DataSet is then used by a DataAdapter (or other objects) to update the original data source. One DataSet can be used/bound by several controls. Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex DataReader vs. DataSet DataReader is connected to data source No caching, just retrieve and display, record after record. DataSet is disconnected from data source held in server memory, holds the whole data, one set for several controls. Rina Zviel-Girshin @Paralex

DataReader vs. DataSet Examples The DataSet's disconnected nature allows it to be transformed into XML and sent over the wire via HTTP if appropriate. A connected DataReader cannot be serialized and thus cannot be passed between physical-tier boundaries where only string (XML) data can go. A connected DataReader is associated with a specific data source therefore cannot be created, filled or traversed without a connection to the data source. A DataSet can be created manually without a connection to the source. In a situation such as an online shopping cart in which a custom data store is required, a DataSet could be created manually and its rows added. Rina Zviel-Girshin @Paralex

DataReader vs. DataSet Examples Another good use of the DataSet is the situation in which data must be retrieved and a complex action performed on each row. A DataSet could be used to store the rowset and some code could loop through the DataSet and perform a complex action. DataReaders are a good choice when an application has to be sensitive to changes in the underlying database (not cached). Binding a DataReader to a DropDownList or even a read-only DataGrid in ASP.NET works well as the data can be retrieved and displayed in the list but does not need to be persisted for editing. DataSets are ideal if data needs to be edited, sorted, filtered or searched. Rina Zviel-Girshin @Paralex

Rina Zviel-Girshin @Paralex DataSet Memory Usage A DataSet object is stored in the server memory. Using viewstate an additional copy is in the memory. If some DataSet holds 100.000 records and we have 100 clients then 100X100.000 records in stored in the server memory. A lot of memory Rina Zviel-Girshin @Paralex

Any Questions? Rina Zviel-Girshin