Access Data Object (cont.)



Similar documents
AD A O.N. ET E Access Data Object

VB.NET - DATABASE ACCESS

Intermediate ASP.NET Web Development with C# Instructor: Frank Stepanski. Data Sources on the Web

Working with Data in ASP.NET 2.0 :: Paging and Sorting Report Data Introduction. Step 1: Adding the Paging and Sorting Tutorial Web Pages

Expanded contents. Section 1. Chapter 2. The essence off ASP.NET web programming. An introduction to ASP.NET web programming

Integrating SAS and Microsoft.NET for Data Analysis

DEVELOPING A PHD MONITORING TOOL USING ASP.NET AND SQL SERVER. A Report Submitted In Partial Fulfillment Of Course BITS C331 Computer Oriented Project

Mastering Visual Basic.NET Database Programming Evangelos Petroutsos; Asli Bilgin

1. La classe Connexion class Connexion {

How To Create A Data Gateway On A Microsoft Powerbook On A Pc Or Macode (For Microsoft) On A Macode 2.5 (For A Microode) On An Ipad Or Macroode ( For A Microos

2. Modify default.aspx and about.aspx. Add some information about the web site.

1.264 Lecture 19 Web database: Forms and controls

One method for batch DHI data import into SQL-Server. A batch data import technique for DateSet based on.net Liang Shi and Wenxing Bao

ASP.NET Using C# (VS2012)

Beginning ASP.NET 4.5

C# Datenbank-Programmierung

MOVING THE SENIOR DEVELOPMENT CLASS FROM WEB DEVELOPMENT TO LIFE CYCLE DEVELOPMENT A CASE FOR VISUAL STUDIO 2005

ASP.NET Programming with C# and SQL Server

Using IRDB in a Dot Net Project

Real-World ASP.NET: Building a Content Management System

Database Communica/on in Visual Studio/C# using Web Services. Hans- Pe=er Halvorsen, M.Sc.

Transition your MCPD Web Developer Skills to MCPD ASP.NET Developer 3.5 (VB)

SQL Server Database Web Applications

How To Create A Database In Araba

Conexión SQL Server C#

A Tutorial on SQL Server CMPT 354 Fall 2007

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

Hands-On Lab. Building a Data-Driven Master/Detail Business Form using Visual Studio Lab version: Last updated: 12/10/2010.

Visual COBOL ASP.NET Shopping Cart Demonstration

Crystal Reports for Visual Studio.NET

1. Create SQL Database in Visual Studio

Aplicação ASP.NET MVC 4 Usando Banco de Dados

RSS Feed from an Access Database

SQL injection attacks SQL injection user input SQL injection SQL Command parameters Database account. SQL injection attacks Data Code

UltraLog HSPI User s Guide

ASP.NET Overview. Ken Casada Developer Evangelist Developer & Platform Evangelism Microsoft Switzerland

Deleting A Record Updating the Database Binding Data Tables to Controls Binding the Data Table to the Data Grid View...

Create an Access Database from within Visual Basic 6.

Creating the Product Catalog Part I (continued)

Sage Abra SQL HRMS. Abra Workforce Connections. Advanced Customization Guide

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Bertrand Meyer. C#: Persistence

IBM DB2 XML support. How to Configure the IBM DB2 Support in oxygen

ASP and ADO (assumes knowledge of ADO)

listboxgaatmee.dragdrop += new DragEventHandler(listBox_DragDrop); ListBox from = (ListBox)e.Data.GetData(typeof(ListBox));

Form Tasarımı - 5. Veri Tabanı Veri tabanı ismi; m Tablo ismi; mt

Siemens Applied Automation Page 1 11/26/03 9:57 PM. Maxum ODBC 3.11

Keywords web applications, scalability, database access

Classe AGI - PHP 5.x

2311A: Advanced Web Application Development using Microsoft ASP.NET Course 2311A Three days Instructor-led

2. Unzip the file using a program that supports long filenames, such as WinZip. Do not use DOS.

TS: Microsoft.NET Framework 3.5, ASP.NET Application Development

Employee Management System

The software shall provide the necessary tools to allow a user to create a Dashboard based on the queries created.

Visual Basic Database Connectivity

Bronson Door Company Web Site

The Developer Side of the MS Business Intelligence Stack

.NET Development for the web Using Microsoft Office SharePoint Server 2007 and ASP.NET

Implementation of the AutoComplete Feature of the Textbox Based on Ajax and Web Service

ACDS AIMS Certified Database Specialist Course.

Interacting with a Database Using Visual Basic.NET

LearnFromGuru Polish your knowledge

Fasthosts ASP scripting examples Page 1 of 17

2 SQL in iseries Navigator

Access Tutorial 1 Creating a Database. Microsoft Office 2013 Enhanced

COMOS. Lifecycle COMOS Snapshots. "COMOS Snapshots" at a glance 1. System requirements for installing "COMOS Snapshots" Database management 3

Crystal Reports. For Visual Studio.NET. Reporting Off ADO.NET Datasets

Managing User Accounts

Create a New Database in Access 2010

Advanced BIAR Participant Guide

Chapter 8 Access Database

New 11g Features in Oracle Developer Tools for Visual Studio. An Oracle White Paper January 2008

Data Mining Commonly Used SQL Statements

Intellect Platform - Tables and Templates Basic Document Management System - A101

Using SQL Server Management Studio

COURSE CURRICULUM COURSE TITLE: WEB PROGRAMMING USING ASP.NET (COURSE CODE: )

Microsoft Access 2010 handout

Crystal Converter User Guide

Developing Rich Web Applications with Oracle ADF and Oracle WebCenter Portal

Advanced Web Application Development using Microsoft ASP.NET

Access Tutorial 1 Creating a Database

Database: Data > add new datasource ALGEMEEN

1. To start Installation: To install the reporting tool, copy the entire contents of the zip file to a directory of your choice. Run the exe.

Sage HRMS 2015 Sage Employee Self Service Advanced Customization. February 2015

SQL Desktop Application For WebService in C# dr inż. Tomasz Tatoń

Enterprise Portal Development Cookbook

Managing User Accounts

SAP Business Objects Business Intelligence platform Document Version: 4.1 Support Package Data Federation Administration Tool Guide

Building ASP.NET Applications

Connect to an Oracle Database from within Visual Basic 6 (Part 1)

TABLE OF CONTENTS INSTALLATION MANUAL MASTERSAF DW

Instructions for creating a data entry form in Microsoft Excel

Transcription:

ADO.NET Access Data Object (cont.)

What is a Dataset? DataTable DataSet DataTable DataTable SqlDataAdapter SqlConnection OleDbDataAdapter Web server memory Physical storage SQL Server 2000 OleDbConnection OleDb Database 1

Data Adapters DataSet DataAdapter SelectCommand UpdateCommand InsertCommand DeleteCommand DataReader Command Command Command Command Connection sp_select sp_update sp_insert sp_delete Database 2

DataAdapter Store the query in a DataAdapter SqlDataAdapter da = new SqlDataAdapter ("select * from Authors",conn); SelectCommand Properties da.selectcommand.commandtext da.selectcommand.connection Definir os comandos InsertCommand, UpdateCommand, and DeleteCommand, se necessário da.insertcommand da.updatecommand 3

Creating a DataSet Create a DataSet Fill executa o SelectCommand DataSet ds = new DataSet(); da.fill(ds, "Authors"); Access a DataTable ds.tables["authors"].rows.count; string str=""; foreach(datarow r in ds.tables["authors"].rows) { str += r[2]; str += r["au_lname"]; } 4

DataAdapder / DataSet DataSource de um Controlo string strconn="provider=microsoft.jet.oledb.4.0;. String strsql= "Select * from Produtos where IdCat= + id; conn=new OleDbConnection(); conn.connectionstring=strconn; OleDbDataAdapter da; da = new OleDbDataAdapter (strsql,conn); DataSet ds= new DataSet(); da.fill(ds, Produtos ); //Populating DataSet gvprodutos.datasource=ds; gvprodutos.databind(); 5

DataAdapder / DataSet Aceder às tabelas no DataSet Usar Tables para aceder à tabela Produtos Objecto DataTable DataTable dt =dataset.tables[ Produtos"] Aceder às linhas da tabela Usar Rows Objecto DataRow DataRow dr= dataset.tables["produtos ].Rows[0]; foreach (DataRow dr in dataset.tables[ Produtos"].Rows) 6

Data-Bound Web Server Controls ASP.NET Data-Bound Web Server Controls GridView displays data as a table and provides the capability to sort columns, page through data, and edit or delete a single record DetailsView renders a single record at a time as a table and provides the capability to page through multiple records, as well as to insert, update, and delete records. FormView renders a single record at a time from a data source and provides the capability to page through multiple records, as well as to insert, update, and delete records. FormView control does not specify a built-in layout. 7

Data-Bound Web Server Controls Repeater renders a read-only list from a set of records returned from a data source. Repeater control does not specify a built-in layout. DataList renders data as table and enables you to display data records in different layouts, such as ordering them in columns or rows ASP.NET Data-Bound Web Server Controls Overview https://msdn2.microsoft.com/en-us/library/ms228214.aspx 8

Data-Bound Web Server Controls Edit, Update, Delete e Insert num Data-Bound Web Server Controls Exemplificado com um DetailView As propriedades e eventos são semelhantes para os outros controlos Todos os métodos não estão protegidos para salientar o código ADO e programação dos eventos Cada método deveria: Tratar excepções Validar parâmetros de entrada 9

DetailsView Populate DetailsView void binddetailview() { DataAccess dal = new DataAccess(); DataSet ds = dal.getdatasetprodutos(); DetailsView1.DataSource = ds; DetailsView1.DataBind(); } } 10

DetailsView Configuration 11

DetailsView Events: DataBinding Occurs when the server control binds to a data source. (inherited from Control) ItemCreated Occurs when a record is created in a DetailsView control. ItemDeleted Occurs when a Delete button within a DetailsView control is clicked, but after the delete operation. ItemDeleting Occurs when a Delete button within a DetailsView control is clicked, but before the delete operation. ItemInserting Occurs when an Insert button within a DetailsView control is clicked, but before the insert operation. ItemUpdated Occurs when an Update button within a DetailsView control is clicked, but after the update operation. 12

Events: ItemUpdating Occurs when an Update button within a DetailsView control is clicked, but before the update operation. Load Occurs when the server control is loaded into the Page object. (inherited from Control) ModeChanging Occurs when a DetailsView control attempts to change between edit, insert, and read-only mode, but before the CurrentMode property is updated. PageIndexChanged Occurs when the value of the PageIndex property changes after a paging operation. PageIndexChanging Occurs when the value of the PageIndex property changes before a paging operation. http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview_events.aspx 13

DetailsView Paging Property: AllowPaging=true; Event : PageIndexChanging Property: PageIndex protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e) { DetailsView1.PageIndex = e.newpageindex; binddetailview(); } 14

DetailsView Edit Property: AutoGenerateEditButton=true; Event: ModeChanging Property: DetailsViewMode (Edit, Insert, ReadOnly ) protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e) { if ( e.newmode == DetailsViewMode.Edit) { DetailsView1.ChangeMode(DetailsViewMode.Edit); } else if (DetailsView1.CurrentMode == DetailsViewMode.Edit && e.cancelingedit) { DetailsView1.ChangeMode(DetailsViewMode.ReadOnly); }... 15

DetailsView DetailsView Structure (GridView, FormView, etc) Contains a collection of Rows Row contains a collection of Cells Cell contains a collection of Controls Rows[0] Cells[0] Cells[1] Controls[0] > (TextBox) nome=((textbox)detailsview1.rows[2].cells[1].controls[0]).text; 16

DetailsView Fields Values protected void getdetailsview_values() { idcat = Convert.ToInt32(((TextBox)DetailsView1.Rows[1].Cells[1]. Controls[0]).Text); nome = ((TextBox)DetailsView1.Rows[2].Cells[1].Controls[0]).Text; preco = Convert.ToDouble(((TextBox)DetailsView1.Rows[3].Cells[1]. Controls[0]).Text); stock = Convert.ToInt32(((TextBox)DetailsView1.Rows[4].Cells[1]. Controls[0]).Text); } 17

DetailsView Update Event: ItemUpdating protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) { int idprod = Convert.ToInt32(DetailsView1.DataKey.Value); DataAccess dal = new DataAccess(); getdetailsviewvalues(); dal.updateproduct(idprod,idcat,nome,preco,stock); DetailsView1.ChangeMode(DetailsViewMode.ReadOnly); binddetailview(); } 18

DetailsView Insert ( New) Property: AutoGenerateInsertButton Event: ItemInserting protected void DetailsView1_ItemInserting(object { sender, DetailsViewInsertEventArgs e) DataAccess dal = new DataAccess(); getdetailsviewvalues(); dal.insertproduct(idcat, nome, preco, stock); DetailsView1.ChangeMode(DetailsViewMode.ReadOnly); binddetailview(); } 19

DetailsView Delete Property: AutoGenerateDeleteButton=true; Event: ItemDeleting protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e) { DataAccess dal = new DataAccess(); int idprod = Convert.ToInt32(DetailsView1.DataKey.Value); dal.deleteproduct(idprod); binddetailview(); } 20

Métodos na Class Data Access Layer getconnection private OleDbConnection getconnection() { } string strcon = DEFAULT_CONN + DEFAULT_DBPATH; OleDbConnection conn = new OleDbConnection(); conn.connectionstring = strcon; conn.open(); return conn; 21

Métodos na Classe Data Access Layer getdatasetproduts public DataSet getdatasetprodutos() { } OleDbConnection conn = getconnection(); OleDbCommand cmd = new OleDbCommand(); cmd.connection = conn; string strsql = "select * from Produtos "; cmd.commandtext = strsql; OleDbDataAdapter da = new OleDbDataAdapter(); da.selectcommand = cmd; DataSet ds = new DataSet(); da.fill(ds); return ds; 22

Métodos na Class Data Access Layer updateproduct public int updateproduct(int idprod, int idcat,string nomeprod,double preco, int stock) { string strsql = "update Produtos SET IdCat=?, NomeProd=?, Preco=?, Stock=? where IdProd=?"; OleDbConnection conn = getconnection(); OleDbCommand cmd = new OleDbCommand(); cmd.connection = conn; cmd.commandtext = strsql; cmd.parameters.addwithvalue("idcat", idcat); cmd.parameters.addwithvalue("stock", stock); cmd.parameters.addwithvalue("idprod", idprod); int result=cmd.executenonquery(); conn.close(); return result; } 23

Class Data Access Layer insertproduct public int insertproduct( int idcat, string nomeprod, double preco, int stock){ string strsql = "insert into Produtos (IdCat, NomeProd, Preco, Stock ) values(?,?,?,?)"; OleDbConnection conn = getconnection(); OleDbCommand cmd = new OleDbCommand(); cmd.connection = conn; cmd.commandtext = strsql; cmd.parameters.addwithvalue("idcat", idcat); cmd.parameters.addwithvalue("nomeprod", nomeprod); cmd.parameters.addwithvalue("preco", preco); cmd.parameters.addwithvalue("stock", stock); int idprod = cmd.executenonquery(); conn.close(); return idprod; } 24

Class Data Access Layer deleteproduct public int deleteproduct(int idprod) { } string strsql = "Delete from Produtos where IdProd=?"; OleDbConnection conn = getconnection(); OleDbCommand cmd = new OleDbCommand(); cmd.connection = conn; cmd.commandtext = strsql; cmd.parameters.addwithvalue("idprod", idprod); int res = cmd.executenonquery(); conn.close(); return res; 25