Managing MS SQL Server - PowerPoint PPT Presentation


PPT – Managing MS SQL Server PowerPoint presentation | free to download - id: 214650-ZDc1Z


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation

Managing MS SQL Server


If seed is 1 and increment is 1, field values for the first three records are 1, 2, 3... SQL Pane generates and displays corresponding SQL statement ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 44
Provided by: Kathlee


Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Managing MS SQL Server

Managing MS SQL Server Data Sources in VS.NET

Data 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

Universal 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
  • 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

Overview 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

Visual 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

General Steps for Using Web Databases
  • Build your database tables and queries/stored
  • 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
  • Create an ASP.NET Web page
  • Add ADO.NET connection object that connects to
    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

.NET Data Provider
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

ADO .NET Data Provider (continued)
  • ADO .NET Data Provider Objects
  • Connection establishes a connection to the data
  • 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
  • DataAdapter populates a DataSet
  • DataSet data disconnected from the database

.NET Managed Data Providers
ADO.NET Objects
  • SQL Server .NET data provider Prefix Sql
  • SqlConnection, SqlCommand, SqlDataReader,
  • OLE-DB data provider Prefix OleDb
  • OleDbConnection, OleDbCommand, OleDbDataReader,
  • Within the Web page, ASP.NET data controls are
    usually bound to DataReaders or DataSets.
  • DataSet is exposed, so it doesnt matter where
    the data came from, or what application it will
    be used in.

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

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 SourceWindamereServer User IDsa
    PASSWORD Initial CatalogMyDatabase"

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

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

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

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

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

The ADO .NET DataReader Object (continued)
  • Read
  • Returns single row and caches each row in memory
    only once
  • Mmoves 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

The ADO .NET DataReader Object (continued)
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
  • Parameter objects can be used

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
  • Displaying the data - data controls

The ADO .NET DataReader Object (continued)
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
  • Parameter objects can be used

The ADO .NET DataReader Object (continued)
  • Figure 6-3 (continued)
  • DataReader object
  • Contains information returned (number rows
  • No records are returned no data binding to

The ADO .NET DataReader Object (continued)
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

The ADO .NET DataAdapter and DataSet Objects
  • 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

The ADO .NET DataAdapter and DataSet Objects
  • 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
  • ConstraintCollection
  • includes information about the primary and
    foreign keys, and constraint rules

The ADO .NET DataAdapter and DataSet Objects
  • 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

The ADO .NET DataAdapter and DataSet Objects
The ADO .NET DataAdapter and DataSet Objects
The ADO .NET DataAdapter and DataSet Objects
  • 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 its in the DataSet,
    DataTables, or DataView
  • Bind DataSet, DataTables, or DataView to data
    controls within the Web page

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

Data Related Namespaces
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

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

Building Database Connections (continued)
  • Server property - server
  • Default name MSDE/SQL Server is
  • 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

Downloading Microsoft SQL Server Desktop Engine
(MSDE) Files
  • Create folder
  • md c\MSDETempDE
  • Download files and extract files
  • Command Window
  • cd C\sql2ksp3\MSDE
  • Verify files and folders installed
  • C cd\Program Files\Microsoft SQL
  • Binn, Data, Install, and Log folder should be

Installing .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 ltOpengt button click ltOpengt
    again (ConfigSamples.exe will not harm your
  3. When "Congratulations!" window opens, click the
    ltLaunchgt button, then the ltConnectgt button (if
    the page loads, installation was successful)

Visual Studio .NET Built-In Database Tools
  • Visual tools to create and manipulate databases
  • Server Explorerestablished database connections,
    creates database, tables, and stored procedures
  • Table Designercreate columns in tables, view and
    edit table data
  • Query and View Editorgraphically create database
    queries, and view results
  • SQL Editorcreate and edit SQL scripts and stored
    procedures, and view results
  • Data Diagramgraphically define relationships
    between columns in two or more tables

Creating a SQL Server Database in Visual Studio
  • 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
  • Needs permission to create database
  • See Appendix B for working with the MSDE and
    troubleshooting data connection problems

The Table Designer (Page 1)
  • Table Design view
  • Create schema or table structure in the Design
  • Column Name (fieldname)do not use blank spaces
    or special characters other than an underscore
  • Data Typeconvert to .NET data types when you
    retrieve your data using ADO.NET
  • Allow Nullsno value, a null value is returned
  • Properties Panebelow the Design pane available
    options depend upon field's data type
  • Table Data view
  • Edit table data (create new row, modify or delete

The Table Designer ("Products" Table)
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 Seedshows the initial value of first
  • Identity Incrementvalue 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

The Properties Pane
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)

The Table Designer ("Products" Table)
The Table Designer (Products Table)
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 Paneadd tables and select columns
    (displays the query visually with icons )
  • Grid Paneselect columns and criteria, just when
    using Microsoft Access column name and table
    indicate where to retrieve the values for column
  • SQL Panegenerates and displays corresponding SQL
  • Preview Panetest run queries and displays results

Creating a View with Query and View Editor
Creating a View with Query and View
Editor (Page 2)
  • The Grid pane elements
  • Aliasdisplay an alternate column name (useful
    for calculated or concatenated columns)
  • Outputshould column be visible or not (some
    columns may be used only for criteria conditions)
  • Sort typeascending or descending
  • Sort ordersorting hierarchy (indicate one or
    more columns to sort the results)
  • Criteriaconditional statement (more than one
    entry indicates an AND condition)
  • Orindicates an alternative condition

Setting 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
  • One-to-one relationship
  • Key at each endpoint

Setting Relationships with Data Diagram
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)

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.

Building Stored Procedures (Page 3)
Building Stored Procedures (Page 3)
  • CREATE PROCEDURE dbo.sp_ReorderProducts
  • /
  • (
  • _at_parameter1 datatype default value,
  • _at_parameter2 datatype OUTPUT
  • )
  • /
  • AS
  • SELECT FROM Products
  • WHERE QuantityAvailable lt MinOnHand

The 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

Modifying Stored Procedures with the SQL Query
  • 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 Builder
  • Select the Edit Stored Procedure command for an
    existing procedure
  • Right-click around the block of SQL code enclosed
    within the blue lines and select Design SQL Block
  • Use the Query and View editor as before

Data 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
  • 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)

The WHERE Clause
  • Base on a truth condition which limits which rows
    are returned in the query
  • Relational operators are , gt, lt, gt, lt, ltgt
  • Examples
  • FROM Payables
  • WHERE PO number "10003"
  • FROM Purchase Order
  • WHERE Subtotal gt 38.15

Building Stored Procedures
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 "_at_"
  • 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
  • Wildcard characters () are permitted

Input Parameters in Stored Procedures (Page 2)
Input Parameters in Stored Procedures (Page 2)
  • CREATE PROCEDURE dbo.sp_DisplayProduct
  • _at_param_ModelName nvarchar(50)
  • /
  • (
  • _at_parameter1 datatype default value,
  • _at_parameter2 datatype OUTPUT
  • )
  • /
  • AS
  • SELECT ProductID, ModelName,
  • UnitCost, ModelNumber, ProductImage
  • FROM Products
  • WHERE ModelName _at_param_ModelName

Input Parameters in Stored Procedures
Output Parameters in Stored Procedures
  • Send values back to the object that called stored
    the procedure
  • ReturnValuea return value is passed back
  • Called with RETURN keyword
  • Retrieves identity column value (the primary key)
  • Return _at__at_Identity
  • Information such as number of records affected
  • Values can be integer, money, varchar - not text

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.)

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
  • VALUES ("10004", "701", 8, .F.)

Insert Stored Procedure with Parameter
Insert Stored Procedure with Parameter
  • CREATE PROCEDURE dbo.sp_InsertCat
  • _at_param_CatName nvarchar(50)
  • /
  • (
  • _at_parameter1 datatype default value,
  • _at_parameter2 datatype OUTPUT
  • )
  • /
  • AS
  • INSERT INTO Categories (CategoryName)
  • VALUES (_at_param_CatName)
  • RETURN _at__at_Identity

Insert Stored Procedure with Parameter
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

WHERE with the LIKE Operator (Page 2)
  • Example 1 (begins with the letter "P")
  • FROM Product
  • WHERE Product LIKE "P"
  • Example 2 (contains the letter "R")
  • FROM Product
  • WHERE Product LIKE "R"

WHERE with the LIKE Operator (Page 3)
WHERE with the LIKE Operator (Page 3)
  • CREATE PROCEDURE dbo.sp_SearchProducts
  • _at_param_SearchProducts nvarchar(50)
  • /
  • (
  • _at_parameter1 datatype default value,
  • _at_parameter2 datatype OUTPUT
  • )
  • /
  • AS
  • SELECT ProductID, ModelName,
  • ModelNumber, UnitCost, ProductImage,
  • Manufacturer
  • FROM Products
  • WHERE ModelNumber LIKE ''
    _at_param_SearchProducts ''
  • OR ModelName LIKE '' _at_param_SearchProducts
  • OR Description LIKE '' _at_param_SearchProducts
  • OR Manufacturer LIKE '' _at_param_SearchProduct
    s ''

(No Transcript)