43
Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

  • View
    221

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Managing MS SQL Server® Data Sources in VS.NET

ASP.NET

Page 2: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Data Source Processing RequirementsData Source Processing Requirements

• In today's computer networks there needs to be the capability to share incompatible data from differing file types:

– Data from different software companies such as Microsoft, Oracle, Sybase, Informix, etc.

– Formatted data (i.e. currency format with 1000's separators), graphics, audio, video, etc.

– Data from a variety of applications, i.e. word processing documents, spreadsheets, etc.

– Data stored on intranets and the Internet including XML data sources

Page 3: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Universal Data AccessUniversal Data Access

• The solution to this need to share data is called universal data access (UDA)

• At core to universal access is OLE (object linking and embedding) DB (database) object interface

• Data from a variety of sources can be shared across different applications and platforms

• Accesses data stored in different formats , i.e.– I.e., UDA can treat data stored in databases and

spreadsheets as if they all were in the same format, even within the same application

Page 4: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Overview of the ADO.NET FrameworkOverview of the ADO.NET Framework

• 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

Page 5: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Visual Studio .NET and DatabasesVisual Studio .NET and Databases

• Visual Studio .NET can operate on databases from different software companies:– Microsoft, Oracle, Sybase, Informix, etc.

• SQL commands are embedded into code enabling communication with the DBMS

• Additionally ASP.NET Toolbox controls may be used to provide database connectivity

Page 6: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

General Steps for Using Web DatabasesGeneral Steps for Using Web Databases

1. Build your database tables and queries/stored procedures

2. Create a connection to the database

– The connection identifies the location of the database (the data source) and the connection method (ODBC driver, OLE-DB provider, or an OLE-DB.NET data provider), and any other settings, i.e. username/password

3. Create an ASP.NET Web page

4. Add ADO.NET connection object that connects to 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 7: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

.NET Data Provider.NET Data Provider

Page 8: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Downloading Microsoft SQL Server Desktop Engine (MSDE) FilesDownloading Microsoft SQL Server Desktop Engine (MSDE) Files

1. Create folder– md c:\MSDETempDE– Download files and extract files

2. Command Window– cd C:\sql2ksp3\MSDE – setup INSTANCENAME="NetSDK"

SECURITYMODE=SQL SAPWD="password"

3. Verify files and folders installed– C: cd\Program Files\Microsoft SQL Server\

MSSQL$NETSDK

– Binn, Data, Install, and Log folder should be there

Page 9: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Installing .NET Framework DSK Samples DatabasesInstalling .NET Framework DSK Samples Databases

1. Click Start and select "All Programs" "Microsoft .NET Framework SDK v1.1" "Samples and Quickstart Tutorials"

2. Select "Setup the Quickstarts" and in the download window click <Open> button; click <Open> again (ConfigSamples.exe will not harm your computer)

3. When "Congratulations!" window opens, click the <Launch> button, then the <Connect> button (if the page loads, installation was successful)

Page 10: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Visual Studio .NET Built-In Database ToolsVisual Studio .NET Built-In Database Tools

• Visual tools to create and manipulate databases– Server Explorer—established database connections,

creates database, tables, and stored procedures

– Table Designer—create columns in tables, view and edit table data

– Query and View Editor—graphically create database queries, and view results

– SQL Editor—create and edit SQL scripts and stored procedures, and view results

– Data Diagram—graphically define relationships between columns in two or more tables

Page 11: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Creating a SQL Server Database in Visual Studio .NETCreating a SQL Server Database in Visual Studio .NET

• 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

Page 12: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

The Table Designer (Page 1)The Table Designer (Page 1)

• Table Design view– Create schema or table structure in the Design pane

• Column Name (fieldname)—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—below the Design pane; available options depend upon field's data type

• Table Data view– Edit table data (create new row, modify or delete rows)

Page 13: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

The Table Designer ("Products" Table)The Table Designer ("Products" Table)

Page 14: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

The Table Designer (Page 2)The Table Designer (Page 2)

• Properties Pane entries for the int data type– Identity property, when set to a value of "Yes",

automatically provides a unique value for each record to locate a specific row of data

– Identity Seed—shows the initial value of first row

– Identity Increment—value by which to increment the seed each time a new row is added

– Therefore:• If seed is 1 and increment is 1, field values for the first

three records are 1, 2, 3…

• If seed is 1 and increment is 2, field values are 1, 3, 5…

Page 15: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

The Properties PaneThe Properties Pane

Page 16: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

The Table Designer (Page 3)The Table Designer (Page 3)

• 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

– Click the Row selector box; then click the "Set Primary Key" icon on Table toolbar

– A yellow "key" icon appears at the side of the column name(s)

Page 17: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

The Table Designer ("Products" Table)The Table Designer ("Products" Table)

Page 18: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

The Table Designer (Products Table)The Table Designer (Products Table)

Page 19: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Creating a View with Query and View Editor (Page 1)

Creating a View with Query and View Editor (Page 1)

• Used to create a query in the database• When one pane changes, the other panes updated– Table Pane—add tables and select columns (displays

the query visually with icons )

– Grid Pane—select columns and criteria, just when using Microsoft Access; column name and table indicate where to retrieve the values for column

– SQL Pane—generates and displays corresponding SQL statement

– Preview Pane—test run queries and displays results

Page 20: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Creating a View with Query and View EditorCreating a View with Query and View Editor

Page 21: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Creating a View with Query and View Editor (Page 2)

Creating a View with Query and View Editor (Page 2)

• The Grid pane elements:– Alias—display an alternate column name (useful for

calculated or concatenated columns)

– Output—should column be visible or not (some columns may be used only for criteria conditions)

– Sort type—ascending or descending

– Sort order—sorting hierarchy (indicate one or more columns to sort the results)

– Criteria—conditional statement (more than one entry indicates an AND condition)

– Or—indicates an alternative condition

Page 22: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Setting Relationships with Data DiagramSetting Relationships with Data Diagram

• Defines relationships (links) between tables– A line is drawn from a field from one table to another

which indicates the fields that define the relationship

– Referential integrity• The value of any row in the linked column of one table must

match a primary key value in the other, or be null

• Endpoints indicate type of relationship– One-to-many relationship

• Key at one endpoint and infinity symbol at the other

– One-to-one relationship• Key at each endpoint

Page 23: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Setting Relationships with Data DiagramSetting Relationships with Data Diagram

Foreign keyForeign key Primary keyPrimary key

Page 24: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Building Stored Procedures (Page 1)Building Stored Procedures (Page 1)

• A stored procedure is a SQL script (command) that is stored within the database:– Runs a SQL query (SELECT, INSERT, etc)

– Because stored procedures already have been parsed and compiled by the server, they are more efficient than a SQL statement in a View

– Values can be replaced with input parameters (represent variable values entered by user)

Page 25: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Building Stored Procedures (Page 2)Building Stored Procedures (Page 2)

• When a new procedure is begun, the editor inserts a skeletal stored procedure (template)

• Color codes the SQL keywords in blue• Comments are contained within /* and */ characters

and displayed in green text • The default behaviors of the editor may be changed in

the Options… dialog window on the Tools menu– Tab size, word wrapping, line numbers, etc.

Page 26: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Building Stored Procedures (Page 3)Building Stored Procedures (Page 3)

Page 27: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

The CREATE and ALTER ProceduresThe CREATE and ALTER Procedures

• When new procedure is being created, first statement is CREATE PROCEDURE

• The first line changes from CREATE PROCEDURE to ALTER PROCEDURE after the procedure has been created and saved (compiles when saved)

• Developer may change or alter the procedure using the ALTER PROCEDURE command

Page 28: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Modifying Stored Procedures with the SQL Query BuilderModifying Stored Procedures with the SQL Query Builder

• The SQL Query Builder is a tool within the Stored Procedure application

• It functions just like the Query and View Editor used for creating and editing views

• To use the SQL Query Builder1.Select the Edit Stored Procedure command for an

existing procedure

2.Right-click around the block of SQL code enclosed within the blue lines and select Design SQL Block

3.Use the Query and View editor as before

Page 29: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Data Retrieval in SQLData Retrieval in SQL

• The keyword SELECT is used in SQL to create queries that retrieve sets of rows (records) from one or more related tables in a relational database

• Basic format:SELECT columnName(s)

FROM tableName(s)

WHERE relationCondition;– SELECT clause is a comma-delimited list that limits

columns returned (required)

– WHERE clause is a condition that limits rows returned (optional)

Page 30: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

The WHERE ClauseThe WHERE Clause

• Base on a truth condition which limits which rows are returned in the query

• Relational operators are =, >, <, >=, <=, <>• Examples:

SELECT *

FROM Payables

WHERE [PO number] = "10003";

SELECT *

FROM [Purchase Order]

WHERE Subtotal >= 38.15;

Page 31: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Building Stored ProceduresBuilding Stored Procedures

Page 32: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Input Parameters in Stored Procedures (Page 1)

Input Parameters in Stored Procedures (Page 1)

• Variable values passed to the stored procedure– Parameters are input parameters by default

– Must match data type and length of the field that it references in the SQL query

– Name always begins with the symbol "@"

– The input parameter is often compared to a value in a SQL WHERE clause

• A default value can be provided– Must be a constant (not a variable) or it can be NULL

– Wildcard characters (*) are permitted

Page 33: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Input Parameters in Stored Procedures (Page 2)

Input Parameters in Stored Procedures (Page 2)

Page 34: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Input Parameters in Stored ProceduresInput Parameters in Stored Procedures

Page 35: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Output Parameters in Stored ProceduresOutput Parameters in Stored Procedures

• Send values back to the object that called stored the procedure – ReturnValue—a return value is passed back

– Called with RETURN keyword

– Retrieves identity column value (the primary key)

Return @@Identity

• Information such as number of records affected– Values can be integer, money, varchar - not text

Page 36: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

The SQL INSERT Statement (Page 1)The SQL INSERT Statement (Page 1)

• The INSERT command adds a row (record) to a table• Format

INSERT INTO tableName

[(columnNames)]

VALUES (value_list …);

• Example (values must match the number of items and data types in the original table):INSERT INTO Payables

VALUES ("10004", ”650", 1000, 3, .F.);

Page 37: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

The SQL INSERT Statement (Page 2)The SQL INSERT Statement (Page 2)

• Example (null values may be indicated by leaving space between commas where they occur in table):INSERT INTO Payables

VALUES ("10004", "701", , 8, .F.);

• Example (a columnName list may be used if not all columns will be assigned a value):INSERT INTO Payables,

([PO number], [Product number], Quantity, [Back ordered?])

VALUES ("10004", "701", 8, .F.);

Page 38: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Insert Stored Procedure with ParameterInsert Stored Procedure with Parameter

Page 39: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

Insert Stored Procedure with ParameterInsert Stored Procedure with Parameter

Page 40: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

WHERE with the LIKE Operator (Page 1)

WHERE with the LIKE Operator (Page 1)

• Uses the symbol "%" as a wildcard to substitute for any other character(s) in a WHERE clause

• Format:SELECT columnNames

FROM tableName

WHERE columnName LIKE "%criteriaString%";– One or more wildcard symbols may be inserted at

beginning middle or end of criteriaString

Page 41: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

WHERE with the LIKE Operator (Page 2)

WHERE with the LIKE Operator (Page 2)

• Example 1 (begins with the letter "P"):SELECT *

FROM Product

WHERE Product LIKE "P%";

• Example 2 (contains the letter "R"):SELECT *

FROM Product

WHERE Product LIKE "%R%";

Page 42: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET

WHERE with the LIKE Operator (Page 3)

WHERE with the LIKE Operator (Page 3)

Page 43: Managing MS SQL Server ® Data Sources in VS.NET ASP.NET