Home How To Online Documentation Support Download Order
 

 

 


SACommand
SAConnection
SAException
SAField
SAParam
SAString
SADateTime
SANumeric

Enums and Typedefs


Server Specific Guide

SQL Server
(DB-Library)

SQLAPI++ allows to work with a number of SQL database servers. It provides common mechanisms to access database, and as a general rule they work for any database server. But each server has some specific features which a developer has to know in order to leverage server's unique features and avoid potential errors.

This page collects all specific information that concerns working with SQL Server (DB-Library API) using SQLAPI++ Library. Full information about using SQLAPI++ see in How To and Online Documentation .

Available topics:

Connecting to a database

Transaction isolation levels

Working with Long or Lob(CLob, BLob) data

Returning output parameters

Cancelling queries

Connection, command, parameter and field options

Getting native SQL Server (DB-Library) API

Getting native SQL Server (DB-Library) connection related handles

Getting native SQL Server (DB-Library) command related handles

Error handling

Special header files - Compile time

 

Connecting to a database

To connect to a database you should create a connection object and then connect it. A connection object is represented by SAConnection class:

SAConnection con;

Starting from version 4.0.3 SQLAPI++ Library uses ODBC as default API when working with SQL Server. If you want to use DB-Library, you should set "UseAPI" connection option before specifying SQL Server client or connecting to database:

con.setOption( "UseAPI" ) = "DB-Library";

If the Library cannot find DB-Library interface it throws an exception.

After the connection is created you need to connect it to SQL Server using SAConnection::Connect method

void Connect( const SAString &sDBString, const SAString &sUserID, const SAString &sPassword, SAClient_t eSAClient = SA_Client_NotSpecified );

with the following parameters:

sDBString.      One of the following formats:

  • "" or "@" - empty string or '@' character, connects to a default database on a local server.
  • <database name> or @ <database name> - connects to a database with the specified name on your local server.
  • <server name>@ - connects to a default database on the specified server.
  • <server name>@<database name> - connects to a database with the specified name on the specified server.

To connect to a named instance of SQL Server 2000 use <server name\instance name> instead of <server name> (use double back slash at C++ constants, for example "MyServer\\SQLEXPRESS@pubs").

sUserID.    A string containing a user name to use when establishing the connection. If sUserID parameter is empty, SQLAPI++ Library requests a secure, or trusted, connection to SQL Server.

sPassword.    A string containing a password to use when establishing the connection.

eSAClient.    Optional. One of the following values from SAClient_t enum:

  • SA_SQLServer_Client    SQLServer client.
  • SA_Client_NotSpecified     Used by default if eSAClientparameter is omitted.  You can use this default value only if you have call SAConnection::setClient method with SA_SQLServer_Client constant before.

The SQLAPI++ Library requires DB-Library version 6.x or higher.

For more details see How To - Connecting to databases, SAConnection object, SAConnection::Connect.

 

Transaction isolation levels

SQL-92 defines four isolation levels, all of which are supported by SQLAPI++:

  • Read uncommitted (the lowest level where transactions are isolated just enough to ensure that physically corrupt data is not read)
  • Read committed
  • Repeatable read
  • Serializable (the highest level, where transactions are completely isolated from one another)

SQLAPI++ maps different isolation levels on SQL Server (DB-Library) in the following way:

      SA_ReadUncommitted = 'READ UNCOMMITTED'
      SA_ReadCommitted = 'READ COMMITTED'
      SA_RepeatableRead = 'REPEATABLE READ'
      SA_Serializable = 'SERIALIZABLE'

For more details see SAConnection::setIsolationLevel.

 

Working with Long or Lob(CLob, BLob) data

SQLAPI++ supports four types for working with Lob(CLob, BLob) data:

Name C enum constant
LongBinary SA_dtLongBinary
LongChar SA_dtLongChar
BLob (Binary Large object) SA_dtBLob
CLob (Character Large object) SA_dtCLob

The table below shows how SQLAPI++ data types correspond with SQL Server's (DB-Library) original data types:

SA_dtLongBinary <= > IMAGE
SA_dtLongChar <= > TEXT
SA_dtBLob = > IMAGE
SA_dtCLob => TEXT

For more details see How To - Working with Long or Lob(CLob, BLob) data , How To - Binding input parameters.

 

Returning output parameters

SQL Server stored procedures can have integer return codes and output parameters. The return codes and output parameters are sent in the last packet from the server and are therefore not available to the application until all result sets from stored procedure (if any) are completely processed using SACommand::FetchNext method.

SQLAPI++ Library automatically creates SAParam object to represent procedure status return code or function return value. You can refer to this SAParam object using SQLAPI++ predefined name "RETURN_VALUE".

For more details see SACommand::Execute, SACommand::FetchNext, SAParam object, How To - Returning Output Parameters.

 

Cancelling queries

Using SACommand::Cancel method you can cancel the following types of processing on a statement:

  • A function running asynchronously on the statement.
  • A function running on the statement on another thread.

For SQL Server (DB-Library) SQLAPI++ calls dbcancel function to cancel a query. To get more details see dbcancel function description in SQL Server DB-Library documentation.
To enable cancellation you have to set command option "UseDataReadyLoopOnExecute" to "on".

For more details see SACommand::Cancel, SQL Server (DB-Library) connection and command options.

 

Connection, command, parameter and field options

A server specific option can relate to a connection, command,  parameter or field. We recommend you specify each option in an appropriate object, although it is possible to specify them in the parental object as well. In that case the option affects all the child objects.

A connection related option must be specified in a  SAConnection object.

A command related option may be specified in either SAConnection object or SACommand object. If it is specified in SAConnection object it affects all the commands on that connection.

A parameter related option may be specified in SAConnection object, SACommand object or SAParam object. If it is specified in SAConnection object it affects all the commands and therefore all the parameters on that connection. If it is specified in SACommand object it affects all the parameters on that command.

A field related option may be specified in SAConnection object, SACommand object or SAField object. If it is specified in SAConnection object it affects all the commands and therefore all the fields on that connection. If it is specified in SACommand object it affects all the fields on that command.

Specific options for SQL Server (DB-Library):

Option name / Scope 

Description

UseAPI

Connection related. Should be specified before connection is made.

Forces SQLAPI++ Library to use OLE DB, ODBC or DB-Library API.
 
Valid values : "OLEDB", "ODBC" and "DB-Lib".
Default value: "ODBC". SQLAPI++ uses ODBC as the default API.

OpenCursor

Command related.

Forces SQLAPI++ Library to use DB-Library cursor functions to process the command execution result set. It allows to open several result sets simultaneously in one transaction. For more information see SQLServer (DB-Library) documentation.

Valid values : "number of rows[, scroll option]". String containing number of rows in the fetch buffer and (optional) scroll option. Valid values for scroll option are: CUR_DYNAMIC, CUR_FORWARD, CUR_KEYSET, CUR_INSENSITIVE. If scroll option parameter is omitted then CUR_FORWARD value is used. For more information see DB-Library documentation.
Default value : "0". "0" means that the command doesn't use a cursor to process its statement.

UseDataReadyLoopOnExecute

Command related 

Forces SQLAPI++ Library to use DB-Library dbsqlsend() + dbdataready() loop + dbsqlok() instead of dbsqlexec() when executing a query. Works slower but allows query cancellation from another thread.

Valid values : "off" and "on".
Default value: "off", SQLAPI++ uses dbsqlexec().

UseDynamicCursor
or
Scrollable

Command related.
Forces SQLAPI++ to use scrollable dynamic server side cursor.

Valid values: "True", "1". Default value: "false".

APPNAME

Connection related. Should be specified before connection is made.

Specifies the client application name.

Valid values: client application name string.
Default value: none.

WSID

Connection related.Should be specified before connection is made.

Specifies the client workstation name.

Valid values: client workstation name string.
Default value: none.

For more details see SAConnection::setOption, SACommand::setOption, SAField::setOption, SAParam::setOption.

 

Getting native SQL Server (DB-Library) API

You can call client specific API functions which are not directly supported by SQLAPI++ Library. SAConnection::NativeAPI method returns a pointer to the set of native API functions available for SQL Server (DB-Library). To use the database API directly you have to downcast this saAPI pointer to the appropriate type and use its implementation-specific members. The following table shows what type cast you have to make and what additional header file you have to include to work with SQL Server with DB-Library API. Note that using appropriate type casting depends on an API (that generally mean that you have to explicitly check client version before casting, see SAConnection::ClientVersion method).

Type casting

Additional
include file

Cast the result to class ssAPI:

saAPI *pResult = con.NativeAPI();
ssAPI *p_ssAPI = (ssAPI *)pResult;

#include <ss6API.h>

To get more information about DBMS API functions see this DBMS specific documentation. 

For more details see SAConnection::NativeAPI.

 

Getting native SQL Server (DB-Library) connection related handles

You have to use native API handles when you want to call specific DB-Library API functions which are not directly supported by the Library. API functions usually need to receive one or more active handles as a parameter(s). SAConnection::NativeHandles method returns a pointer to the set of native API connection related handles. To use API handles directly you have to downcast saConnectionHandles pointer to the appropriate type and use its implementation-specific members. The following table shows what type cast you have to make and what additional header file you have to include to work with SQL Server with DB-Library API. Note that using appropriate type casting depends on an API (that generally mean that you have to explicitly check client version before casting, see SAConnection::ClientVersion method).

Type casting

Cast the result to class ssConnectionHandles:

#include <ss6API.h>

saConnectionHandles *pResult = con.NativeHandles();
ssConnectionHandles *p_ssCH =
                                (ssConnectionHandles *)pResult;

Available handles:

  • PDBPROCESS m_dbproc;

To get more information about DBMS API functions and handles see this DBMS specific documentation. 

For more details see SAConnection::NativeHandles.

 

Getting native SQL Server (DB-Library) command related handles

You have to use native API handles when you want to call specific DB-Library API functions which are not directly supported by the Library. API functions usually need to receive one or more active handles as a parameter(s). SACommand::NativeHandles method returns a pointer to the set of native API command related handles. To use API handles directly you have to downcast saCommandHandles pointer to the appropriate type and use its implementation-specific members. The following table shows what type cast you have to make and what additional header file you have to include to work with SQL Server with DB-Library API. Note that using appropriate type casting depends on an API (that generally mean that you have to explicitly check client version before casting, see SAConnection::ClientVersion method).

Type casting

Cast the result to class ssCommandHandles:

#include <ss6API.h>

saCommandHandles *pResult = cmd.NativeHandles ();
ssCommandHandles *p_ssCH =
                                (ssCommandHandles *)pResult;

Available handles:

  • no handles available

To get more information about DBMS API functions and handles see this DBMS specific documentation. 

For more details see SACommand::NativeHandles.

 

Error handling

When an error occurs inside SQLAPI++ Library it throws an exception of type SAException. SAException::ErrPos method gets an error position in SQL statement. In SQL Server (DB-Library) SAException::ErrPos method returns the number of line within SQL statement where error occurred.

For more details see How To - Error handling, SAException object.

 

Special header files - Compile time

The header file in the include subdirectory of SQLAPI++ distributions:
#include <SQLAPI.h> - main header, should be used whenever SQLAPI++ is used.
#include <ss6API.h> - SQLServer API, should be included if direct DB-Libary calls are required.

For more details see Online Documentation - Instructions for Compiling and Linking Applications with SQLAPI++ 

 

Problems and Questions

If you haven't found the answer to you questions or have some problems on using the Library, please, send e-mail to howto@sqlapi.com.