690 likes | 836 Views
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
E N D
Chapter Objectives Introduction to ASP.NET, Second Edition
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
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
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
.NET Data Provider Introduction to ASP.NET, Second Edition
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
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
.NET Managed Data Providers Introduction to ASP.NET, Second Edition
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
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
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
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
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
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
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
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
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
The ADO .NET DataReader Object (continued) Introduction to ASP.NET, Second Edition
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
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
The ADO .NET DataReader Object (continued) Introduction to ASP.NET, Second Edition
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
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
The ADO .NET DataReader Object (continued) Introduction to ASP.NET, Second Edition
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
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
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
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
The ADO .NET DataAdapter and DataSet Objects (continued) Introduction to ASP.NET, Second Edition
The ADO .NET DataAdapter and DataSet Objects (continued) Introduction to ASP.NET, Second Edition
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
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
Data Related Namespaces Introduction to ASP.NET, Second Edition
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
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
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
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
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
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
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
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
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
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
The Table Designer (Products Table Page 285) Introduction to ASP.NET, Second Edition
The Table Designer (Products Table Page 285) Introduction to ASP.NET, Second Edition
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
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
Creating a View with Query and View Editor (Page 290) Introduction to ASP.NET, Second Edition
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