Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to ASP.NET, Second Edition2 Chapter Objectives.

Similar presentations


Presentation on theme: "Introduction to ASP.NET, Second Edition2 Chapter Objectives."— Presentation transcript:

1

2 Introduction to ASP.NET, Second Edition2 Chapter Objectives

3 Introduction to ASP.NET, Second Edition3 Overview of the ADO.NET Framework Universal Data Access Model (UDA) – Data can be shared across different applications and platforms ActiveX Data Object Model (ADO) – Provides objects that interface with the database – Separates the process of connecting to the data source from the manipulation of the data – Ability to write code that can transfer between applications

4 Introduction to ASP.NET, Second Edition4 General Steps for Using Web Databases 1.Build your database tables and queries 2.Create a connection to the database – The connection identifies the location of the database (the data source) and the connection method (an ODBC driver, OLE-DB provider, or an OLE-DB.NET data provider), along with any other settings such as username or password 3.Create an ASP.NET Web page 4.Add an ADO.NET connection object that connects to the database, executes commands, and returns data from the database 5.Create code that will interact with the data, display the data in an ASP.NET control, perform calculations on the data, or upload changes to the database

5 Introduction to ASP.NET, Second Edition5 ODBC, OLE DB, OLE DB.NET ODBC drivers and OLE DB providers provide low- level interface to the database – Interact using proprietary interface – standard to translate commands – ODBC drivers access ODBC compliant database – OLE DB providers – relational databases Access, SQL Server, and Exchange Server – OLE DB - ODBC providers - support legacy database applications – OLE DB.NET – New.NET Providers

6 Introduction to ASP.NET, Second Edition6.NET Data Provider

7 Introduction to ASP.NET, Second Edition7 ADO.NET Data Provider Managed Providers or.NET Data Providers – ASP.NET application interacts with.NET objects – SQL Server.NET data provider is used to connect to a SQL Server database Native communication method to communicate so there is a performance improvement – OLE DB.NET data provider connect to any other data source accessed via the OLE DB interface

8 Introduction to ASP.NET, Second Edition8 ADO.NET Data Provider (continued) ADO.NET Data Provider Objects – Connection – establishes a connection to the data source – Command – executes a command (SQL) – Parameter – send information with the command (retrieve only employees who live in Michigan) – DataReader – forward only, read only stream of data – DataAdapter – populates a DataSet DataSet – once its populated, the data remain disconnected from the database

9 Introduction to ASP.NET, Second Edition9.NET Managed Data Providers

10 Introduction to ASP.NET, Second Edition10 ADO.NET Objects SQL Server.NET data provider – Prefix ‘Sql’ – SqlConnection, SqlCommand, SqlDataReader, SqlDataAdaptor OLE-DB data provider – Prefix ‘OleDb’ – OleDbConnection, OleDbCommand, OleDbDataReader, OleDbDataAdaptor. Within the Web page, ASP.NET data controls are usually bound to DataReaders or DataSets. – DataSet is exposed, so it doesn’t matter where the data came from, or what application it will be used in.

11 Introduction to ASP.NET, Second Edition11 The ADO.NET Connection Object Provides the connection to the database – Requires a connection string Connection string – Provider – name of the managed provider. – Data Source - name of the server – User ID and Password - identifies authentication – Initial Catalog - name of the database

12 Introduction to ASP.NET, Second Edition12 The ADO.NET Connection Object (continued) SQL Server Connection String Manually configured Data Connections window of Visual Studio.NET Single string or concatenate Dim CS As String CS = "Data Source=WindamereServer; User ID=sa; PASSWORD=; Initial Catalog=MyDatabase;"

13 Introduction to ASP.NET, Second Edition13 The ADO.NET Command Object Identify command or a stored procedure – Connection - Connection used – CommandType - type of command Text - default, text string contains the command StoredProcedure – Name of a stored procedure – Stored procedures - commands that are stored within the database (compiled) TableDirect - specifies table to return

14 Introduction to ASP.NET, Second Edition14 The ADO.NET Command Object (continued) CommandText – command to execute – Written in SQL - Structured Query Language Language used to identify what records to add, delete, or modify within the database Exposes a Parameters collection of objects – Used to apply values passed to stored procedures

15 Introduction to ASP.NET, Second Edition15 The ADO.NET Command Object (continued) CommandText – command to execute – Written in SQL - Structured Query Language Language used to identify what records to add, delete, or modify within the database Exposes a Parameters collection of objects – Used to apply values passed to stored procedures – WHERE employeeState = “MI”

16 Introduction to ASP.NET, Second Edition16 The ADO.NET Command Object (continued) ExecuteReader method – Method that ‘executes’ the command and passes the results to the DataReader object ExecuteNonQuery method – Does not return rows – Used to insert, modify, and delete data – Returns an integer of the number of rows affected – Perform SQL commands CREATE TABLE, INSERT INTO

17 Introduction to ASP.NET, Second Edition17 The ADO.NET DataReader Object Deliver a stream of data from the database – High-performance – Read-only, forward-only stream of data Requires continual access to the database – Must remain connected until you have finished reading the stream of data

18 Introduction to ASP.NET, Second Edition18 The ADO.NET DataReader Object (continued) Read – Returns single row and caches each row in memory only once – Moves current record pointer to the next record Close method – Closes the DataReader object and releases the references to the rowset CommandObject.CommandBehavior property – Closes the connection - CloseConnection

19 Introduction to ASP.NET, Second Edition19 The ADO.NET DataReader Object (continued)

20 Introduction to ASP.NET, Second Edition20 The ADO.NET DataReader Object (continued) Figure 6-3 – Connection object provides ConnectionString – Command object provides SQL statements CommandType - Text CommantText - SELECT, which returns data ExecuteReader method provides DataReader as a result Parameter objects can be used

21 Introduction to ASP.NET, Second Edition21 The ADO.NET DataReader Object (continued) Figure 6-3 (continued) – DataReader - receive the data Contains a collection of DataRows Read method to literally read through each row Use a looping structure to repeat through the rows. – Displaying the data - data controls

22 Introduction to ASP.NET, Second Edition22 The ADO.NET DataReader Object (continued)

23 Introduction to ASP.NET, Second Edition23 The ADO.NET DataReader Object (continued) Figure 6-3 (continued) – Connection object provides ConnectionString – Command object provides SQL statements CommandType - Text CommandText - INSERT, which creates a new record ExecuteNonQuery method – returns number of rows affected Parameter objects can be used

24 Introduction to ASP.NET, Second Edition24 The ADO.NET DataReader Object (continued) Figure 6-3 (continued) – DataReader object Contains information returned (number rows affected) No records are returned – no data binding to controls

25 Introduction to ASP.NET, Second Edition25 The ADO.NET DataReader Object (continued)

26 Introduction to ASP.NET, Second Edition26 The ADO.NET DataAdapter and DataSet Objects DataAdaptor object – Works with Connection object to connect to data – Provides bridge between DataSet and data source Commands to manage DataSet – Automatically generated via CommandBuilder object – SelectCommand - retrieve data – InsertCommand - add a new record – UpdateCommand - modify the data within a record – DeleteCommand - permanently remove a record

27 Introduction to ASP.NET, Second Edition27 The ADO.NET DataAdapter and DataSet Objects (continued) Fill method - populates DataSet – Insert data SelectCommand returns into DataSet DataSet object – disconnected collection of one or more tables that are stored in memory on the server – maintains original set of data and changes – effectively a private copy of the database – does not necessarily reflect the current state

28 Introduction to ASP.NET, Second Edition28 The ADO.NET DataAdapter and DataSet Objects (continued) DataSet Object – DataTableCollection Collection of one or more DataTable Objects Each DataTable consists of: – DataRowCollection and DataColumnCollection » store information about the rows and columns of data – ConstraintCollection » includes information about the primary and foreign keys, and constraint rules

29 Introduction to ASP.NET, Second Edition29 The ADO.NET DataAdapter and DataSet Objects (continued) Primary key – No duplicate records appear in this column. Constraint rules – Field contains the correct datatype and values DataRelationCollection contains data required to maintain relationships between DataTables – Relationships can be made between DataTables – Tables are joined using the primary and foreign keys defined in the DataTable

30 Introduction to ASP.NET, Second Edition30 The ADO.NET DataAdapter and DataSet Objects (continued)

31 Introduction to ASP.NET, Second Edition31 The ADO.NET DataAdapter and DataSet Objects (continued)

32 Introduction to ASP.NET, Second Edition32 The ADO.NET DataAdapter and DataSet Objects (continued) Figure 6-3 – DataAdapter provides access to: Connection object provides ConnectionString Command object – in this case the commands are part of the DataAdapter (SelectCommand) DataAdapter populates DataSet – Can work with data while it’s in the DataSet, DataTables, or DataView – Bind DataSet, DataTables, or DataView to data controls within the Web page

33 Introduction to ASP.NET, Second Edition33 The ADO.NET DataView Object Contains the data from the DataSet – Single DataTable or subset of records DataTable - DefaultView property – Returns all the records in the DataTable – Can select a subset of records from a table – Add columns to the DataColumnCollection RowFilter property - filter a subset Sort property - sort data based upon criteria in one or more of the columns

34 Introduction to ASP.NET, Second Edition34 Data Related Namespaces

35 Introduction to ASP.NET, Second Edition35 Building Database Connections Server Explorer window - access to local and network databases and data connections – Servers node Access to the services running on the computer, such as a database server – Data Connections node Data connection to a database Graphical tool used to create a database

36 Introduction to ASP.NET, Second Edition36 Building Database Connections (continued) – ConnectString - connection properties – Database property - database – Type property - database application – Driver - ODBC driver, OLE-DB provider, or.NET managed data provider – State - if database is currently connected – User Name of user account that created database Creator is database owner, or dbo

37 Introduction to ASP.NET, Second Edition37 Building Database Connections (continued) Server property - server – Default name MSDE/SQL Server is [MachineName]\NetSDK MachineName - name of your local computer (local)\NetSDK or localhost Driver/Provider – Assumed to be SQL Server if it uses the SQLClient class - not required in the Connection String

38 Introduction to ASP.NET, Second Edition38 Downloading the MSDE Files (Page 275, 276) Create folder c:\MSDETempDE – Download files and extract files Download files Command Window cd C:\sql2ksp3\MSDE setup INSTANCENAME="NetSDK"  SECURITYMODE=SQL SAPWD="password" Verify files and folders installed – C:\Program Files\Microsoft SQL Server\MSSQL$NETSDK – Binn, Data, Install, and Log folder

39 Introduction to ASP.NET, Second Edition39 Installing the.NET Framework SDK Samples Database (Page 277, 278, 279) Samples and QuickStart Tutorials link – Set up the QuickStarts hyperlink – QuickStart installs at http://localhost/QuickStarthttp://localhost/QuickStart – Click – Start the ASP.NET QuickStart Tutorial – Click - An E-Commerce Storefront – Run Sample Create Chapter6 project View GrocerToGo database Add connection to GrocerToGo

40 Introduction to ASP.NET, Second Edition40 Visual Studio.NET Built-In Database Tools Visual tools to create and manipulate database – Server Explorer - creates database, tables, and stored procedures – Table Designer – create columns in tables – Query and View Editor - create database queries – SQL Editor - create and edit SQL scripts and stored procedures

41 Introduction to ASP.NET, Second Edition41 Creating a SQL Server Database in Visual Studio.NET (Page 282) Authentication is required for access – Windows NT – SQL Server User ID identifies users and access to objects – Each user has roles - create or modify data object – Needs permission to create database See Appendix B for working with the MSDE and troubleshooting data connection problems Create Chapter6 database

42 Introduction to ASP.NET, Second Edition42 The Table Designer Table Design view – create schema or table structure Column Name - Do not use blank spaces or special characters other than an underscore Data Type –convert to.NET data types when you retrieve your data using ADO.NET Allow Nulls – no value, a null value is returned – Properties Pane - will depend upon data type Table Data view – Create new row, modify or delete row

43 Introduction to ASP.NET, Second Edition43 The Table Designer (continued) INT data type - integer – Identity property provide a unique value for each row to locate a specific row of data – Identity Seed - shows the initial value – Identity Increment - value to increment the seed when a new row is added If seed is 1 and increment is 1, then it’s 1, 2, 3… If seed is 1 and increment is 2, then it’s 1, 3, 5…

44 Introduction to ASP.NET, Second Edition44 The Table Designer (continued) Primary Key – Each value must be unique within this column All rows must have a value for this column No record contains a null value for column – Row selector, Table toolbar, or Diagram menu – Yellow key at the side of the column name

45 Introduction to ASP.NET, Second Edition45 The Table Designer (Products Table Page 285)

46 Introduction to ASP.NET, Second Edition46 The Table Designer (Products Table Page 285)

47 Introduction to ASP.NET, Second Edition47 Creating a View with Query and View Editor Create a query in the database Change one pane, other panes updated Table Pane – add tables and select columns – Displays query visually with icons SQL Pane – generates the SQL Preview Pane – test run queries Grid Pane – select columns and criteria, just as you do when using Microsoft Access. The column name and table indicate where to retrieve the values for the column

48 Introduction to ASP.NET, Second Edition48 Creating a View with Query and View Editor (continued) Output property - column should be visible Sort order - indicate one or more columns to sort the results Sort type - ascending or descending Alias - display an alternate column name Criteria - conditional statement that must be met before the record can be retrieved Or - indicates an alternative condition that could be met before the record can be retrieved

49 Introduction to ASP.NET, Second Edition49 Creating a View with Query and View Editor (Page 290)

50 Introduction to ASP.NET, Second Edition50 Creating a Relationship with the Data Diagram Define relationships between tables using columns – A line is drawn from a field from one table to another to indicate fields to define the relationship Endpoints indicate type of relationship – One-to-many relationship Key at one endpoint and a figure-eight at the other – One-to-one relationship Key at each endpoint

51 Introduction to ASP.NET, Second Edition51 Creating a Relationship with the Data Diagram (Page 292) Create relationship between Categories and Products – One-to-many relationship – CategoryID column – One CategoryID value in Categories – Many products that have the same CategoryID

52 Introduction to ASP.NET, Second Edition52 Creating a Relationship with the Data Diagram (continued)

53 Introduction to ASP.NET, Second Edition53 Building SQL Scripts and Stored Procedures SQL command stored within the database – Already been parsed and compiled by the server More efficient than a SQL statement – Run a SQL query (SELECT, INSERT, etc) – Values can be replaced with input parameters

54 Introduction to ASP.NET, Second Edition54 Building SQL Scripts and Stored Procedures (continued) Input parameter – Value passed to the stored procedure Parameters are input parameters by default Must match data type and length Name always begins with @ Input parameter is often compared to a value – Default value can be provided Must be a constant or it can be NULL Wildcard characters (*) are permitted

55 Introduction to ASP.NET, Second Edition55 Building SQL Scripts and Stored Procedures (continued) Output Parameters – Send values back to object that called stored procedure ReturnValue - return value passed back Called with RETURN keyword Retrieve identity column value Return @@Identity – Information such as number of records affected Values can be integer, money, varchar - not text

56 Introduction to ASP.NET, Second Edition56 Building SQL Scripts and Stored Procedures (continued) Create and edit SQL scripts & stored procedures – Inserts skeletal stored procedure when you create a new stored procedure – Color code SQL keywords – Comments Indicated with /* and */ characters Displayed in green text – Change default behaviors Tab size, word wrapping, and line numbers Options on the Tools menu

57 Introduction to ASP.NET, Second Edition57 Creating Stored Procedures with the SQL Editor (Page 294) CREATE and ALTER Procedures – First line changes from CREATE PROCEDURE to ALTER PROCEDURE – Procedure has been created – Change or alter the procedure using the ALTER PROCEDURE command

58 Introduction to ASP.NET, Second Edition58 sp_ReorderProducts (Page 294)

59 Introduction to ASP.NET, Second Edition59 sp_DisplayProduct (Page 295)

60 Introduction to ASP.NET, Second Edition60 sp_DisplayProduct (continued)

61 Introduction to ASP.NET, Second Edition61 sp_InsertCat (Page 297) Name - sp_InsertCat – Use prefix sp_ to indicate it’s a stored procedure Input paramater - @param_CatName Return the CatID value – – Return @@Identity – Returns the identity column (primary key is the identity column)

62 Introduction to ASP.NET, Second Edition62 Sp_InsertCat (Page 297)

63 Introduction to ASP.NET, Second Edition63 Sp_InsertCat (Page 297)

64 Introduction to ASP.NET, Second Edition64 sp_SearchProducts (Page 298) Search for a matching value Input parameter – Named @param_SearchProducts – Mapped to the ModelName column – Data type is nvarchar with Length 50 bytes Retrieve a subset of the columns from the database – WHERE clause to search for a condition that contains the string passed with the parameter – Wild card character and LIKE keyword locate any text that contains the string Run the procedure – search for Waterford

65 Introduction to ASP.NET, Second Edition65 sp_SearchProducts (Page 298)

66 Introduction to ASP.NET, Second Edition66 Modifying Stored Procedures with the SQL Query Builder (Page 300) SQL Query Builder - Query and View Editor – Code is stored in the stored procedure – Edit the blocks of code enclosed within a blue line – Right-click and click Design SQL Block Modify sp_ReorderProducts – Change QuantityAvailable criteria from < MinOnHand to <=10 – Save and Run procedure

67 Introduction to ASP.NET, Second Edition67 Internet Resources Microsoft MSDN Library — http://msdn.microsoft.com/library/ http://msdn.microsoft.com/library/ Microsoft UDA — Microsoft Data Access and Storage Developer Center http://www.microsoft.com/data/ http://www.microsoft.com/data/ SQL Server at Microsoft — http://www.microsoft.com/sql/ http://www.microsoft.com/sql/

68 Introduction to ASP.NET, Second Edition68 Summary ADO.NET model allows you to connect to data sources using managed providers. –.NET application interfaces with.NET Providers ADO.NET object model – Includes Connection, Command, DataAdapter, and DataReader objects – DataAdapter is a bridge between DataSet and data source – DataReader is high-performance object that provides a stream of data.

69 Introduction to ASP.NET, Second Edition69 Summary DataSet – Is a disconnected set of data stored in memory. – Consists of a DataTable Collection and DataRelationCollection Visual Database Tools allow you to create and maintain your database Many Web sites provide information on data related technologies


Download ppt "Introduction to ASP.NET, Second Edition2 Chapter Objectives."

Similar presentations


Ads by Google