No Slide Title

Download Report

Transcript No Slide Title

70-320 XML Web Services
XML Web Services
Alistair Lawson, SoC
Bill Buchanan, SoC
W. Buchanan/A. Lawson (1)
XML Web Services
Date
Title
Kalani
Monday 4 June 11-12
Unit 0: Introduction to .NET
-
Monday 4 June 2-4pm
Unit 0: Introduction to .NET
-
Tuesday 5 June 10-12
Unit 1: Creating/Manipulating Datasets
Unit 1
Tuesday 5 June 2-4pm
Unit 1: Creating/Manipulating Datasets
Unit 1
Tuesday 11 June
Unit 2: Manipulating XML Data
Unit 2
Tuesday 12 June
Unit 2: Manipulating XML Data
Unit 2
Tuesday 18 June
Unit 3: .NET Remoting
Unit 3
Tuesday 19 June
Unit 3: .NET Remoting
Unit 3
Tuesday 25 June
Unit 4: Web Services
Unit 4
Tuesday 26 June
Unit 4: Web Services
Unit 5
W. Buchanan/A. Lawson (2)
30-720 Consuming/Manipulating
Data
XML Web Services
Alistair Lawson, SoC
Bill Buchanan, SoC
W. Buchanan/A. Lawson (3)
Consuming and Manipulating Data
Access and manipulate data from a Microsoft SQL Server™ database by creating
and using ad hoc queries and stored procedures.
XML Web Services
•Introduction to XML
•Create and manipulate DataSets.
•Manipulate a DataSet schema.
•Manipulate DataSet relationships.
•Create a strongly typed DataSet.
W. Buchanan/A. Lawson (4)
XML
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (5)
WWW languages of the past, especially HTML, have been fixed in their
tags. A new format named XML (eXtensible Markup Language) can be
used to create new tags, and provide a common platform for transferring
information between different systems and packages.
XML Web Services
The first line of an XML file typically contains an optional xml processing
instruction (known as the XML declaration). This can contain pseudoattributes that indicate the XML language version, the character set, and
whether it can be used as a standalone entity.
An example is the XML declaration that begins every valid XML file:
<?xml version="1.0" standalone="yes" ?>
W. Buchanan/A. Lawson (6)
XML Web Services
The XML document conforms to the XML
recommendations, and has a logical structure
that is composed of declarations, elements,
comments, character references, and
processing instructions.
It also has a physical structure which is
composed of entities, starting with the root, or
document entity.
W. Buchanan/A. Lawson (7)
XML Web Services
The XML object model defines a standard way in which the
elements of the XML structured tree are defined. It is fully
object-oriented and uses properties, methods, and the
actual content (data) contained in an object. This model
controls how users interpret the trees, and exposes all tree
elements as objects, which can be accessed without any
return trips to the server. The XML object model uses the
W3C standard know as Document Object Model.
W. Buchanan/A. Lawson (8)
XML-Data Reduced (XDR) is one of the first languages
defined which uses a schema (that is one that is
defined in an XML form. It defines the:
XML Web Services
Form of elements that are child elements of others.
Sequence in which the child elements can appear.
Number of child elements.
It also defines whether an element is empty or can
include text. XDR is now well established and uses
XML as its basic language. A new standard known as
XSD (XML Schema Definition) has been standardized
by the W3C XML Schema Working Group.
W. Buchanan/A. Lawson (9)
Root
XML Web Services
Children
W. Buchanan/A. Lawson (10)
Next Sibling
Document
root
Root
First Child
Child
Child
XML Web Services
Child
W. Buchanan/A. Lawson (11)
Document
root
Root
XML Web Services
First Child
Child
Root
Child
Child
Child
Child
Next Sibling
W. Buchanan/A. Lawson (12)
Document
root
Root
XML Web Services
Child
Child
Child
Root
Child
Child
W. Buchanan/A. Lawson (13)
XML Elements
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (14)
•
•
XML Web Services
•
An XML document can only have one document element. In XML a
document element is a single element that contains all the content that
is to be considered as past of the document itself. The document root is
the first element that appears in the XML document.
All XML elements must have end tags. In HTML, some tags do not
require an end tag, such as <P>, whereas XML requires that every tag
has an end tag.
XML elements cannot overlap. The XML tags must be properly
structured so that they do not overlap. For example the following is not
allow:
<tag1>Blah blah <tag2>Blah</tag1> Blah</tag2>
In this case the XML parser will stop after </tag1> as the parser
expects to find the </tag1> next.
W. Buchanan/A. Lawson (15)
• Attribute values must contain quotes, whether or not they
contain spaces. These quotes can either be a single invert
comma, or a double one.
• <, >, or & cannot be used within the text the document. For
these characters the entities of &lt;, &gt; and &amp; can be
used.
XML Web Services
For example: If we wanted the contents of a tag to be My <Web>
then the following would be used:
<MyTag>My &lt; Web&gt;</MyTag>
W. Buchanan/A. Lawson (16)
XML Web Services
An attribute is an XML structural construct. It has a name-value pair, separated
by an equals sign, and is included inside a tagged element that modifies the
features of an element. Thus, all attributes are string values. The following
shows an example of an attribute were id_value is set to “disk001”.
<?xml version="1.0"?>
<software>
<disk id_value="disk001">
<author>Smith, Fred</author>
<title>XML Developer's Guide</title>
<function>Disk tools</function>
<price>100</price>
<version>1.02</version>
<description>Windows XP version </description>
</disk>
</software>
Attributes
W. Buchanan/A. Lawson (17)
XML Web Services
An entity is an XML structural construct. It can be a file, a database
record, or any other item that contains data. Its main aim is to hold
content, and not structure, rules, or grammar. In the XML document,
each entity is has a unique name and contains its own content.
Entity
W. Buchanan/A. Lawson (18)
XML Format
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (19)
Prolog. This part is at the start of the document (or root element). It
contains information on the document as a whole. Typical
references are to the character encoding, document structure,
and style sheets. An example is:
XML Web Services
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="my_style.xsl"?>
<!DOCTYPE catalog SYSTEM "mycatalog.dtd">
<!--This file was last updated 2002-12-10-->
The prolog can include processing instructions, such as the xmlstylesheet processing instruction (as shown in the about
example). The <!-- …. --> statement contains a comment.
W. Buchanan/A. Lawson (20)
Document elements. The form the backbone of the XML documents, and creates
the required structure. Within it there the elements identify named sections of
information, and use markup tags that identify the name, start, and end of the
element.
All elements have names, and are case-sensitive and must start with a letter or an
underscore.
Tags establish boundaries around content. The start tags has the following format:
<elementName attrib1="attrib1Value" attrib2="attrib2Value"...>
XML Web Services
If an element name does not have any attributes then its format is:
<elementName >
and the end tag has the following format (which cannot have any attributes):
</elementName>
W. Buchanan/A. Lawson (21)
Elements are contained between a start and an end tag. For example:
<pet>
<type>Cat</type>
<name>Honey</name>
</pet>
where the pet element contains two other elements:
XML Web Services
<type> and <name>. The <type> tags contains the text ‘Cat’, and the <name>
element contains ‘Honey’. An empty tag can be used when there is no textual
content. An empty tag contains a slash (/) before the closing character, such as:
<help/>
which is the same as:
<help></help>
W. Buchanan/A. Lawson (22)
Family and tree metaphors are used to describe the relationship between elements.
All XML documents must contain a root element (which is also know as a
document). In the following XML document, <pet> is the root document:
<pet>
<type>Cat</type>
<name>Honey</name>
</pet>
XML Web Services
The following cannot be an XML document, as it has no root document:
<type>Cat</type>
<name>Honey</name>
With a tree structure, the leaves refer to elements that do not contain any other
elements (such as in a real tree, where leaves are ends of branches. These leaf
elements generally contain simple text or no textural information.
W. Buchanan/A. Lawson (23)
XML Web Services
The terms used in a family metaphors are such as parent, child,
ancestor, descendant, and sibling. These are used to describe
relationship between elements relative to each other (and not to the
entire document). The following abstract sample document illustrates
the relationships between elements.
<tag1>
<tag2>
<tag3>
<tag4/>
</tag3>
</tag2>
</tag1>
W. Buchanan/A. Lawson (24)
Manipulate a Data schema
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (25)
User
Interface
DataGrid
ListBox
CheckBox
DataView
XML Web Services
Data
Model
Data
Store
DataRow
DataSet
SqlReader
BinaryReader
StreamReader
XmlNode
SqlCommand
BinaryWriter
StreamWriter
XmlDocument
SqlAdapter
FileStream
FileStream
XmlTextReader
SqlConnection
Binary file
Text file
XML file
Database
W. Buchanan/A. Lawson (26)
XML Web Services
• The DataSet object thus provides a generalized abstraction of
the data source, which gets rid of any differences in the methods
used to store the data.
• The DataSet loaded into memory from the data source, and can
thus be accessed faster than reading the data from its source.
• The DataSet is stored in a relational way which allows one-toone and one-to-many relationships.
Data
Store
SqlReader
BinaryReader
StreamReader
XmlNode
SqlCommand
BinaryWriter
StreamWriter
XmlDocument
SqlAdapter
FileStream
FileStream
XmlTextReader
SqlConnection
Binary file
Text file
XML file
Database
W. Buchanan/A. Lawson (27)
XML Web Services
• The DataSet contains a schema, which defines the form of the
stored data, such as its structure and its data types. This is
defined as metadata. .NET provides an environment for the
creation, editing and updating for this schema. An important
element is that a strongly typed DataSet allows the data to be
checked at any early stage, and should result in fewer errors.
Data
Store
SqlReader
BinaryReader
StreamReader
XmlNode
SqlCommand
BinaryWriter
StreamWriter
XmlDocument
SqlAdapter
FileStream
FileStream
XmlTextReader
SqlConnection
Binary file
Text file
XML file
Database
W. Buchanan/A. Lawson (28)
XML Web Services
• The core of ADO.NET is the DataSet object, which mimics a
database store, and reduces the differences in various database
types, and can thus interface with many types of databases,
such as Oracle and Microsoft SQL Server.
Data
Store
SqlReader
BinaryReader
StreamReader
XmlNode
SqlCommand
BinaryWriter
StreamWriter
XmlDocument
SqlAdapter
FileStream
FileStream
XmlTextReader
SqlConnection
Binary file
Text file
XML file
Database
W. Buchanan/A. Lawson (29)
Creating an XML Schema
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (30)
.NET contains an XML
graphical designer
which can create the
DataSet schema.
XML Web Services
It can be created using
an editor.
It can be generated
from a data source,
such as from an XML
file.
Creating an XML Schema
W. Buchanan/A. Lawson (31)
XML Web Services
XML file
<?xml version="1.0" encoding="utf-8"?>
<diary>
<week number="1">
<monday>9:30 .NET meeting</monday>
<tuesday>10:00 Group meeting</tuesday>
<wednesday>HOLIDAY</wednesday>
<thursday>HOLIDAY</thursday>
<friday>HOLIDAY</friday>
</week>
<week number="2">
<monday>10:00 Training</monday>
<tuesday>15:00 Software Installation</tuesday>
<wednesday>9:00 AGM</wednesday>
<thursday>HOLIDAY</thursday>
<friday>HOLIDAY</friday>
</week>
<week number="3">
<monday>HOLIDAY</monday>
<tuesday>No meetings</tuesday>
<wednesday>All day Software Training</wednesday>
<thursday>HOLIDAY</thursday>
<friday>HOLIDAY</friday>
</week>
</diary>
XSD file
Defines the
format of the
data
W. Buchanan/A. Lawson (32)
XML Web Services
Initially an element tag is dragged on the design area, and its name is
changed to week (to correspond to the highest level of the tree).
W. Buchanan/A. Lawson (33)
XML Web Services
Next the other elements can be added, along with an attribute which is
created by dragging the attribute symbol onto the table.
W. Buchanan/A. Lawson (34)
XML Web Services
W. Buchanan/A. Lawson (35)
XML Web Services
<?xml version="1.0" ?>
<xs:schema id="diary" targetNamespace="http://tempuri.org/XMLFile1.xsd"
xmlns:mstns="http://tempuri.org/XMLFile1.xsd"
xmlns="http://tempuri.org/XMLFile1.xsd" >
<xs:element name="diary" msdata:EnforceConstraints="False">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="week">
<xs:complexType>
<xs:sequence>
<xs:element name="monday" type="xs:string" />
<xs:element name="tuesday" type="xs:string" />
<xs:element name="wednesday" type="xs:string" />
<xs:element name="thursday" type="xs:string" />
<xs:element name="friday" type="xs:string" />
</xs:sequence>
<xs:attribute name="number" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
While the XML schema provides an abstract definition of the structure and
type of the data, there are some incompatibilities when importing into other
packages. For example Microsoft Access does not interpret the attributes
tags. Also the element tag is used to define a hierarchy.
W. Buchanan/A. Lawson (36)
Tutorial Session 1:
XML Web Services
Page 13 and 14
W. Buchanan/A. Lawson (37)
Manipulate DataSet
Relationships
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (38)
Along with creating simple data definitions, the DataSet can also create
tables which have a linked relationship, in the same way that
databases created relationships.
XML Web Services
These relationships are defined with keys and can either be a one-to-one
or a one-to-many relationship. A primary key is one which identifies
uniquely identifies a row in a table of a database.
W. Buchanan/A. Lawson (39)
XML Web Services
Figure shows an example of three tables (Suppliers, Products and Orders). In the
Suppliers table there is a primary key for the SupplierID, each of which has a
Supplier Name (SupplierName), Address, City and Country. In the Products table,
there is a unique Products ID (ProductID), a Supplier ID (which relates to the ID in
the Suppliers table), a Product Description (ProductDescription), a Unit Price
(UnitPrice) and a field which defines if product is discontinued (Discontinued).
W. Buchanan/A. Lawson (40)
XML Web Services
To create a one-to-many relation, a record in one table must match to another in
another table. The one which has a key on it will be defined as the one, and other is
the many. For example, each supplier will have many products, thus we can drag
the SupplierID label from the Suppliers table onto the Products one.
W. Buchanan/A. Lawson (41)
XML Web Services
W. Buchanan/A. Lawson (42)
XML Web Services
W. Buchanan/A. Lawson (43)
XML Web Services
W. Buchanan/A. Lawson (44)
XML Web Services
Show XSD
W. Buchanan/A. Lawson (45)
XML Web Services
Along with one-to-many relationships, it is possible to created a nested relationship
(a one-to-one relationship), where complex abstractions can be simplified with a
nested structure. Figure shows an example of a table named Order Details which
contains the details of an order, which is a one-to-one relationship with an order. A
nested relationship is more in-tuned with the XML format of a nested hierarchy, with
a parent-child structure.
W. Buchanan/A. Lawson (46)
Tutorial Session 2:
XML Web Services
Page 20 and 21
W. Buchanan/A. Lawson (47)
Create a strongly typed
DataSet
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (48)
A key element to any programming language is the way that it checks for
errors in the code. This is normally achieved with a strong compiler and
with a strongly object-oriented approach to code development.
Unfortunately, a developer cannot normally check all of the required run
time options, such as whether a certain table in the database exists. For
example:
XML Web Services
SqlDataAdapter daEmployees = new SqlDataAdapter();
daEmployees.Fill(ds.”Employees”);
might fill up a data adapter from the “Employees” table within the
DataSet (ds). Unfortunately a column might be accessed incorrectly,
through a spelling mistake or if it has not been named correctly. Thus a
strongly typed allows for data to be bound at an early stage. It has the
following advantages:
W. Buchanan/A. Lawson (49)
XML Web Services
• The IntelliSense can be used to guide the developer as to the
range of options which can be used.
• It is faster in operation.
• It improves syntax, where data structured objects are
automatically created.
W. Buchanan/A. Lawson (50)
XML Web Services
This can be achieved by opening the data source, and dragging it into the solution. The
basic operation is:
1. Open up the data source on the server.
2. Drag the table onto the form.
3. Create a dataset by selecting the data adapters, and select Generate DataSet.
W. Buchanan/A. Lawson (51)
XML Web Services
dsSupplier.cs. This contains the code to interface to the strongly typed data.
dsSuppliers.xsx. This contains details on the layout of the objects on the designer.
W. Buchanan/A. Lawson (52)
XML Web Services
W. Buchanan/A. Lawson (53)
The strongly typed dataset object can be used by filling up
the dataset with the database values, and then assigning it
to a data grid, such as:
XML Web Services
private void Form1_Load(object sender, System.EventArgs e)
{
oleDbDataAdapter1.Fill(dsSupplier1,"Suppliers");
dgSuppliers.DataSource = dsSupplier1;
dgSuppliers.DataMember = "Suppliers";
}
W. Buchanan/A. Lawson (54)
XML Web Services
If can be seen in Figure where the IntelliSense has picked up the columns of
the table (with AddressColumn, CityColumn, and so on). The result is shown
next.
W. Buchanan/A. Lawson (55)
XML Web Services
W. Buchanan/A. Lawson (56)
Tutorial Session 3:
XML Web Services
Page 25
W. Buchanan/A. Lawson (57)
SQL/.NET test
XML Web Services
http://buchananweb.co.uk/agilenttest.html
W. Buchanan/A. Lawson (58)
Introduction to Databases
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (59)
XML Web Services
Data
Layer
Business Logic
Layer
Presentation
Layer
W. Buchanan/A. Lawson (60)
XML Web Services
Data
Layer
Business Logic
Layer
Presentation
Layer
Data Layer
This manages the
data used by the
application. An
application that
stores its data in
data files is said to
implement the data
layer itself. Many
applications use a
database to
manage the storage
of data. In these
cases, the
database itself is
considered to be
the data layer of the
application.
W. Buchanan/A. Lawson (61)
XML Web Services
Data
Layer
Business Logic
Layer
Presentation
Layer
Business Logic
This layer builds on
the data layer, and
contains the various
rules and
operations that the
application
performs on its
data. For example,
when a user runs a
query on a
database, the data
layer stores the
order details while
the business logic
layer performs all
the generation of
the query in the
correct format,
validates the
parameters and
ensures all the
relevant
information
W. Buchanan/A.
Lawson
(62)
XML Web Services
Data
Layer
Presentation
This layer builds on
the business logic
layer and presents
and manages the
display which
enables the user to
interact with the
system. Graphical
user interfaces
(GUI) and web
pages are typical
examples of a
presentation layer.
Business Logic
Layer
Presentation
Layer
W. Buchanan/A. Lawson (63)
User
Interface
DataGrid
ListBox
CheckBox
DataView
XML Web Services
Data
Model
Data
Store
DataRow
DataSet
SqlReader
BinaryReader
StreamReader
XmlNode
SqlCommand
BinaryWriter
StreamWriter
XmlDocument
SqlAdapter
FileStream
FileStream
XmlTextReader
SqlConnection
Binary file
Text file
XML file
Database
W. Buchanan/A. Lawson (64)
SQL Server Access
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (65)
Create Connection. SqlConnection.
Create DataAdapter. SqlDataAdapter.
Run Query. SqlCommand.
Create Reader/Writer. SqlDataReader.
XML Web Services
SqlConnection cnn = new SqlConnection();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText =
"SELECT LastName, FirstName FROM Employees "+
"ORDER BY LastName";
SqlDataReader dr = cmd.ExecuteReader();
W. Buchanan/A. Lawson (66)
System.Data.SqlClient
SqlConnection
Close()
SqlCommand
ConnectionString
SqlDataReader
Open()
XML Web Services
SqlDataAdapter
SqlError
SqlParameter
System.Data.SqlClient
W. Buchanan/A. Lawson (67)
System.Data.SqlClient
SqlConnection
Close()
SqlCommand
ConnectionString
SqlDataReader
Open()
XML Web Services
SqlDataAdapter
SqlError
SqlParameter
System.Data.SqlClient
W. Buchanan/A. Lawson (68)
SQL Server Queries
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (69)
Ad-hoc query
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText =
"SELECT LastName, FirstName FROM Employees "+
"ORDER BY LastName";
XML Web Services
Stored Procedure
CREATE PROCEDURE procCustomerSales (@CustomerID char(5),
@TotalSales money OUTPUT
)
AS
SELECT @TotalSales = SUM(Quantity * UnitPrice)
FROM ((Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID)
INNER JOIN [Order Details]
ON Orders.OrderID = [Order Details].OrderID)
WHERE Customers.CustomerID = @CustomerID
SqlCommand cmd = sqlConnection1.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "procEmployeeSales";
cmd.ExecuteNonQuery();
W. Buchanan/A. Lawson (70)
The main methods to conduct Ad-hoc queries are:
XML Web Services
- Visual Studio .NET IDE.
- OSQL.
- SQL Query Analyzer.
- ASP.NET Application
W. Buchanan/A. Lawson (71)
Queries using the .NET IDE
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (72)
XML Web Services
Add New View
Filtering
W. Buchanan/A. Lawson (73)
Diagram pane
Grid pane
XML Web Services
SQL pane
Results pane
Diagram pane. This contains the tables
which are to be used in the query.
Grid pane. This the columns which are to
be used in the query.
SQL pane. This shows the SQL code
which has been generated for the query.
Results pane. This shows the results of
the query, after it has been executed.
Filtering
W. Buchanan/A. Lawson (74)
Diagram pane
Grid pane
XML Web Services
SQL pane
Results pane
Filtering
W. Buchanan/A. Lawson (75)
Whitespace, tabs and
newlines have no
effect on the command
XML Web Services
dbo is the default owner
of the database.
SELECT
FROM
ORDER BY
Filtering
TOP 100 PERCENT ProductName, SupplierID, CategoryID
dbo.Products
SupplierID
W. Buchanan/A. Lawson (76)
XML Web Services
SELECT
FROM
WHERE
Filtering
CompanyName, City, Country
dbo.Customers
(Country = 'GERMANY') AND (City <> 'BERLIN')
W. Buchanan/A. Lawson (77)
XML Web Services
W. Buchanan/A. Lawson (78)
XML Web Services
W. Buchanan/A. Lawson (79)
XML Web Services
W. Buchanan/A. Lawson (80)
XML Web Services
W. Buchanan/A. Lawson (81)
XML Web Services
private void Page_Load(object sender, System.EventArgs e)
{
sqlDataAdapter1.Fill(dsCustomers1,"vwGermanyNotBerlin");
DataGrid1.DataBind();
}
W. Buchanan/A. Lawson (82)
XML Web Services
W. Buchanan/A. Lawson (83)
SQL Server Queries using osql
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (84)
XML Web Services
C:\ >osql /?
usage: osql
[-U login id]
[-P password]
[-S server]
[-H hostname]
[-E trusted connection]
[-d use database name] [-l login timeout]
[-t query timeout]
[-h headers]
[-s colseparator]
[-w columnwidth]
[-a packetsize]
[-e echo input]
[-I Enable Quoted Id]
[-L list servers]
[-c cmdend]
[-D ODBC DSN name]
[-q "cmdline query"]
[-Q "cmdline query" and exit]
[-n remove numbering] [-m errorlevel]
[-r msgs to stderr]
[-V severitylevel]
[-i inputfile]
[-o outputfile]
[-p print statistics] [-b On error batch abort]
[-X[1] disable commands [and exit with warning]]
[-O use Old ISQL behavior disables the following]
<EOF> batch processing
Auto console width scaling
Wide messages
default errorlevel is -1 vs 1
[-? show syntax summary]
W. Buchanan/A. Lawson (85)
XML Web Services
1> select companyname, city, country
2> from customers
3> where (country='GERMANY') and (city<>'BERLIN')
4> go
companyname
city
---------------------------------------- --------------Drachenblut Delikatessen
Aachen
Königlich Essen
Brandenburg
QUICK-Stop
Cunewalde
Lehmanns Marktstand
Frankfurt a.M.
Ottilies Käseladen
Köln
Morgenstern Gesundkost
Leipzig
Blauer See Delikatessen
Mannheim
Frankenversand
München
Toms Spezialitäten
Münster
Die Wandernde Kuh
Stuttgart
(10 rows affected)
country
--------Germany
Germany
Germany
Germany
Germany
Germany
Germany
Germany
Germany
Germany
W. Buchanan/A. Lawson (86)
SQL Server Query Analyzer
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (87)
XML Web Services
The SQL Query Analyzer is available from the
SQL Server, and allows for multiple queries, an
object browser, performance analysis tools and
templates for common queries.
W. Buchanan/A. Lawson (88)
Using a .NET Form for the
Query
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (89)
XML Web Services
Run command Create command
Ad-hoc query
private void btnCommand_Click(object sender,
System.EventArgs e)
{
SqlCommand cmd = sqlConnection1.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = txtQuery.Text;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds,"Results");
dgResults.DataSource =ds;
dgResults.DataMember = "Results";
dgResults.DataBind();
}
W. Buchanan/A. Lawson (90)
XML Web Services
W. Buchanan/A. Lawson (91)
SQL Server Queries
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (92)
The outline format of the SELECT statement is:
from_field
from_table
clause
clause
clause
clause
XML Web Services
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
W. Buchanan/A. Lawson (93)
XML Web Services
SELECT * from Employees
W. Buchanan/A. Lawson (94)
XML Web Services
SELECT LastName,FirstName from Employees
W. Buchanan/A. Lawson (95)
Server should
cast the return value
as an integer
XML Web Services
SELECT ProductName,UnitsInStock,UnitsOnOrder,
CAST (UnitsInStock+UnitsOnOrder AS INTEGER) AS Stock
FROM Products
W. Buchanan/A. Lawson (96)
XML Web Services
SELECT ProductName,UnitsInStock,UnitsOnOrder
FROM Products
WHERE UnitsInStock>50
W. Buchanan/A. Lawson (97)
XML Web Services
SELECT ProductName,UnitsInStock,UnitsOnOrder
FROM Products
WHERE UnitsInStock>20
AND UnitsOnOrder>5
W. Buchanan/A. Lawson (98)
XML Web Services
SELECT ProductName,UnitsInStock,UnitsOnOrder
FROM Products
WHERE UnitsInStock>20
OR UnitsOnOrder>5
W. Buchanan/A. Lawson (99)
XML Web Services
SELECT Employees.EmployeeID, LastName, FirstName, OrderID,
CustomerID
FROM Employees INNER JOIN Orders
ON Employees.EmployeeID = Orders.EmployeeID
W. Buchanan/A. Lawson (100)
XML Web Services
SELECT *
FROM Products
WHERE ProductName LIKE 'Ch%'
W. Buchanan/A. Lawson (101)
XML Web Services
SELECT *
FROM Products
WHERE ProductName LIKE 'Cha_'
W. Buchanan/A. Lawson (102)
XML Web Services
SELECT *
FROM Products
ORDER BY ProductName DESC
W. Buchanan/A. Lawson (103)
XML Web Services
SELECT *
FROM Products
ORDER BY Len(ProductName) ASC
W. Buchanan/A. Lawson (104)
XML Web Services
SELECT ProductID, Sum(Quantity) AS TOTALSALES
FROM [Order Details]
GROUP BY ProductID
W. Buchanan/A. Lawson (105)
Tutorial Session:
XML Web Services
Page 38 and 39
W. Buchanan/A. Lawson (106)
XML Web Services
W. Buchanan/A. Lawson (107)
SQL Server Stored Procedures
XML Web Services
Alistair Lawson
Introduction
W. Buchanan/A. Lawson (108)
XML Web Services
Ad-hoc queries are useful in allowing users to directly interface their SQL
commands to the database, and in testing the query, but, in most cases,
a stored procedure is used to store the query. These stored procedures
are generic in their nature, and can be reused in other applications.
CREATE PROCEDURE procGermanyCustomersNotBerlin
AS
SELECT * FROM Customers
WHERE Country = 'Germany' and City<>'Berlin'
W. Buchanan/A. Lawson (109)
XML Web Services
CREATE PROCEDURE procGermanyCustomersNotBerlin
AS
SELECT * FROM Customers
WHERE Country = 'Germany' and City<>'Berlin'
W. Buchanan/A. Lawson (110)
XML Web Services
W. Buchanan/A. Lawson (111)
private void Page_Load(object sender,
System.EventArgs e)
{
SqlCommand cmd = sqlConnection1.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "procGermanyCustomersNotBerlin";
XML Web Services
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds,"Results");
dgResults.DataSource =ds;
dgResults.DataMember = "Results";
dgResults.DataBind();
}
W. Buchanan/A. Lawson (112)
XML Web Services
W. Buchanan/A. Lawson (113)
XML Web Services
@parameter1 datatype = default value,
@parameter2 datatype OUTPUT
ALTER PROCEDURE procEmployeeSales
(
@EmployeeID int,
@TotalSales money OUTPUT
)
AS
SELECT @TotalSales = SUM(Quantity * UnitPrice)
FROM ((Employees INNER JOIN Orders
ON Employees.EmployeeID = Orders.EmployeeID)
INNER JOIN [Order Details]
ON Orders.OrderID = [Order Details].OrderID)
WHERE Employees.EmployeeID = @EmployeeID
W. Buchanan/A. Lawson (114)
XML Web Services
W. Buchanan/A. Lawson (115)
XML Web Services
Each table must have a single entry column, which contains a
unique value which has been assigned by the server. Examples
of this include EmployeeID in the Employees table, and OrderID
in the Orders table. The @@INDENTITY can be used to generate
a unique entry for a new row.
W. Buchanan/A. Lawson (116)
XML Web Services
ALTER PROCEDURE procInsertEmployee
(
@NewEmployee char(20),
@EmployeeID int OUTPUT
)
AS
INSERT INTO Employees (LastName, FirstName)
VALUES (@NewEmployee, 'Fred')
SELECT @EmployeeID = @@IDENTITY
RETURN
@@IDENTITY
W. Buchanan/A. Lawson (117)
XML Web Services
W. Buchanan/A. Lawson (118)
Tutorial Session
XML Web Services
Page 45
W. Buchanan/A. Lawson (119)
XML Web Services
Cram Session
W. Buchanan/A. Lawson (120)
DataSets are made up of XML elements.
Default file extension for XML schema is .xsd
XML schema represents metadata that describes a DataSet object's
allowable content
XML Web Services
Creating XML schema: Drag and drop elements into the DataSet schema
designer.
Simple types allow constraints on the data in the DataSet.
Schema created from an existing table by dragging it into the DataSet
schema designer.
ADO.NET: Contains data-specific data provider classes and databaseindependent DataSet classes
W. Buchanan/A. Lawson (121)
SqlConnection: Object provides the connection to a database.
SqlCommand: Object represents the command to be executed on a
database server.
SqlParameter: Object supports the passing of a parameter to a stored
procedure in a database.
XML Web Services
SqlDataReader: Object is a fast way to retrieve a resultset from a command
passed to a database.
SqlDataAdapter: Object supports a two-way pipeline between a database
and the data model
DataSet: Object represents the structure and data of a relational database in
memory.
DataView: Object is used to provide a filtered row of data from a DataTable.
W. Buchanan/A. Lawson (122)
DataTable, DataRelation, DataRow and DataColumn. objects are contained
within the DataSet object.
SQL statements are case-insensitive.
Running queries: Within the Visual Studio enviroment; Using OSQL; Using
SQL Query Analyser; Using an ASP.NET application
XML Web Services
SELECT * FROM EMPLOYEES: access all the columns in the Employees
table.
dbo part of dbo.Employees. dbo is the default database owner.
Why are square brackets used: When a table name has spaces in it.
Microsoft SQL Server dialect used to implement the ANSI SQL-92 language:
Transact-SQL
W. Buchanan/A. Lawson (123)
SELECT: statement is used to retrieve data from tables in a database.
INSERT: statement is used to add data from tables in a database.
UPDATE: statement is used to modify existing data from tables in a
database.
DELETE: statement is used to remove data from tables in a database.
XML Web Services
WHERE: SQL clause is used to restrict the output of an SQL statement.
HAVING: restricts the rows used as input to an aggregate in an SQL
statement.
Stored procedure: Most prefer method of invoking a query on a database.
W. Buchanan/A. Lawson (124)
@@INDENTITY variable: Create a unique value for a primary key.
First line of a stored procedure which is named "MyProc“: CREATE
PROCEDURE MyProc
use Northwind: OSQL is used to load the Northwind database.
go: OSQL is used to run a query.
XML Web Services
OSQL: command-line utility which is used to run SQL queries.
XML element: Standalone XML entity.
W. Buchanan/A. Lawson (125)
XML attribute: further defines an element.
Simple data type: Restriction in a data range between 0
and 100 be set in the XML schema designer.
XML Web Services
Facet in XML: piece of information describing an XML
simple type.
When is a one-to-many relationship most useful in an
XML schema: Used with a relational database.
Difference between ad-hoc queries and stored
procedures: Ad-hoc queries are sent to the server, but
stored procedures are stored on the server.
W. Buchanan/A. Lawson (126)
XML Web Services
Namespaces including in the ADO.NET model:
System.Data
System.Data.SqlTypes
System.Data.SqlClient
System.Data.OleDb
W. Buchanan/A. Lawson (127)