+ All Categories
Home > Documents > Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Date post: 28-Dec-2015
Category:
Upload: tiffany-daniel
View: 223 times
Download: 6 times
Share this document with a friend
Popular Tags:
54
Chapter 6 1 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata
Transcript
Page 1: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 1

Managing Data Sources

Introduction to ASP.NET

By Kathleen Kalata

Page 2: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 2

Objectives

• In this chapter, you will:

• Learn about the ADO.NET model

• Build a database connection

• Create a SQL Server database using Visual Studio .NET

• Manage a database using the Visual Studio .NET database tools

• Create SQL scripts using Visual Studio .NET

• Create SQL stored procedures using Visual Studio .NET

Page 3: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 3

Overview of the ADO.NET Framework

• Universal Data Access Model (UDA) provides a method whereby data can be shared across different applications and platforms

• The ODBC drivers and OLE-DB providers provide the low-level interface to the database

• The ActiveX Data Object Model (ADO) provides objects that allow you to interface with the database

– The new version of ADO is ADO.NET

– The ADO.NET model separates the process of connecting to the data source from the manipulation of the data

– Allows us the ability to write code that can transfer between applications

Page 4: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 4

General Steps for UsingWeb 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

Page 5: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 5

ADO.NET Object Model

• Interact with the database using its own proprietary interface

• Use a standard to translate commands between your application and the database to provide the low-level interface to the database

• ODBC drivers are used to provide access to an ODBC compliant database

• OLE-DB providers are available for most common data stores, including Access, SQL Server, and Exchange Server

• OLE-DB - ODBC providers - an OLE-DB provider that will interface with the ODBC driver in order to support legacy database applications

Page 6: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 6

ADO Model

Page 7: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 7

ADO.NET Object Model

• Two new sets of data providers are known as Managed Providers or .NET providers

– The ASP.NET application interacts with ADO.NET objects built into the ADO.NET Model

– SQL Server .NET data provider is used to connect to a SQL Server database

• SQL Server .NET data provider uses a native communication method to communicate with SQL Server so there is a performance improvement

– OLE-DB .NET data provider is used to connect to any other data source that can be accessed via the OLE-DB interface

Page 8: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 8

.NET Managed Data Providers

Page 9: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 9

ADO.NET Objects

– SQL Server .NET data provider

• SqlConnection, SqlCommand, SqlDataReader, and SqlDataAdaptor

– OLE-DB data provider

• OleDbConnection, OleDbCommand, OleDbDataReader, and OleDbDataAdaptor.

– DataSet object - to retrieve data from the database.

• Within the Web page, the ASP.NET data controls are bound to the DataReaders or DataSets.

• The DataSet is exposed, so it doesn’t matter where the data came from, or what application it will be used in.

Page 10: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 10

ADO.NET Core Objects

Page 11: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 11

ADO.NET Connection Object

• Provides the connection to the database

– Requires a connection string that can be manually configured, or created in the Data Connections window of Visual Studio .NET

• Connection string

– Provider – name of the managed provider. The name of the SQL Server OLE-DB Provider is Provider=SQLOLEDB.1

– Data Source - name of the server

– User ID and Password - identifies the authentication

– Initial Catalog - name of the database

Page 12: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 12

Command Object Properties

• Identify an SQL command or a stored procedure

– Connection - the Connection used by the Command object

– CommandType - to indicate the type of command being executed

• Text - by default, indicates that the command is an SQL text string

– SQL - the Structured Query Language - to identify what records to add, delete, or modify within the database

• StoredProcedure - specifies the name of a stored procedure

– Stored procedures - SQL commands that are stored within the database

Page 13: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 13

Command Object Properties and Methods

– CommandText – identifies the command to execute

– TableDirect - specifies the name of a table to return

– The Command object also exposes a Parameters collection that can be used to apply parameters which are passed to stored procedures

– Execute method - executes the command and passes the results to the DataReader object

– ExecuteNonQuery method - does not return rows because it’s used to insert, modify, and delete data, but returns an integer of the number of rows affected by the command

– Used to perform SQL commands such as CREATE TABLE, INSERT INTO, etc

Page 14: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 14

The ADO.NET DataReader Object

• To deliver a stream of data from the database

– Provides a high-performance method of accessing read-only data

– Read-only, forward-only stream of data from the database

– Requires continual access to the database, while the DataAdaptor uses a disconnected dataset to access the data

– You must remain connected until you have finished reading the stream of data

Page 15: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 15

Methods and Properties

• Read - returns a single row and caches each row in memory only once, then moves the current record pointer to the next record or row

• CommandBehavior property - closes the connection as CloseConnection

• Close method - closes the DataReader object and releases the references to the rowset

• ExecuteReader method - retrieves rows of data as they are located in the database

Page 16: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 16

Using ADO.NET Objects to Access a DataSet Object

Page 17: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 17

The ADO.NET DataAdapter Object

– DataAdapter Object accesses a DataSet object, because the DataSet is disconnected, there must be methods used to maintain the original set of data and the changes

– DataSet object is a disconnected collection of one or more tables that are stored in memory on the server

– The DataSet is effectively a private copy of the database, and does not necessarily reflect the current state of the database

– If you want to see the latest changes made by other users, you can refresh the dataset by calling the appropriate Fill method of the DataAdapter object

Page 18: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 18

DataAdapter Object Methods and Properties

– Fill method of the DataAdapter inserts the data returned from the SelectCommand into the DataSet object

– SelectCommand is used to retrieve data

– InsertCommand is used to add a new record

– UpdateCommand is used to modify the data within an existing record

– DeleteCommand is used to permanently remove a record from the database

• DataAdapter commands are automatically generated via the CommandBuilder object

• So, the DataAdapter provides the bridge between the DataSet object and the data source

Page 19: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 19

The ADO.NET DataSet Object

– Consists of the DataTableCollection and the DataRelationCollection

• DataTableCollection is a collection of one or more DataTable Objects

• Each DataTable object consists of a DataRowCollection, DataColumnCollection, and ConstraintCollection

• The DataRowCollection and DataColumnCollection store information about the rows and columns of data

– The ConstraintCollection includes information about the primary and foreign keys, and constraint rules

Page 20: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 20

The ADO.NET DataSet Object

– A primary key is used to ensure that no duplicate records appear in this column. Therefore, if one customer has the customer number, no other customer can have that number

• The Constraint rules are used to ensure that the field contains the correct datatype and values

• The UniqueContraint and ForeignKeyConstraint are used to create the relationships

– The DataRelationCollection contains the data required to maintain relationships between the DataTables

• Relational data can be exposed via ADO.NET because relationships can be made between DataTables

• The tables are joined using the primary and foreign keys that are defined in the DataTable object

Page 21: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 21

Using the DataSet Object toAccess the DataTableCollection

Page 22: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 22

The ADO.NET DataView Object

• Contains the data from the DataSet for a single DataTable or subset of records from a table

– The DataTable object has a DefaultView property that returns all the records in the DataTable. However, you can select a subset of records from a table

– You an add columns to the DataColumnCollection as well as to filter and sort the data in a DataView

– The RowFilter property of the DataView allows you to filter a subset of the DataView

– The Sort property of the DataView also allows you to sort the data based upon a specific criteria in one or more of the columns

Page 23: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 23

Data Related Namespaces

• The ADO.NET data objects are stored within the Data base class

– You can access these objects via the Data namespaces. These namespaces must be imported into your Web application

Page 24: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 24

Building Database Connections

• The Server Explorer window provides you access to your local and network databases and data connections

– The two nodes within the Server Explorer window

• Servers node provides you access to the services running on the computer, such as a database server

• Data Connections node provides you access to the data connection to a database, and to the graphical tool used to create a database

Page 25: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 25

Data Connection Properties

– ConnectString property contains all of the values of the data connection properties in a single string

– Database property is the name of the database and the Server property is the name of the server

– Type property is the name of the database application

– Driver is the name of the ODBC driver, OLE-DB provider, or .NET managed data provider

– State property indicates if the database is currently connected

– User property identifies the name of the user account that created the database. The creator of the database is known as the database owner, or dbo

Page 26: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 26

Data Connection Properties

• SQL Server Name

• Default name of the MSDE version of SQL Server is MachineName\NetSDK

• MachineName is the name of your local computer

• Also referred to as (local)\NetSDK or localhost

• Not required in the Connection String – assumed to be SQL Server if it uses the SQLClient class

Page 27: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 27

Creating a Database Connection to the GrocerToGo Database

Page 28: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 28

Visual Studio .NET Built-In Database Tools

• There are a variety of Visual Studio .NET tools that allow you to create and manipulate a database

– Server Explorer - creates your database, tables, and stored procedures

– Table Designer – creates the columns in the tables

– Query and View Editor - creates database queries

– SQL Editor - creates and edits SQL scripts and stored procedures

Page 29: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 29

Creating a SQL Server Database in Visual Studio .NET

• Authentication is required for access to the SQL Server

– Windows NT authentication

– SQL Server authentication

• SQL Server User ID identifies users and controls which users can access the database objects

• Each user has roles that identify if they are able to create or modify a data object

• The user needs permission to create the database

• See MSDE readme document provided in Chapter6Data directory for MSDE installation instructions

Page 30: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 30

Creating a Database

Page 31: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 31

The Table Designer

• Table Designer - creates the schema for the table. Enter the data, create new columns, and modify the properties of the columns

– In Table Data view, you can create a new row, modify an existing row, or delete a row

– In Table Design view, create the table structure

• Lower half - Properties Pane

– The properties will depend upon the data type

Page 32: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 32

The Table Designer

– Column Name - Do not use any blank spaces or special characters in a column name other than an underscore

– Data Type - The ADO.NET managed providers convert the data types to Managed .NET data types when you retrieve your data using ADO.NET. There are many data types defined within SQL Server. For example, the int data type is used to identify an integer

– Allow Nulls - If a column does not contain a value for a particular record, a null value is returned

Page 33: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 33

SQL Server 2000 data types

• INT data type

– Identity property is the identity column used to provide a unique value for each row to locate a specific row of data

– Identity Seed property shows the initial value of the column which is used to create the value for the first row

– Identity Increment is the value that is used to increment the seed when a new row is added

• For example, you can increment the identity column by one, so that the value of the identity column increases by one for each new record

Page 34: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 34

Primary Key

• Primary Key is used to identify the column as a unique column

– Each value for each row must be unique within this column

– All rows must have a value for this column

– No record will be allowed that contains a null value for this column

– The primary key is configured for a specific column using the row selector, the Table toolbar, or the Diagram menu

– A yellow key appears at the side of the column name to indicate that this column is the Primary Key column

Page 35: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 35

The SQL Server Column Options

Page 36: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 36

Create a Table

• Create the Products Table and columns using the Table Designer

• Create the ProductID column and set the properties

– Data type - int

– length - 4

– Allow Nulls check box – deselected

– Identity - Yes

– Identity Seed – 1

– Identity Increment - 1

– Set Primary Key

Page 37: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 37

Creating a Database Table Using the Table Editor

Page 38: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 38

Table Values

Page 39: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 39

Creating a View with Query and View Editor

• Query and View Editor allows you to create a query in the database. When you make a change in one pane, the other panes are automatically updated

– Table Pane you can add tables and select which columns to include in the query. The query is displayed visually with icons next to the columns

– SQL Pane generates the SQL for you based upon the Table Pane and Grid Pane

– Output Pane is where the output from the Query is displayed and is used to test your queries

– Grid Pane is used to 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

Page 40: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 40

Grid Pane

– Output property indicates if the column should be visible in the results when the query is executed. (Note: The output property is like the “show” check box in Access.)

– Sort order is used to indicate one or more columns to sort the results.

– Sort type as ascending or descending.

– Alias property is used to display an alternate name in the results.

– Criteria property creates a conditional statement that must be met before the record can be retrieved.

– Or property indicates an alternative condition that could be met before the record can be retrieved.

Page 41: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 41

The Query and View Editor

Page 42: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 42

Creating a Relationship with the Data Diagram

• Database Designer allows you to define relationships between tables using columns

– A line is drawn from a field from one table to another to indicate which fields are used to define the relationship between the tables

– The endpoints of the line indicate whether the relationship is one-to-one or one-to-many

• key at one endpoint and a figure-eight at the other, it is a one-to-many relationship

• key at each endpoint, it is a one-to-one relationship

Page 43: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 43

Creating a Relationship with the Data Diagram

• Table view property - view of the table

• Column view – displays the tables and columns

• Standard view - displays the column names and the schema for the columns

• Keys view - displays the names of the tables in the title bar and the names of the primary key columns

• Name view - displays only the names of the tables in the title bar

• Custom view - allows you to add any of the properties to the view

Page 44: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 44

Create a Relationship

• Create a relationship between the Categories table and the Products table

• The relationship is a one-to-many relationship

– The CategoryID is used to create a one-to-many relationship between the two tables

– There is only one CategoryID value in the Categories table

– There can be many products that have the same CategoryID value

Page 45: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 45

Create a Relationship

Page 46: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 46

Building SQL Scripts and Stored Procedures

• Stored procedures can be used to create a SQL command that is stored within the database

– Because the command is stored with the database server, it has already been parsed and compiled by the server and is more efficient than a SQL statement

– You can use a stored procedure to merely run a SQL query

– Values within the SQL statement can be replaced with the input parameters

Page 47: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 47

Input Parameters

– An input parameter is a value that is passed to the stored procedure when it is called

• All parameters are considered input parameters by default

• Must match the data type and length that is specified within the stored procedure

• Name of the parameter within the stored procedure always begins with the @ symbol

• Usually, the input parameter is compared to a value

– A default value can be provided for the parameter

• A default value for the parameter must be a constant or it can be NULL

• Wildcard characters such as the asterisk are permitted with the default value

Page 48: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 48

Output Parameters

• Output Parameters can send values back to the object that called the stored procedure

• ReturnValue parameter is a return value passed back to the stored procedure

– Called with the RETURN keyword

• The return value can be information such as the number of records affected

• Values returned can be integer, money, and varchar but not text

Page 49: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 49

Creating Stored Procedures with the SQL Editor

• To create and edit SQL scripts and stored procedures

– inserts a skeletal stored procedure when you create a new stored procedure

– color code SQL keywords, which helps minimize syntax and spelling errors

– comments in the SQL skeleton script are indicated with /* and */ characters

– comments are displayed in green text

– change the editor's default behaviors such as tab size, word wrapping, and line numbers by selecting Options on the Tools menu

Page 50: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 50

SQL Editor

• CREATE and ALTER Procedures

– Once you save a procedure, the first line SQL commands changes from CREATE PROCEDURE to ALTER PROCEDURE

– This means that the procedure has been created

– You can only change, or alter the procedure using the ALTER PROCEDURE command

Page 51: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 51

SQL Editor

Page 52: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 52

Create Stored Procedures Using the SQL Editor

• Search a field in the database for a matching value

– Search a text string under the ModelName field, then display the results

– The input parameter is named @param_SearchProducts which is mapped to the ModelName column

– The data type is nvarchar with a length of 50 bytes

– Retrieve a subset of the columns from the database

– Use the WHERE clause to search for a condition that contains the string that was passed with the parameter

– Use the wild card character and the LIKE keyword to locate any text that contains the string passed

Page 53: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 53

Modifying Stored Procedures with the SQL Query Builder

• The SQL Query Builder has the same user interface

as the Query and View Editor that you used earlier

• However, the code to create the query is stored in

the stored procedure

• Within the stored procedure, you can edit the blocks

of code that are enclosed within a blue line, via the

SQL Query Builder

Page 54: Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.

Chapter 6 54

Internet Resources

• There are lots of samples, documentation, and tutorials already on the Internet

– Microsoft MSDN Library — http://msdn.microsoft.com/library/

– Microsoft UDA — http://www.microsoft.com/data/default.htm

– MSDN Library — http://www.msdn.microsoft.com/library/

– SQL at Microsoft — http://www.microsoft.com/sql/


Recommended