1 / 69

Chapter Objectives

Chapter Objectives. 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

tamber
Download Presentation

Chapter Objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter Objectives Introduction to ASP.NET, Second Edition

  2. 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 Introduction to ASP.NET, Second Edition

  3. General Steps for UsingWeb Databases • Build your database tables and queries • 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 • Create an ASP.NET Web page • Add an ADO.NET connection object that connects to the database, executes commands, and returns data from the database • 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 Introduction to ASP.NET, Second Edition

  4. 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 Introduction to ASP.NET, Second Edition

  5. .NET Data Provider Introduction to ASP.NET, Second Edition

  6. 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 Introduction to ASP.NET, Second Edition

  7. 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 Introduction to ASP.NET, Second Edition

  8. .NET Managed Data Providers Introduction to ASP.NET, Second Edition

  9. 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. Introduction to ASP.NET, Second Edition

  10. 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 Introduction to ASP.NET, Second Edition

  11. 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;" Introduction to ASP.NET, Second Edition

  12. 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 Introduction to ASP.NET, Second Edition

  13. 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 Introduction to ASP.NET, Second Edition

  14. 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” Introduction to ASP.NET, Second Edition

  15. 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 Introduction to ASP.NET, Second Edition

  16. 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 Introduction to ASP.NET, Second Edition

  17. 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 Introduction to ASP.NET, Second Edition

  18. The ADO .NET DataReader Object (continued) Introduction to ASP.NET, Second Edition

  19. 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 Introduction to ASP.NET, Second Edition

  20. 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 Introduction to ASP.NET, Second Edition

  21. The ADO .NET DataReader Object (continued) Introduction to ASP.NET, Second Edition

  22. 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 Introduction to ASP.NET, Second Edition

  23. 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 Introduction to ASP.NET, Second Edition

  24. The ADO .NET DataReader Object (continued) Introduction to ASP.NET, Second Edition

  25. 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 Introduction to ASP.NET, Second Edition

  26. 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 Introduction to ASP.NET, Second Edition

  27. 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 Introduction to ASP.NET, Second Edition

  28. 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 Introduction to ASP.NET, Second Edition

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

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

  31. 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 Introduction to ASP.NET, Second Edition

  32. 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 Introduction to ASP.NET, Second Edition

  33. Data Related Namespaces Introduction to ASP.NET, Second Edition

  34. 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 Introduction to ASP.NET, Second Edition

  35. 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 Introduction to ASP.NET, Second Edition

  36. 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 Introduction to ASP.NET, Second Edition

  37. Downloading the MSDE Files (Page 275, 276) • Create folder c:\MSDETempDE • Download files and extract 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 Introduction to ASP.NET, Second Edition

  38. 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/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 Introduction to ASP.NET, Second Edition

  39. 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 Introduction to ASP.NET, Second Edition

  40. 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 Introduction to ASP.NET, Second Edition

  41. 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 Introduction to ASP.NET, Second Edition

  42. 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… Introduction to ASP.NET, Second Edition

  43. 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 Introduction to ASP.NET, Second Edition

  44. The Table Designer (Products Table Page 285) Introduction to ASP.NET, Second Edition

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

  46. 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 Introduction to ASP.NET, Second Edition

  47. 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 Introduction to ASP.NET, Second Edition

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

  49. 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 Introduction to ASP.NET, Second Edition

More Related