Direct Data Access, Data Binding. Content Direct Data Access Data Binding Muzaffer DOĞAN - Anadolu University2.

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

Introduction to SQL, OleDB interface to Access from VB.NET.
Database Connections with ASP.Net
Best PracticesUSCA Fall 2010: Baylor University3.
ADO.NET: Working in Disconnected Environment Sergey Baidachni MCT, MCSD, MCDBA.
Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control.
Rob Walker, May 2008Student Learning Unit Victoria University1 Essay Writing A workshop for ASW 3102: Critical Social Work Theories.
Connecting to Database 21 Feb Database Options Can use many different databases in conjunction with php. – MySql; MS Access; Oracle; etc etc Most.
User Controls, Master Pages, GridView. Content User Controls Styles, Themes, Master Pages Working with Data GridView Muzaffer DOĞAN - Anadolu University2.
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.
The ADO Data Control. Universal Data Access Open Database Connectivity (ODBC) –standard for accessing data in databases OLE-DB –allows access to data.
1 Pertemuan 09 Database Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions.
Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Chapter 9 Using the SqlDataSource Control. References aspx.
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.
Chapter 10 Managing Data with ASP.NET. ASP.NET 2.0, Third Edition2.
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.
1 ASP.NET ASP.NET Rina Zviel-Girshin Lecture 4. 2 Overview Data Binding Data Providers Data Connection Data Manipulations.
Developing Web Applications Using Microsoft ® Visual Studio ® 2008.
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.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
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.
Delivering Excellence in Software Engineering ® EPAM Systems. All rights reserved. Data Binding in ASP.NET.
ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix.
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.
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
ADO.NET Part 2. Slide 2 Overview Slide 3 Introduction to the DataGridView Control It’s a two-dimensional grid containing rows and columns Its use in.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
 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.
Mauricio Featherman, Ph.D. Washington St. University
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
BlackBerry Applications using Microsoft Visual Studio and Database Handling.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
HNDIT Rapid Application Development
ADO.NET FUNDAMENTALS BEGINNING ASP.NET 3.5 IN C#.
Module 4 Introduction ADO.NET.
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.
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Database Processing with ADO.NET
Introduction to Database Processing with ADO.NET
Unit 9.1 Learning Objectives Data Access in Code
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
VB.NET Using Database.
Tonga Institute of Higher Education
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Chapter 10 ADO.
Database Applications
Chapter 10 Accessing Database Files
Active Data Objects Binding ASP.NET Controls to Data
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

Direct Data Access, Data Binding

Content Direct Data Access Data Binding Muzaffer DOĞAN - Anadolu University2

3

Important Note!!! Last week, you learned how to connect to databases using built-in controls. Use them in your applications whenever possible. If you sure that you need to connect to database manually, follow the instructions explained in this class. Muzaffer DOĞAN - Anadolu University4

Direct Data Access – Querying 1. Create Connection, Command, and DataReader objects 2. Use the DataReader to retrieve information from the database, and display it in a control 3. Close your connection 4. Send the page to the user Muzaffer DOĞAN - Anadolu University5

Updating, Inserting, Deleting 1. Create new Connection and Command objects 2. Execute the Command with the appropriate SQL statement Muzaffer DOĞAN - Anadolu University6

Direct Data Access with ADO.NET Muzaffer DOĞAN - Anadolu University7

ADO.NET Data Provider Classes SQL ServerOleDB (Access) ConnectionSqlConnectionOleDbConnection CommandSqlCommandOleDbCommand DataReaderSqlDataReaderOleDbDataReader DataAdapterSqlDataAdapterOleDbDataAdapter Muzaffer DOĞAN - Anadolu University8 Use OracleConnection, OracleCommand, etc. for Oracle data providers Use OdbcConnection, OdbcCommand, etc. for ODBC data providers

Namespace Imports Import following namespaces for SQL Server: using System.Data; using System.Data.SqlClient; Import following namespaces for Access: using System.Data; using System.OleDb; Muzaffer DOĞAN - Anadolu University9

Connecting Access Database OleDbConnection conn = new OleDbConnection(); conn.ConnectionString Data Source=|DataDirectory|\Survey.mdb"; conn.Open(); // Database operations will be here... conn.Close(); Muzaffer DOĞAN - Anadolu University10

Connecting SQL Server Express SqlConnection conn = new SqlConnection(); conn.ConnectionString Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\Survey.mdf; Integrated Security=True;User Instance=True"; conn.Open(); // Database operations will be here... conn.Close(); Muzaffer DOĞAN - Anadolu University11

How to Obtain Connection Strings Use SqlDataSource or AccessDataSource objects to connect to the database and copy the connection strings into your code shows many connection string options Muzaffer DOĞAN - Anadolu University12

Storing the Connection String Write the connection string into connectionString section of web.config file:... Muzaffer DOĞAN - Anadolu University13

Retrieving the Connection String string connectionString = WebConfigurationManager. ConnectionStrings["Pubs"].ConnectionString; Muzaffer DOĞAN - Anadolu University14

Execute Command Command object has several methods starting with the "Execute" string: ExecuteNonQuery(): Used for queries that don't return any records (e.g. Update, Insert, Delete queries) ExecuteReader(): Used for queries that return one or more records (e.g. Select query) ExecuteScalar(): Used for queries that return one or more records but this method returns only the first column of the first row (suitable for obtaining number of records, maximum value of a column) Muzaffer DOĞAN - Anadolu University15

The DataReader Allows you to quickly retrieve all your results Uses a live connection and should be used quickly and then closed Can retrieve only one record at a time Supports fast-forward-only and read-only access to the results (previous record cannot be reached) Provides better performance than the DataSet Muzaffer DOĞAN - Anadolu University16

The DataReader Create a DataReader by ExecuteReader method of the Command object Retrieve the record by the Read() method of the DataReader object To retrieve the next record, use Read() method again If next record is successfully read, the Read() method returns true So, continue reading until the Read() method returns false Muzaffer DOĞAN - Anadolu University17

The DataReader OleDbConnection conn = new Source= |DataDirectory|\Survey.mdb"); OleDbCommand cmd = new OleDbCommand("SELECT * FROM UserInfo", conn); conn.Open(); OleDbDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Label1.Text += reader["FirstName"] + " "; } reader.Close(); conn.Close(); Muzaffer DOĞAN - Anadolu University18

ExecuteScalar Example OleDbConnection conn = new Source= |DataDirectory|\Survey.mdb"); OleDbCommand cmd = new OleDbCommand("SELECT MAX(FavoriteNumber) FROM UserInfo", conn); conn.Open(); int maxfav = (int)cmd.ExecuteScalar(); conn.Close(); Muzaffer DOĞAN - Anadolu University19

ExecuteNonQuery Example OleDbConnection conn = new Source= |DataDirectory|\Survey.mdb"); OleDbCommand cmd = new OleDbCommand("DELETE * FROM UserInfo WHERE UserID=5", conn); conn.Open(); int affectedRowNumber = cmd.ExecuteNonQuery(); conn.Close(); Muzaffer DOĞAN - Anadolu University20

Muzaffer DOĞAN - Anadolu University21

Data Binding You can use the DataSet or the DataReader to retrieve rows of information, format them individually, and add them to an HTML table on a web page Conceptually, this isn’t too difficult. However, it still requires a lot of repetitive code to move through the data, format columns, and display it in the correct order Repetitive code may be easy, but it’s also error-prone, difficult to enhance, and unpleasant to read Fortunately, ASP.NET adds a feature that allows you to skip this process and pop data directly into HTML elements and fully formatted controls. It’s called data binding Muzaffer DOĞAN - Anadolu University22

Data Binding The basic principle of data binding is this: you tell a control where to find your data and how you want it displayed, and the control handles the rest of the details. ASP.NET data binding works in one direction only. Information moves from a data object into a control. Then the data objects are thrown away, and the page is sent to the client. If the user modifies the data in a data-bound control, your program can update the corresponding record in the database, but nothing happens automatically. Muzaffer DOĞAN - Anadolu University23

Types of ASP.NET Data Binding Single-Value, or "Simple", Data Binding Single-value data binding allows you to take a variable, a property, or an expression and insert it dynamically into a page Single-value binding also helps you create templates for the rich data controls Repeated-Value, or "List", Binding Allows you to display an entire table (or just a single field from a table) Muzaffer DOĞAN - Anadolu University24

Using Data Binding To use single-value binding, you must insert a data binding expression into the markup in the.aspx file (not the code-behind file). To use repeated-value binding, you must set one or more properties of a data control. Once you specify data binding, you need to activate it. You accomplish this task by calling the DataBind() method of the control. Alternatively, you can bind the whole page at once by calling the DataBind() method of the current Page object. Muzaffer DOĞAN - Anadolu University25

A Simple List Binding Example ArrayList fruit = new ArrayList(); fruit.Add("Kiwi"); fruit.Add("Mango"); fruit.Add("Blueberry"); fruit.Add("Apricot"); fruit.Add("Banana"); lstItems.DataSource = fruit; lstItems.DataBind(); // or this.DataBind(); Muzaffer DOĞAN - Anadolu University26

References Beginning ASP.NET 3.5 in C# 2008: From Novice to Professional Visual Studio and MSDN Help Muzaffer DOĞAN - Anadolu University27