Setup and Connecting

An ODBC application has two methods of giving an ODBC driver the information the driver needs to connect to the proper server and database. Either the application can connect using an existing ODBC data source containing this information, or it can call either SQLDriverConnect or SQLBrowseConnect, which provides the information in the connection string parameter.

Setting up a Data Source

ODBC data sources contain information that tells a driver how to connect to a database. ODBC data sources can be created by using the ODBC Administrator application in Control Panel or by an application calling the ODBC SQLConfigDataSource function.

Data source definitions are stored in C:\Windows\System\Odbc.ini for the Microsoft Windows® version 3.x and Windows for Workgroups version 3.x operating systems.

Win32 data sources fall into one of two categories (for details, see Microsoft Knowledge Base article Q136481):

Information about the drivers installed on a client is stored in C:\Windows\System\Odbcinst.ini in Windows 3.x or Windows for Workgroups 3.x and in HKEY_LOCAL_MACHINE\Software\ODBC\Odbcinst.ini in Windows NTand Windows 95.

Each driver needs to store driver-specific information in its data sources. When a user adds a data source using ODBC Administrator, the driver displays a dialog box, where the user specifies data source information. When a data source is defined with SQLConfigDataSource, the function accepts an attribute string parameter that can contain driver-specific keywords. All of the SQLConfigDataSource driver-specific keywords for the SQL Server ODBC driver have counterparts in the dialog box that displays when using ODBC Administrator.

Here's an example SQLConfigDataSource call that sets up a SQL Server data source referencing a server using DHCP on TCP/IP:

RETCODE   retcode;
UCHAR   *szDriver = "SQL Server";
UCHAR   *szAttributes =
   "DSN=my65dsn\0DESCRIPTION=SQLConfigDSN Sample\0"
   "SERVER=my65server\0ADDRESS=HRServer\0NETWORK=dbmssocn\0"
   "DATABASE=pubs\0";
retcode = SQLConfigDataSource(NULL,
        ODBC_ADD_DSN,
        szDriver,
        szAttributes);

Driver-specific SQLConfigDataSource Keywords

The following sections describe the driver-specific keywords supported by the Microsoft SQL Server ODBC driver.

SERVER, NETWORK, and ADDRESS

The SERVER, NETWORK, and ADDRESS parameters associate a data source with a specific instance of SQL Server on the network. These parameters are directly related to the advanced entries created with the SQL Server Client Configuration Utility:

If ADDRESS is present, it is always used as the network address for the connection. If ADDRESS is not present, then SERVER is used as the network address for the connection.

Here's an example entry to make a named pipes connection to a server:

SERVER=xyz,NETWORK=dbnmpntw,ADDRESS=HRServer

The following entry evaluates to the same network address:

SERVER=HRServer,NETWORK=dbnmpntw

Here's an example entry to make a sockets connection to the same computer:

SERVER=tcpxyz,NETWORK=dbmssocn,ADDRESS=123.123.123.123,1433

There are two special cases to consider:

The SERVER, NETWORK, and ADDRESS parameters specified on SQL Server ODBC driver data sources operate the same way as the Server, DLL, and Connection String parameters specified for advanced entries made with the SQL Server Client Configuration Utility. For more information about the advanced-entry parameters, see the Microsoft SQL Server Administrator's Companion. The same parameters can be specified in the data source creation dialog box displayed in ODBC Administrator.

The relationship between the parameters is illustrated in the following table.

SQLConfigDataSource ODBC Administrator SQL Client Configuration Utility
SERVER Server Server
NETWORK Network Library DLL
ADDRESS Network Address Connection String

If a data source is defined with the SERVER, NETWORK, and ADDRESS parameters, a SQL Server advanced connection entry is made in the registry, and can be viewed using the SQL Client Configuration Utility.

DATABASE

This parameter specifies the default database for the ODBC data source.

LANGUAGE

This parameter specifies the default national language to use.

OEMTOANSI

This parameter specifies whether to convert extended characters to OEM values.

SQL Server is usually run with one of three code pages:

The 437 and 850 code pages are sometimes collectively referred to as the OEM code pages.

All three code pages define 256 different values to use in representing characters. The values from 0 to128 represent the same characters in all three code pages. The values from 129 to 255, which are known as the extended characters, represent different characters in all three code pages.

Because ODBC applications are Windows applications, they generally use ANSI code page 1252. If they are communicating with a SQL Server also running ANSI code page 1252, there is no need for character-set conversion. If they connect to a server running a 437 or 850 code page however, the driver must be informed that it should convert extended characters from their 1252 values to 437 or 850 values before sending them to the server. In this case, the data source should have OEMTOANSI=YES. For a more in-depth discussion of SQL Server code pages, see Microsoft Knowledge Base article Q153449.

TRANSLATIONDLL

This parameter specifies the name of the ODBC translation DLL to use with the data source.

TRANSLATIONNAME

This parameter specifies the name of the translator to use with the data source.

TRANSLATIONOPTION

This parameter specifies whether translation should be done on the data going to SQL Server. YES specifies translation; NO specifies no translation. For more information about ODBC translation, see the ODBC 2.0 Programmer's Reference.

USEPROCFORPREPARE

This parameter specifies whether the driver generates stored procedures to support the ODBC SQLPrepare function. For more information, see "SQLExecDirect vs. SQLPrepare/SQLExecute."

The following driver-specific SQLConfigDataSource keywords are new in SQL Server 6.5 SP2.

QuotedID

This parameter specifies whether the driver should issue a SET QUOTED IDENTIFIERS ON option when connecting to a SQL Server version 6.0 or later database. YES specifies QUOTED_IDENTIFIERS is ON; NO specifies the option is OFF. For more information, see  "SET Options Used by the Driver."

AnsiNPW

This parameter specifies whether the driver should SET ON the ANSI_NULLS, ANSI_PADDING, and ANSI_WARNINGS options when connecting to a SQL Server version 6.5 or later database. YES specifies the options are ON; NO specifies they are OFF. For more information, see "SET Options Used by the Driver."

The following driver-specific SQLConfigDataSource keywords are new in SQL Server 6.5.

QueryLogFile

This parameter specifies the file name the driver should use to log long-running queries. Include the full path name for the file. For more information, see "ODBC Driver Profiling Features."

QueryLog_ON

This parameter specifies whether the data source should do query profiling. 1 specifies profiling is done; omitting the parameter specifies no profiling. For more information, see "ODBC Driver Profiling Features."

QueryLogTime

This parameter specifies the interval for long-running queries. The interval is specified in milliseconds. If a query is outstanding for a period exceeding the QueryLogTime, it is written to the QueryLogFile. For more information, see "ODBC Driver Profiling Features."

StatsLogFile

This parameter specifies the file name the driver should use to log long performance statistics. Include the full path name for the file. For more information, see "ODBC Driver Profiling Features."

StatsLog_On

This parameter specifies whether the data source should log performance statistics. 1 specifies profiling is done; omitting the parameter specifies no profiling. For more information, see "ODBC Driver Profiling Features."

Trusted_Connection

This parameter specifies whether the data source should use trusted connections when connecting to SQL Server. 1 specifies trusted connections; omitting the parameter specifies no trusted connections. For more information, see "Integrated and Standard Security."

Creating Data Sources in ODBC Administrator

When you add, modify, or double-click a SQL Server data source in ODBC Administrator, the SQL Server ODBC driver displays the ODBC SQL Server Setup dialog box. The parameters in this dialog box control the same features that are controlled by the SQLConfigDataSource keywords earlier in this paper, although they have slightly different names. Many of the options are in the dialog box that displays when you click Options. To specify the query and performance profiling options, click Options, and then click Profiling.

Driver-specific SQLDriverConnect Keywords

An ODBC application can connect to a SQL Server without referencing a data source:

RETCODE   retcode;
UCHAR   szDSN[MAXBUFLEN+1] =
   "DRIVER={SQL Server};SERVER=MyServer;"
   "UID=sa;PWD=astring;APP=Generic32;DATABASE=pubs";
UCHAR   szUID[MAXUID+1] = "sa",
      szAuthStr[MAXAUTHSTR+1] = "password",
      szConnStrOut[MAXBUFLEN+1];
SWORD   swStrLen;
retcode = SQLDriverConnect(hdbc1,
         NULL,
         szDSN,
         (SWORD)strlen(szDSN),
         szConnStrOut,
         MAXBUFLEN,
         &swStrLen,
         SQL_DRIVER_NOPROMPT);

The SQL Server ODBC driver supports three classes of keywords on SQLDriverConnect:

APP

This keyword specifies the application name to be recorded in the program_name column in master.dbo.sysprocesses. APP is equivalent to a DB-Library application calling the DBSETLAPP function in C or the SQLSetLApp function in the Visual Basic® programming system.

WSID

This keyword specifies the workstation name to be recorded in the hostname column in master.dbo.sysprocesses. WSID is equivalent to a DB-Library application calling the DBSETLHOST function in C or the SQLSetLHost function in Visual Basic.

Connection Messages

The SQL Server ODBC driver returns SQL_SUCCESS_WITH_INFO on a successful SQLConnect, SQLDriverConnect, or SQLBrowseConnect. When an ODBC application calls SQLError after getting SQL_SUCCESS_WITH_INFO, it can receive the following messages:

The following example shows these messages being returned on a successful connect by the System Administrator (SA) login. The SA login has its default database at the server defined as the master database, the server is running US English, and the connect used an ODBC data source that specified pubs as the default database.

Full Connect:
   szSqlState = "01000", *pfNativeError = 5701,
   szErrorMsg="[Microsoft][ODBC SQL Server Driver][SQL Server]
          Changed database context to 'master'."
   szSqlState = "01000", *pfNativeError = 5703,
   szErrorMsg="[Microsoft][ODBC SQL Server Driver][SQL Server]
          Changed language setting to 'us_english'."
   szSqlState = "01000", *pfNativeError = 5701,
   szErrorMsg="[Microsoft][ODBC SQL Server Driver][SQL Server]
          Changed database context to 'pubs'."
Successfully connected to DSN 'my60server'.

Applications can ignore these 5701 and 5703 messages; they are informational only. Applications cannot, however, ignore a return of SQL_SUCCESS_WITH_INFO return code on the SQLConnect, SQLDriverConnect, or SQLBrowseConnect. This is because messages other than 5701 and 5703 that do require action may be returned. For example, if a driver connects to a SQL Server with outdated system stored procedures, one of the messages returned through SQLError is:

SqlState:   01000
pfNative:   0
szErrorMsg: "[Microsoft][ODBC SQL Server Driver]The ODBC
            catalog stored procedures installed on server
            my421server are version 02.00.4127; version 06.00.0115
            or later is required to ensure proper operation.
            Please contact your system administrator."

An application's error handling routines for SQL Server connections should call SQLError until it returns SQL_NO_DATA_FOUND and act on any messages other than the ones that return a pfNative code of 5701 or 5703.

Integrated and Standard Security

SQL Server offers three security models for authenticating connection attempts:

The SQL Server ODBC driver always uses a trusted connection when connecting to a server running integrated security. The driver can also be instructed to open trusted connections when connecting to a server that is running with standard or mixed security. Only the named pipes or multiprotocol Net-Libraries support integrated security and trusted connections.

There are two ways to tell the driver to use trusted connections:

Integrated security offers several benefits:

Protocol Considerations

Integrated security is only available when using either the named pipes or multiprotocol Net-Libraries. When using the multiprotocol Net-Library, the SA can also configure the server to encrypt packets sent across the network, so that even users of network sniffers cannot see the data. The named pipes and multiprotocol Net-Libraries can also work with either a TCP/IP, SPX/IPX, or NetBEUI protocol stack. This means a client running only a TCP/IP protocol stack can use either the Windows sockets, named pipes, or multiprotocol Net-Libraries. The Windows sockets (TCP/IP), SPX/IPX, Appletalk, DECNet, and Banyan Vines Net-Libraries only work with their single, associated, protocol stack.

Due to their added functionality, such as the encryption feature, the multiprotocol Net-Libraries are somewhat slower than the others. Testing at Microsoft has found that the TCP/IP Net-Libraries are somewhat faster than the other Net-Libraries. Other considerations, however, such as database design, indexing, and the design of queries and applications, usually have a greater impact on performance than the choice of a Net-Library.

Applications running against SQL Server 6.0 or 6.5 can sometimes improve their performance by resetting the TDS network packet size. The default packet size is set at the server, and is 4K. 4K generally gives the best performance. Applications can set the packet size themselves if testing shows that they perform better with a different packet size. ODBC applications can do this by calling SQLSetConnectionOption with the SQL_PACKET_SIZE option before connecting. Some applications may perform better with a larger packet size, but performance improvements are generally minimal for packet sizes larger than 8K.

Verifying and Testing Data Sources

The Odbcping.exe utility can be used to check whether an ODBC connection can be made between a client and a SQL Server.  The command syntax to use the utility is:

odbcping {/Sservername | /Ddatasource} /Ulogin_id /Ppassword

where

servername
   
Is the network name of the server running SQL Server.

datasource
   
Is the name of an ODBC data source.

login_id
   
Is the SQL Server login ID.

password
   
Is the login password.

You must specify either /S or /D, but not both. (The version of odbcping that ships with SQL Server 6.0 will not accept the /D parameter, only /S, /U, and /P.)

When odbcping makes a successful connection, it displays a message indicating the connection was successful and the versions of the driver and server. For example:

CONNECTED TO SQL SERVER
ODBC SQL Server Driver Version: 02.65.0201
SQL Server Version: SQL Server for Windows NT6.50 - 6.50.201 (Intel X86)
   Apr 3 1996 02:55:53
   Copyright (c) 1988-1997 Microsoft Corporation

If the connect attempt is not successful, odbcping displays the errors it receives.  (The 6.0 version of odbcping does not display the Native Error code.) For example:

COULD NOT CONNECT TO SQL SERVER
SQLState: 01000  Native Error: 2
Error Message: [Microsoft][ODBC SQL Server Driver][dbnmpntw]
                       ConnectionOpen (CreateFile()).
SQLState: 08001  Native Error: 6
Error Message: [Microsoft][ODBC SQL Server Driver][dbnmpntw]
                      Specified SQL Server not found.

The pfNative (or Native Error) code is important in diagnosing connection problems. For more information, see "pfNative Error Codes."