Developing Web Applications Using Microsoft ® Visual Studio ® 2008.

Slides:



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

ADO.NET: Working in Disconnected Environment Sergey Baidachni MCT, MCSD, MCDBA.
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.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
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.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
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.
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.
Objective In this session we will discuss about : What is ADO. NET ?
Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.
Chapter 9 Using the SqlDataSource Control. References aspx.
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.
INSPIRING CREATIVE AND INNOVATIVE MINDS Module 4: Adding Code to a Microsoft ASP.NET Web Form Implementing Code-Behind Pages Adding Event Procedures to.
FEN Data connection DataReader DataSet Bonus info: Concurrency and Database Transactions Embedded SQL.
Overview of Data Access MacDonald Ch. 15 MIS 324 Professor Sandvig.
1 ASP.NET ASP.NET Rina Zviel-Girshin Lecture 4. 2 Overview Data Binding Data Providers Data Connection Data Manipulations.
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.
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.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
ASP.NET Rina Zviel-Girshin Lecture 5
Module 9: Accessing Relational Data Using Microsoft Visual Studio.NET.
Module 2: Connecting to Data Sources. Overview Choosing a.NET Data Provider Defining a Connection Managing a Connection Handling Connection Exceptions.
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:
An Introduction to ADO.Net Marmagna Desai.NET Seminar, Fall-2003.
Module 3: Performing Connected Database Operations.
ADO.NET Objects – Data Providers Dr. Ron Eaglin. Requirements Visual Studio 2005 Microsoft SQL Server 2000 or 2005 –Adventure Works Database Installed.
C# Programming in Depth Prof. Dr. Bertrand Meyer March 2007 – May 2007 Chair of Software Engineering Lecture 10: Database Lisa (Ling) Liu.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
ADO.NET AND STORED PROCEDURES - Swetha Kulkarni. RDBMS ADO.NET Provider  SqlClient  OracleClient  OleDb  ODBC  SqlServerCE System.Data.SqlClient.
Module 3: Working with Local Data. Overview Using DataSets Using XML Using SQL Server CE.
HNDIT Rapid Application Development
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.
1 Database Programming with ADO.NET Kashef Mughal.
C# .NET Software Development
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.
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
Integrating Data Lesson 6.
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Database Processing with ADO.NET
Introduction to Database Processing with ADO.NET
ADO.NET Framework.
PROG Advanced Web Apps 5/23/2018 Notes on ADO.NET (1) Wendi Jollymore, ACES.
Programming the Web Using ASP.Net
Lecture 6 VB.Net SQL Server.
Active Data Objects Binding ASP.NET Controls to Data
Tonga Institute of Higher Education
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:

Developing Web Applications Using Microsoft ® Visual Studio ® 2008

Module 5: Accessing Data with Microsoft ADO.NET and Visual Studio 2008 Overview of ADO.NET Connecting to a Database Accessing Data Accessing Multiple Tables

Lesson: Overview of ADO.NET What Is ADO.NET? The ADO.NET Object Model DataSets and DataReaders Accessing Data with ADO.NET

What Is ADO.NET? ADO.NET provides a set of classes for working with data. ADO.NET is: An evolutionary, more flexible successor to ADO A system designed for disconnected environments A programming model with advanced XML support A set of classes, interfaces, structures, and enumerations that manage data access in the.NET Framework

The ADO.NET Object Model DataSet DataTable ODBC Data Provider ODBC Data Provider SQL Server Data Provider SQL Server Data Provider OLE DB.NET Data Provider OLE DB.NET Data Provider Oracle Data Provider Oracle Data Provider ODBC sources SQL Server 7.0 (and later) OLEDB sources (SQL Server 6.5) Oracle sources

DataSets and DataReaders DataSetDataReader Read/write access to dataRead-only Includes multiple tables from different databases Based on one SQL statement from one database DisconnectedConnected Bind to multiple controlsBind to one control only Forward and backward scanning of data Forward-only Slower accessFaster access Supported by Visual Studio 2008 Designer Manually coded

Database Client makes request Create a SqlDataSource Client manipulates the data Update the data Use the SqlDataSource to open a database connection, update the database, and close the connection Accessing Data with ADO.NET GridView Control Client Web server SqlDataSource Return the data to the client

Lesson: Connecting to a Database Generating a Connection by Using Server Explorer The DataAdapter Object Model Generating a DataSet Creating a Connection Programmatically

In Server Explorer, right-click Data Connections, and then click Add Connection. Configure the connection Generating a Connection by Using Server Explorer

Command The DataAdapter Object Model SELECT SelectCommandUpdateCommandInsertCommandDeleteCommand DataAdapter UPDATEINSERTDELETE Database DataSet Connection

Generating a DataSet Creating a DataSet Filling the DataSet [Visual Basic] Dim myDataSet As _ New DataSet() [Visual Basic] Dim myDataSet As _ New DataSet() [Visual C#] DataSet myDataSet = new DataSet(); [Visual C#] DataSet myDataSet = new DataSet(); [Visual Basic] myDataAdapter1.Fill(ds) myDataAdapter2.Fill(ds) [Visual Basic] myDataAdapter1.Fill(ds) myDataAdapter2.Fill(ds) [Visual C#] myDataAdapter1.Fill(ds); myDataAdapter2.Fill(ds); [Visual C#] myDataAdapter1.Fill(ds); myDataAdapter2.Fill(ds);

Creating a Connection Programmatically Creating a SqlConnection instance Setting connection string parameters  Connection timeout  Data source  Initial catalog  Integrated security  Password  Persist security info  Provider  User ID [Visual Basic] Dim connectionString As String = "data source=localhost; " & _ "initial catalog=northwind; integrated security=true" Dim connection As New SqlConnection(connectionString) [Visual Basic] Dim connectionString As String = "data source=localhost; " & _ "initial catalog=northwind; integrated security=true" Dim connection As New SqlConnection(connectionString) [Visual C#] string connectionString = "data source=localhost; " + "initial catalog=northwind; integrated security=true"; SqlConnection connection = new SqlConnection(connectionString); [Visual C#] string connectionString = "data source=localhost; " + "initial catalog=northwind; integrated security=true"; SqlConnection connection = new SqlConnection(connectionString);

Lesson: Accessing Data Binding Data to Controls by Using the IDE Creating a Command Object Creating a DataReader Retrieving Data by Using a DataReader Creating a DataSet Displaying a DataSet in a List-Bound Control Handling Errors

Add a GridView control to the Web Form Bind the GridView control to a SqlDataSource control that contains the connection and query information SqlDataSource properties:  ConnectionString. The connection string to connect to the database.  ProviderName. The database type. GridView properties:  Columns. The set of columns to be shown in the control  DataSourceID. The control ID of a data source Binding Data to Controls by Using the IDE

Creating a Command Object ExecuteReader. Returns a DataReader object ExecuteScalar. Returns a single scalar value object ExecuteNonQuery. Executes a command that does not return any rows ExecuteXmlReader. Returns an XmlReader object [Visual C#] myCommand.Connection.Open(); SqlDataReader myDataReader = myCommand.ExecuteReader(); // Process the results. myCommand.Connection.Close(); [Visual C#] myCommand.Connection.Open(); SqlDataReader myDataReader = myCommand.ExecuteReader(); // Process the results. myCommand.Connection.Close(); [Visual Basic] myCommand.Connection.Open() Dim myDataReader As SqlDataReader = myCommand.ExecuteReader() ' Process the results. myCommand.Connection.Close() [Visual Basic] myCommand.Connection.Open() Dim myDataReader As SqlDataReader = myCommand.ExecuteReader() ' Process the results. myCommand.Connection.Close()

Creating a DataReader Create and open the database connection Create a Command object Create a DataReader from the Command object Call the ExecuteReader method Use the DataReader object Close the DataReader object Close the Connection object

Retrieving Data by Using a DataReader Call Read for each record  Returns false when there are no more records Access fields  Parameter is the ordinal position or name of the field  Get functions give best performance Close the DataReader Close the connection [Visual Basic] Do While myDataReader.Read() text &= myDataReader(1) text &= myDataReader ("field") text &= myDataReader.GetDateTime(2) Loop [Visual Basic] Do While myDataReader.Read() text &= myDataReader(1) text &= myDataReader ("field") text &= myDataReader.GetDateTime(2) Loop [Visual C#] while (myDataReader.Read()) { text += myDataReader[1]; text += myDataReader["field"]; text += myDataReader.GetDateTime(2); } [Visual C#] while (myDataReader.Read()) { text += myDataReader[1]; text += myDataReader["field"]; text += myDataReader.GetDateTime(2); }

Creating a DataSet Create and populate a DataSet with DataTable objects  Fill method executes the SelectCommand Access a DataTable [Visual Basic] Dim myDS As New DataSet() myDA.Fill(myDataSet, _ "Authors") [Visual Basic] Dim myDS As New DataSet() myDA.Fill(myDataSet, _ "Authors") [Visual C#] DataSet myDS = new DataSet(); myDA.Fill(myDataSet, "Authors"); [Visual C#] DataSet myDS = new DataSet(); myDA.Fill(myDataSet, "Authors"); [Visual Basic] myDS.Tables( _ "Authors").Rows.Count... Dim row As DataRow Dim text As String For Each row in myDS.Tables("Authors").Rows text &= r(1) text &= r("LastName") Next [Visual Basic] myDS.Tables( _ "Authors").Rows.Count... Dim row As DataRow Dim text As String For Each row in myDS.Tables("Authors").Rows text &= r(1) text &= r("LastName") Next [Visual C#] myDataSet.Tables[ "Authors"].Rows.Count;... string text = ""; foreach(DataRow row in myDS.Tables["Authors"].Rows) { text += row[1]; text += row["LastName"]; } [Visual C#] myDataSet.Tables[ "Authors"].Rows.Count;... string text = ""; foreach(DataRow row in myDS.Tables["Authors"].Rows) { text += row[1]; text += row["LastName"]; }

Displaying DataSet Data in List-Bound Controls Set the properties Fill the DataSet, then call the DataBind method [Visual Basic] myDataAdapter.Fill(myDataSet) employeesList.DataBind() [Visual Basic] myDataAdapter.Fill(myDataSet) employeesList.DataBind() [Visual C#] myDataAdapter.Fill(myDataSet) employeesList.DataBind(); [Visual C#] myDataAdapter.Fill(myDataSet) employeesList.DataBind(); PropertyDescription DataSourceThe DataSet containing the data DataMemberThe DataTable in the DataSet DataTextFieldThe field in the DataTable that is displayed DataValueField The field in the DataTable that becomes the value of the selected item in the list

[Visual Basic] Catch ex1 As System.Data.SqlClient.SqlException Select Case ex1.Number... Case errorsLabel.Text = errorsLabel.Text & _ ("Invalid user name")... End Select End Try [Visual Basic] Catch ex1 As System.Data.SqlClient.SqlException Select Case ex1.Number... Case errorsLabel.Text = errorsLabel.Text & _ ("Invalid user name")... End Select End Try Handling Exceptions Connection will not open  Connection string is invalid  Server or database not found  Login failed [Visual C#] catch (System.Data.SqlClient.SqlException ex1) { switch(ex1.Number) {... case 18452: errorsLabel.Text = errorsLabel.Text + ("Invalid user name"); break;... } [Visual C#] catch (System.Data.SqlClient.SqlException ex1) { switch(ex1.Number) {... case 18452: errorsLabel.Text = errorsLabel.Text + ("Invalid user name"); break;... } DataAdapter cannot create a DataSet  Invalid SQL syntax  Invalid table or field name

Lesson: Accessing Multiple Tables Storing Data From Multiple Tables Creating Relationships Programmatically Navigating Between Tables by Using Relationships

[Visual C#] ordersDataAdapter = new SqlDataAdapter ("select * from Orders", connection2); customersDataAdapter.Fill(myDataSet, "Orders"); [Visual C#] ordersDataAdapter = new SqlDataAdapter ("select * from Orders", connection2); customersDataAdapter.Fill(myDataSet, "Orders"); [Visual Basic] ordersDataAdapter = new SqlDataAdapter _ ("select * from Orders", connection2) customersDataAdapter.Fill(myDataSet, "Orders") [Visual Basic] ordersDataAdapter = new SqlDataAdapter _ ("select * from Orders", connection2) customersDataAdapter.Fill(myDataSet, "Orders") Storing Data From Multiple Tables Add the first table Add the subsequent table(s) Orders Customers DataSet [Visual C#] customersDataAdapter = new SqlDataAdapter ("select * from Customers", connection1); customersDataAdapter.Fill(myDataSet, "Customers"); [Visual C#] customersDataAdapter = new SqlDataAdapter ("select * from Customers", connection1); customersDataAdapter.Fill(myDataSet, "Customers"); [Visual Basic] customersDataAdapter = new SqlDataAdapter _ ("select * from Customers", connection1) customersDataAdapter.Fill(myDataSet, "Customers") [Visual Basic] customersDataAdapter = new SqlDataAdapter _ ("select * from Customers", connection1) customersDataAdapter.Fill(myDataSet, "Customers") connection1 connection2

[Visual C#] DataRelation coDataRelation; DataColumn parentColumn, childColumn; parentColumn = myDataSet.Tables["Customers"].Columns["CustomerID"]; childColumn = myDataSet.Tables["Orders"].Columns["CustomerID"]; cODataRelation = new DataRelation("CustomerOrders", parentColumn, childColumn); myDataSet.Relations.Add(cODataRelation); [Visual C#] DataRelation coDataRelation; DataColumn parentColumn, childColumn; parentColumn = myDataSet.Tables["Customers"].Columns["CustomerID"]; childColumn = myDataSet.Tables["Orders"].Columns["CustomerID"]; cODataRelation = new DataRelation("CustomerOrders", parentColumn, childColumn); myDataSet.Relations.Add(cODataRelation); Creating Relationships Identify parent column Identify child column Create DataRelation Orders table Customers table DataSet parentColumn childColumn DataRelation [Visual Basic] Dim cORelation As DataRelation Dim parentColumn As DataColumn, childColumn As DataColumn parentColumn = _ myDataSet.Tables("Customers").Columns("CustomerID") childColumn = _ myDataSet.Tables("Orders").Columns("CustomerID") cODataRelation = New DataRelation("CustomerOrders" _, parentColumn, childColumn) myDataSet.Relations.Add(cODataRelation) [Visual Basic] Dim cORelation As DataRelation Dim parentColumn As DataColumn, childColumn As DataColumn parentColumn = _ myDataSet.Tables("Customers").Columns("CustomerID") childColumn = _ myDataSet.Tables("Orders").Columns("CustomerID") cODataRelation = New DataRelation("CustomerOrders" _, parentColumn, childColumn) myDataSet.Relations.Add(cODataRelation)

Programmatically Navigating Between Tables by Using Relationships Customers Orders GetChildRows GetParentRow DataSet [Visual Basic] ds.Tables(index).Rows(index).GetChildRows("relation") ds.Tables(index).Rows(index).GetParentRow("relation") [Visual Basic] ds.Tables(index).Rows(index).GetChildRows("relation") ds.Tables(index).Rows(index).GetParentRow("relation") [Visual C#] ds.Tables[index].Rows[index].GetChildRows("relation"); ds.Tables[index].Rows[index].GetParentRow("relation"); [Visual C#] ds.Tables[index].Rows[index].GetChildRows("relation"); ds.Tables[index].Rows[index].GetParentRow("relation");

Lab: Accessing Data with Microsoft ADO.NET and Visual Studio 2008 Exercise 1: Connecting to the Doctors Database Exercise 2: Paging and Selection in a GridView Control Exercise 3: Implementing a SqlDataReader Exercise 4: (If Time Permits) Viewing Doctors from All Cities Logon information Virtual machine 2310C-LON-DEV-08 User name Student Password Pa$$w0rd Estimated time: 45 minutes

Lab Scenario Medical medical.aspx Benefits Home Page Default.aspx Life Insurance life.aspx Retirement retirement.aspx Dentists dental.aspx Doctors doctors.aspx Logon Page login.aspx Logon Page login.aspx Registration register.aspx Registration register.aspx Prospectus prospectus.aspx XML Web Service DentalService1.asmx Page Header header.ascx Lab Web Application User Control nameDate.ascx Menu Component Benefits.cs or Benefits.vb Master Page benefitsMaster.master LINQ to SQL Classes Doctors.dbml ASPState Dentists Doctors XML Files Web. config TempDB

Lab Review Review Questions How can you add a connection to a database? What controls are created when you drag a table from Server Explorer to a Web page? How can you enable paging for a GridView control? How can you add a select column to a GridView control? When you use Connection and SqlDataReader objects, what must you do?

Module Review and Takeaways Review Questions Real-World Issues and Scenarios Best Practices