Académique Documents
Professionnel Documents
Culture Documents
API
A database API defines how to code an application to connect to a database and pass
commands to the database. An object model API is usually language independent and
defines a set of objects, properties, and interfaces, while a C or Microsoft Visual Basic
API defines a set of functions for applications written in C, C++, or Visual Basic.
Database language
A database language defines the syntax of the commands sent to the database. The
commands sent through the API allow the application to access and modify data. They
also allow the application to create and modify objects in the database. All commands
are subject to the permissions granted to the user.
This section discusses:
The APIs supported by SQL Server and the issues to consider when choosing which API
to use in an application.
Transact-SQL
The database language supported by SQL Server is Transact-SQL. Transact-SQL complies
with the Entry Level of the SQL-92 standard, but also supports several features from the
Intermediate and Full Levels. It also supports some powerful extensions to the SQL-92
standard. For more information, see Transact-SQL in Microsoft SQL Server Transact-SQL
and Utilities Reference.
The ODBC specification defined extensions to the SQL defined in the SQL-92 standard.
These ODBC SQL extensions are also supported by OLE DB. Transact-SQL supports the
ODBC extensions from applications using the Microsoft ActiveX Data Objects (ADO), OLE
DB, or ODBC APIs, or the APIs that map over ODBC.
APIs Supported by SQL Server
SQL Server supports a number of APIs for building general-purpose database applications:
Open APIs with publicly defined specifications supported by several database vendors:
OLE DB
Open Database Connectivity (ODBC) and the object APIs built over ODBC: Remote
Data Objects (RDO) and Data Access Objects (DAO)
The legacy DB-Library API developed specifically to be used with earlier versions of SQL
Server that predate the SQL-92 standard.
Choosing an API
The general-purpose APIs recommended for use in new applications using Microsoft SQL
Server 7.0 are:
Microsoft ActiveX Data Objects (ADO) for most database applications. ADO supports
rapid development of robust applications and has access to most SQL Server features.
The SQL Server features needed by most applications are supported by ADO when
using the Microsoft OLE DB Provider for SQL Server.
OLE DB for COM-based tools and utilities, or COM-based system level development
needing either top performance or access to SQL Server features not exposed through
ADO. The OLE DB Provider for SQL Server uses provider-specific properties, interfaces,
and methods to expose SQL Server features not covered by the OLE DB specification.
Most of these provider-specific features are not exposed through ADO.
ODBC for the same class of applications as are listed above for OLE DB, but which are
not based on COM.
There are several factors to consider when selecting which general-purpose APIs to use in a
SQL Server application:
Existing
Existing API specifications are mature, stable specifications. Supplementary
information about the API is readily available in third-party books and classes. There
is an existing pool of programmers familiar with the API.
Emerging
Emerging API specifications are recent and may be evolving rapidly. Supplementary
information about the latest version of the API may be scarce. There are relatively
few programmers available who have used the API, although programmers familiar
with a similar API can be retrained quickly.
Legacy
Legacy API specifications are stable but unchanging. They may not support new
features, and are likely to be discontinued at a future date. Information about the API
is readily available, but the pool of programmers familiar with the API may be
shrinking.
Overhead:
Native APIs
Native APIs are low-level APIs implemented with providers or drivers that
communicate directly to SQL Server using the Tabular Data Stream (TDS) protocol.
They are relatively complex APIs, but offer the best performance because they have
the least overhead.
Hosted APIs
Hosted APIs also encapsulate a native API, but do not use an object model. The
efficiency, ease-of-use, and feature-set issues for hosted APIs are similar to those
for object model APIs.
The following table maps the general-purpose database APIs supported by SQL Server to
the issues listed earlier.
De SQ
gr L
A M Ov ee Ser
PI at erh of ver
uri ea de 7.0
ty d vel fea
op tur
er e
co su
ntr pp
ol ort
OL
AP
Ser
vic
es
feat
ure
sup
por
t
La
ng
ua
ge
su
pp
ort
A ga ect
O cy Mo
del
ove
r
OD
BC
E Le Ho
S ga ste
Q cy d
L
ove
r
DB
Lib
rar
y
D Le Nat
B ga ive
- cy
Li
br
ar
y
fo
r
C
w ited
ual
Ba
sic
Vis
ual
C+
+
Lo Lim No Vis
w ited
ual
C+
+
CO
BO
L
Hi Lim No Vis
gh ited
ual
C+
+
Additional APIs
SQL Server also supports a number of interfaces that allow applications to make full use of
all SQL Server features:
Microsoft Visual Basic, Microsoft Visual C++, Microsoft Visual J++, and Microsoft Visual
FoxPro.
ADO applications access data through OLE DB providers. Microsoft SQL Server version 7.0
introduces a native Microsoft OLE DB Provider for SQL Server that can be used by ADO
applications to access the data in SQL Server. In earlier versions of SQL Server, ADO
applications had to use the OLE DB Provider for ODBC layered over the Microsoft SQL
Server ODBC driver. While ADO applications can still use the OLE DB Provider for ODBC
with the SQL Server ODBC driver, it is more efficient to only use the OLE DB Provider for
SQL Server.
ADO is the API most recommended for general purpose data access from SQL Server. ADO:
The core capabilities of the OLE DB specification provides all the data access functionality
needed by most applications. In addition, OLE DB allows individual providers to define
provider-specific mechanisms to support additional features of the data engine accessed by
the provider. ADO exposes the core capabilities of OLE DB, but does not expose providerspecific features. ADO applications cannot access a few SQL Server features exposed
through provider-specific features of the OLE DB Provider for SQL Server, such as the
IRowsetFastLoad bulk copy methods, SQL Server-specific extended diagnostic information,
and auto-fetch cursors.
ADO has evolved from the earlier, ODBC-based Remote Data Objects (RDO) and Data
Access Objects (DAO) APIs. RDO and DAO applications can be converted to ADO, and
RDO and DAO application programmers quickly learn ADO. ADO is used extensively in
Active Server Pages (ASP).
See Also
In This Volume
Programming ADO
In Other Volumes
SQL Server and ADO in Microsoft SQL Server Building Applications
A LINKEDSERVERS rowset that exposes catalog information from the linked servers
used in SQL Server distributed queries.
See Also
In Other Volumes
SQL Server and OLE DB in Microsoft SQL Server Building Applications
The X/Open CAE Specification Data Management: SQL Call-Level Interface (CLI)
ODBC has been widely accepted by database programmers, and several database vendors
or third-party companies supply ODBC drivers. Several other Microsoft data access APIs
were defined as simplified object models over ODBC:
Microsoft SQL Server includes a native Microsoft SQL Server ODBC driver that can be used
by ODBC applications to access the data in SQL Server. The SQL Server ODBC Driver
complies with Level 2 of the ODBC 3.51 specification and exposes all the features of SQL
Server. In SQL Server version 7.0 all of the SQL Server utilities except isql use the ODBC
API and the SQL Server ODBC Driver.
ODBC can be used in tools, utilities, or system level development needing either top
performance or access to SQL Server features, and which are not COM applications. ODBC,
like OLE DB, allows individual drivers to define driver-specific mechanisms to support
additional features of the data engine accessed by the driver. These features include:
A set of bulk copy functions based on the earlier DB-Library bulk copy functions.
Extensions to the ODBC diagnostic functions and records to get SQL Server-specific
information from messages and errors.
A set of functions that exposes catalog information from the linked servers used in SQL
Server distributed queries.
Various driver-specific attributes and connection string keywords to control SQL Serverspecific behaviors.
See Also
In Other Volumes
SQL Server and ODBC in Microsoft SQL Server Building Applications
SQL-DMO API
SQL Distributed Management Objects (SQL-DMO) encapsulate the objects found in
Microsoft SQL Server databases. SQL-DMO allows applications written in languages that
support OLE Automation or COM to administer all parts of a SQL Server installation. SQLDMO is the API used by the SQL Server version 7.0 SQL Server Enterprise Manager, so
applications using SQL-DMO can perform all functions performed by SQL Server Enterprise
Manager.
SQL-DMO is intended for any OLE Automation or COM application that needs to incorporate
SQL Server administration. Examples include:
Applications that encapsulate SQL Server as their data store and want to shield users
from as much SQL Server administration as possible.
Applications that have specialized administrative logic incorporated the application itself.
Applications that want to integrate SQL Server administrative tasks in their own user
interface.
See Also
In This Volume
Programming ADO
In Other Volumes
Developing SQL-DMO Applications in Microsoft SQL Server Distributed Management
Objects
Wizards
When an application uses the SQL-NS objects, SQL Server Enterprise Manager must be
installed on any client that attempts to run the SQL-NS application.
See Also
In This Volume
Administration Architecture
In Other Volumes
Programming SQL-NS Applications in Microsoft SQL Server Building Applications
Microsoft ActiveX controls that allow you to provide the functionality of the Distribution
Agent or the Merge Agent in custom programs.
See Also
In This Volume
Administration Architecture
In Other Volumes
Programming Replication ActiveX Controls in Microsoft SQL Server Building Applications
Programming Transactional and Snapshot Replication from Heterogeneous Data Sources
in Microsoft SQL Server Building Applications
Overview of Replication in Microsoft SQL Server Distributed Data Operations and
Replication
Developing SQL-DMO Applications in Microsoft SQL Server Distributed Management
Objects
1. An administrator defines an English Query domain for the database and uses that to
compile what is called an English Query application file. The English Query application
file is what defines the structure of the database to the English Query run-time engine.
An English Query domain contains:
Definitions of entities, which are usually associated with tables and columns of the
database.
2. The domain is defined with a domain editor and tested with a test tool. The result is
saved as a domain project (or *.eqp file). This file is compiled to form the English Query
application file (*.eqd).
English Query is an OLE Automation server that can be called from any OLE Automation
application. The OLE Automation server, or run-time engine, exposes an object model API.
An OLE Automation application uses the API to load the English Query application file and
then send the run-time engine natural-language questions. The run-time engine usually
returns an SQL statement or batch that will retrieve the needed information from the SQL
Server database. Sometimes the run-time engine can provide the answer directly, request
clarification, or return an error.
See Also
In This Volume
Installing Microsoft English Query
Gateways
Gateways are server applications that allow applications written to access SQL Server to
work with other databases. The need for gateway applications has largely been made
obsolete by newer technologies such as SQL Server distributed queries, Windows NT
Component Services, and general database APIs such as ADO and OLE DB.
See Also
In Other Volumes
Programming Open Data Services in SQL Server Books Online
The source generated by the precompiler is then compiled using the compiler for the
application programming language.
Embedded SQL has a simpler syntax than COM APIs such as OLE DB or Call Level
Interfaces such as ODBC, so it is easier to learn and program. It is less flexible than OLE DB
or ODBC, where well-written applications can switch from one DBMS to another by simply
switching drivers or providers. OLE DB and ODBC are also better at dealing with
environments where the SQL statements are not known when the application is compiled,
such as when developing ad hoc query tools.
Microsoft SQL Server provides an Embedded SQL precompiler for C applications. The SQL
Server precompiler translates Embedded SQL statements as calls to the appropriate DBLibrary API functions. The Microsoft implementation of ESQL has the same restrictions as
DB-Library applications.
SQL Server is designed such that it can support COBOL Embedded-SQL applications
compiled with third-party Embedded SQL precompilers that support Microsoft SQL Server.
See Also
In Other Volumes
Programming Embedded SQL for C in SQL Server Books Online
DB-Library API
DB-Library is a Call Level Interface that allows C applications to access Microsoft SQL
Server. Microsoft also provides a closely matched API that provides access to the DB-Library
functions from Microsoft Visual Basic applications. DB-Library for Visual Basic supports a
subset of the functions in DB-Library for C. DB-Library was the original API that allowed
applications to access SQL Server, and remains specific to SQL Server.
The DB-Library API has not been enhanced beyond the level of SQL Server 6.5. All DBLibrary applications can work with SQL Server 7.0, but only as 6.5 level clients. New features
introduced in SQL Server 7.0 are not supported for DB-Library applications.
SQL Server 7.0 does not include a programming environment for DB-Library for Visual
Basic. Existing DB-Library for Visual Basic applications can run against SQL Server 7.0, but
must be maintained using the software development tools from SQL Server 6.5. All
development of new Visual Basic applications that access SQL Server should use the Visual
Basic data APIs such as ActiveX Data Objects (ADO) and Remote Data Objects (RDO).
See Also
In This Volume
DB-Library for C Reference
Use the ODBC extensions to SQL-92 whenever they provide functionality needed by the
application that SQL-92 does not support.
Use the native SQL syntax of the database engine whenever it provides functionality
needed by the application that SQL-92 and the ODBC extensions do not support.
This approach minimizes the overhead of OLE DB providers and ODBC drivers. The
providers and drivers only have to parse incoming SQL statements for ODBC escape
sequences or SQL-92 syntax not accepted by the database. Any ODBC escape sequences
and unsupported SQL-92 syntax are transformed into the corresponding SQL syntax
accepted by the database engine. All other SQL syntax is passed through to the database
engine.
SQL Server 7.0 applications using OLE DB, ODBC, or one of the other APIs that
encapsulate these two, should follow these guidelines and:
Use SQL-92 syntax whenever it provides the functionality needed by the application.
Use ODBC escape sequences when they provide functionality needed by the application
but not provided by SQL-92.
Use Transact-SQL syntax when it provides functionality needed by the application but
not provided by SQL-92 or the ODBC escape sequences.
Using SQL with DB-Library and Embedded SQL
DB-Library supports only Transact-SQL. DB-Library does not support the ODBC escape
sequences.
ESQL/C supports only the SQL syntax defined in Embedded SQL for C and SQL Server.
English Query
English Query makes a definition of the entities and relationships defined in a SQL Server
database. Given this definition, an application can use an OLE Automation API to pass
English Query a string containing a natural-language question about the data in the
database. English Query returns an SQL statement the application can use to extract the
data needed to answer the question.
Many database administrators have developed database-driven Web sites using Active
Server Pages (ASP), Microsoft ActiveX Data Objects (ADO), and SQL Server or another
database server only to find it difficult for their users to search the database and report on it.
It is easy enough to build a query form that allows users to search based on one or two
fields, but it is much more difficult to build form-based Web pages that allow searches across
multiple tables and multiple fields. A more flexible search is difficult to implement (there are
many problems beyond the user-interface and Web-coding aspects, such as defining how
the various tables and fields are related to each other) and even the best interface can be
difficult for the casual Web visitor to use and understand.
Microsoft English Query, which was introduced in Microsoft SQL Server version 6.5,
Enterprise Edition, and has been improved for SQL Server version 7.0, addresses this issue
by providing users with the ability to use natural language when querying existing databasedriven Web sites and applications.
Using English Query to develop an initial natural-language search is easyit is only a small
part of the effort of building your overall applicationand the development process is more
conceptual than traditional programming. In fact, the process can be mastered by
nonprogrammers who have some database background, for example, database
administrators and Web-content developers who work often with databases.
English Query ships with an engine, a component object model (COM) server, that is used at
run time to convert a users English language question to an SQL statement. English Query
also provides sample ASPs that you can embed in a Web site to drive the engine, prompting
a user for questions, executing the engines returned SQL queries, and displaying query
results to the user.
This chapter explains how to author an English Query domain and how to embed the domain
and the English Query engine in your Web site.
From the File menu on the Database tab, click New, and then select Structure loaded
from database.
This initializes the database structure from your SQL Server schema, filling the
Database tab with tables and fields.
This illustration shows the database structure of the sample SQL Server pubs database.
If any tables in the database are missing primary keys, edit them and supply one or more
fields as the primary key. It is not necessary that the underlying database have a primary
key, but all tables must have primary keys identified in the authoring tool for your application
to build.
If tables will be related to each other in queries, then there should be joins indicated between
those tables. The joins usually are retrieved from the foreign keys defined in your database.
However, if the necessary foreign keys are not present (usually they are there to force
referential integrity), then you should add the joins manually inside the authoring tool.
Creating Entities
Now you are ready to start adding semantic entities. This consists of defining the entities in
your database and the tables or fields with which they are associated.
To create an entity
1. On the Semantics tab, right-click Entities, and then click Insert Entity . . . .
2. In the New Entity dialog box, under Semantic Properties, in the Words/phrases
identifying entity box, enter a description of the entity, for example, author or writer. In
the Entity type box, enter a type.
3. Under Database Properties, in the Table or Fields boxes, identify which part of the
database represents the entity.
Major entities usually correspond to entire tables. If it is a major entity, then enter the
fields that should be used to display the entity, such as name or address.
This illustration shows this process for the entity author in the pubs database.
Major entities have two kinds of minor entities associated with them: names and traits.
Names indicate how the entity is identified in questions and statements. By clicking
Autoname for the author entity, you can create an entity that represents the name of the
author entity, associated with the first and last name fields. You can create such name
entities for major entities that are represented by entire tables so the user can identify the
specific entity in question.
By clicking Autotrait, you can create traits for the entity: minor entities that the major entity
has. Click Accept All to create minor entities for all of the semantically meaningful fields, as
well as for trait relationships between the major entity and the newly created minor entities.
In general, you should create entities for all tables in the database. By using Autotrait, you
can create entities for all fields in the database.
Creating Relationships
By establishing traits for the major entities, your model begins with several relationships. At
this point, you can ask questions about things having traits, for example, What authors have
city Seattle?, Show authors and their cities, What book has the title The Busy Executives
Database Guide?, or What publishers have country/region France? But to ask the really
interesting questions, you must create relationships between major entities, for example,
authors write books and publishers publish books.
Phrasing
Next, you can create phrasings for the relationship. Phrasing types include verb phrasings
(authors write books), preposition phrasings (publishers are in cities), adjective phrasings
(books are popular), and subset phrasings (some books are bestsellers). Most trait
phrasings (books have royalties) and name phrasings (author names are the names of
authors) are created by Autotrait and Autoname.
This illustration shows the Verb Phrasing dialog box defined for authors write books.
SQL command using ADO and displaying the result in a table is embedded in the
DoSQLCommand function (available in Samples/Asp/Common.inc).
If the command is an answer, it is displayed directly to the user. The response might also be
a request for clarification. For example, the question might be What are all the compact cars
in Washington? and the clarification question might ask whether Washington is a city or a
state. The DoClarification call (also available in Common.inc) encapsulates the code
necessary to prompt the user to choose from possible values in the UserInputs collection on
the Response object.