1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
<HTML>
<HEAD>
<TITLE>prguide.htm</TITLE>
<LINK REL="ToC" HREF="httoc.htm">
<LINK REL="Index" HREF="htindex.htm">
<LINK REL="Next" HREF="prguid11.htm">
<LINK REL="Previous" HREF="prguide9.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF">
<P ALIGN=CENTER>
<A HREF="prguide9.htm" TARGET="_self"><IMG SRC="graprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="httoc.htm" TARGET="_self"><IMG SRC="gratoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="htindex.htm" TARGET="_self"><IMG SRC="graindex.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Index"></A>
<A HREF="prguid11.htm" TARGET="_self"><IMG SRC="granext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<HR ALIGN=CENTER>
<P>
<UL>
<LI>
<A HREF="#E9E11" >Constructing an Application</A>
<UL>
<LI>
<A HREF="#E10E40" >Sample Application Code</A>
<UL>
<LI>
<A HREF="#E11E38" >Static SQL Example</A>
<LI>
<A HREF="#E11E39" >Interactive Ad Hoc Query Example</A></UL>
<LI>
<A HREF="#E10E41" >Testing and Debugging an Application</A>
<LI>
<A HREF="#E10E42" >Installing and Configuring ODBC Software</A></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E9E11"></A>
<H1>
<FONT FACE="Arial"><B>CONSTRUCTING AN APPLICATION</B><A NAME="I2"></A></FONT></H1>
<BR>
<BLOCKQUOTE>
<P>This chapter provides two examples of C-language source code for applications. For developers writing ODBC-enables applications a summary of development, debugging, installation, and administration tools provided by the ODBC SDK 2.0 is included.
</BLOCKQUOTE>
<A NAME="E10E40"></A>
<H2>
<FONT FACE="Arial"><B>Sample Application Code</B><A NAME="I3"></A></FONT></H2>
<BLOCKQUOTE>
<P>The following sections contain two examples that are written in the C programming language:<A NAME="I4"></A>
</BLOCKQUOTE>
<UL>
<BLOCKQUOTE>
<LI>An example that uses static SQL functions to create a table, add data to it, and select the inserted data.<A NAME="I5"></A>
</BLOCKQUOTE>
<BLOCKQUOTE>
<LI>An example of interactive, ad-hoc query processing.
</BLOCKQUOTE></UL>
<BLOCKQUOTE>
<P>These examples can use either ODBC header files or SOLID <I>SQL API</I> header files.
</BLOCKQUOTE>
<A NAME="E11E38"></A>
<H3>
<FONT FACE="Arial">Static SQL Example<A NAME="I6"></A><A NAME="I7"></A><A NAME="I8"></A><A NAME="I9"></A><A NAME="I10"></A><A NAME="I11"></A></FONT></H3>
<BLOCKQUOTE>
<P>The following example constructs SQL statements within the application. The example comments include equivalent embedded SQL calls for illustrative purposes.
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>#ifdef SOLIDSQLAPI
<BR>#include "CLI0DEFS.H"
<BR>#include "CLI0CORE.H"
<BR>#include "CLI0EXT1.H"
<BR>#else
<BR>#include "SQL.H"
<BR>#include "SQLEXT.H"
<BR>#endif
#include <string.h>
#ifndef NULL
<BR>#define NULL 0
<BR>#endif
#define MAX_NAME_LEN 50
<BR>#define MAX_STMT_LEN 100
int print_err(HDBC hdbc, HSTMT hstmt);
int example1(server, uid, pwd)
<BR>UCHAR * server;
<BR>UCHAR * uid;
<BR>UCHAR * pwd;
<BR>{
<BR>HENV henv;
<BR>HDBC hdbc;
<BR>HSTMT hstmt;
SDWORD id;
<BR>UCHAR name[MAX_NAME_LEN + 1];
<BR>UCHAR create[MAX_STMT_LEN]
<BR>UCHAR insert[MAX_STMT_LEN]
<BR>UCHAR select[MAX_STMT_LEN]
<BR>SDWORD namelen;
<BR>RETCODE rc;
/* EXEC SQL CONNECT TO :server USER :uid USING :pwd; */
<BR>/* Allocate an environment handle. */
<BR>/* Allocate a connection handle. */
<BR>/* Connect to a data source. */
<BR>/* Allocate a statement handle. */
SQLAllocEnv(&henv);
<BR>SQLAllocConnect(henv, &hdbc);
<BR>rc = SQLConnect(hdbc, server, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
<BR>if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
<BR> return(print_err(hdbc, SQL_NULL_HSTMT));
<BR>SQLAllocStmt(hdbc, &hstmt);
/* EXEC SQL CREATE TABLE NAMEID */
<BR>/* (ID integer, NAME varchar(50)); */
<BR>/* Execute the SQL statement. */
lstrcpy(create, "CREATE TABLE NAMEID (ID INTEGER, NAME
<BR> VARCHAR(50))");
<BR>rc = SQLExecDirect(hstmt, create, SQL_NTS);
<BR>if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
<BR> return(print_err(hdbc, hstmt));
/* EXEC SQL COMMIT WORK; */
<BR>/* Commit the table creation. */
/* Note that the default transaction mode for drivers */
<BR>/* that support SQLSetConnectOption is auto-commit */
<BR>/* and SQLTransact has no effect. */
SQLTransact(hdbc, SQL_COMMIT);
/* EXEC SQL INSERT INTO NAMEID VALUES ( :id, :name ); */
<BR>/* Show the use of the SQLPrepare/SQLExecute method: */
<BR>/* Prepare the insertion and bind parameters. */
<BR>/* Assign parameter values. */
<BR>/* Execute the insertion. */
lstrcpy(insert, "INSERT INTO NAMEID VALUES (?, ?)");
<BR>if (SQLPrepare(hstmt, insert, SQL_NTS) != SQL_SUCCESS)
<BR> return(print_err(hdbc, hstmt));
<BR>SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG,
<BR> SQL_INTEGER, 0, 0, &id, 0, NULL);
<BR>SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
<BR> SQL_VARCHAR, MAX_NAME_LEN, 0, name, 0, NULL);
<BR>id=500;
<BR>lstrcpy(name, "Babbage");
<BR>if (SQLExecute(hstmt) != SQL_SUCCESS)
<BR> return(print_err(hdbc, hstmt));
/* EXEC SQL COMMIT WORK; */
<BR>/* Commit the insertion. */
SQLTransact(hdbc, SQL_COMMIT);
/* EXEC SQL DECLARE c1 CURSOR FOR */
<BR>/* SELECT ID, NAME FROM NAMEID; */
<BR>/* EXEC SQL OPEN c1; */
<BR>/* Show the use of the SQLExecDirect method. */
<BR>/* Execute the selection. */
<BR>/* Note that the application does not declare a cursor. */
lstrcpy(select, "SELECT ID, NAME FROM NAMEID");
<BR>if (SQLExecDirect(hstmt, select, SQL_NTS) !=
<BR> SQL_SUCCESS)
<BR> return(print_err(hdbc, hstmt));
/* EXEC SQL FETCH c1 INTO :id, :name; */
<BR>/* Bind the columns of the result set */
<BR>/* with SQLBindCol. */
<BR>/* Fetch the first row. */
SQLBindCol(hstmt, 1, SQL_C_SLONG, &id, 0, NULL);
<BR>SQLBindCol(hstmt, 2, SQL_C_CHAR, name,
<BR> (SDWORD)sizeof(name), &namelen);
<BR>SQLFetch(hstmt);
/* EXEC SQL COMMIT WORK; */
<BR>/* Commit the transaction. */
SQLTransact(hdbc, SQL_COMMIT);
/* EXEC SQL CLOSE c1; */
<BR>/* Free the statement handle. */
SQLFreeStmt(hstmt, SQL_DROP);
/* EXEC SQL DISCONNECT; */
<BR>/* Disconnect from the data source. */
<BR>/* Free the connection handle. */
<BR>/* Free the environment handle. */
SQLDisconnect(hdbc);
<BR>SQLFreeConnect(hdbc);
<BR>SQLFreeEnv(henv);
return(0);
<BR>}</PRE></BLOCKQUOTE>
<A NAME="E11E39"></A>
<H3>
<FONT FACE="Arial">Interactive Ad Hoc Query Example<A NAME="I12"></A><A NAME="I13"></A><A NAME="I14"></A><A NAME="I15"></A></FONT></H3>
<BLOCKQUOTE>
<P>The following example illustrates how an application can determine the nature of the result set prior to retrieving results.
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>#ifdef SOLIDSQLAPI
<BR>#include "CLI0DEFS.H"
<BR>#include "CLI0CORE.H"
<BR>#include "CLI0EXT1.H"
<BR>#else
<BR>#include "SQL.H"
<BR>#include "SQLEXT.H"
<BR>#endif
<BR>#include <string.h>
<BR>#include <stdlib.h>
#define MAXCOLS 100
<BR>#define max(a,b) (a>b?a:b)
int print_err(HDBC hdbc, HSTMT hstmt);
<BR>UDWORD display_size(SWORD coltype, UDWORD collen, UCHAR *colname);
example2(server, uid, pwd, sqlstr)
<BR>UCHAR * server;
<BR>UCHAR * uid;
<BR>UCHAR * pwd;
<BR>UCHAR * sqlstr;
<BR>{
<BR>int i;
<BR>HENV henv;
<BR>HDBC hdbc;
<BR>HSTMT hstmt;
<BR>UCHAR errmsg[256];
<BR>UCHAR colname[32];
<BR>SWORD coltype;
<BR>SWORD colnamelen;
<BR>SWORD nullable;
<BR>UDWORD collen[MAXCOLS];
<BR>SWORD scale;
<BR>SDWORD outlen[MAXCOLS];
<BR>UCHAR * data[MAXCOLS];
<BR>SWORD nresultcols;
<BR>SDWORD rowcount;
<BR>RETCODE rc;
/* Allocate environment and connection handles. */
<BR>/* Connect to the data source. */
<BR>/* Allocate a statement handle. */
<BR>SQLAllocEnv(&henv);
<BR>SQLAllocConnect(henv, &hdbc);
<BR>rc = SQLConnect(hdbc, server, SQL_NTS, uid, SQL_NTS,
<BR> pwd, SQL_NTS);
<BR>if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
<BR> return(print_err(hdbc, SQL_NULL_HSTMT));
<BR>SQLAllocStmt(hdbc, &hstmt);
/* Execute the SQL statement. */
<BR>if (SQLExecDirect(hstmt, sqlstr, SQL_NTS) !=
<BR> SQL_SUCCESS)
<BR> return(print_err(hdbc, hstmt));
/* See what kind of statement it was. If there are */
<BR>/* no result columns, the statement is not a SELECT */
<BR>/* statement. If the number of affected rows is */
<BR>/* greater than 0, the statement was probably an */
<BR>/* UPDATE, INSERT, or DELETE statement, so print */
<BR>/* the number of affected rows. If the number of */
<BR>/* affected rows is 0, the statement is probably a */
<BR>/* DDL statement, so print that the operation was */
<BR>/* successful and commit it. */
SQLNumResultCols(hstmt, &nresultcols);
<BR>if (nresultcols == 0) {
<BR> SQLRowCount(hstmt, &rowcount);
<BR> if (rowcount > 0 ) {
<BR> printf("%ld rows affected.\n", rowcount);
<BR> } else {
<BR> printf("Operation successful.\n");
<BR> }
<BR> SQLTransact(hdbc, SQL_COMMIT);
/* Otherwise, display the column names of the result */
<BR>/* set and use the display_size() function to */
<BR>/* compute the length needed by each data type. */
<BR>/* Next, bind the columns and specify all data will */
<BR>/* be converted to char. Finally, fetch and print */
<BR>/* each row, printing truncation messages as */
<BR>/* necessary. */
} else {
<BR> for (i = 0; i < nresultcols; i++) {
<BR> SQLDescribeCol(hstmt, i + 1, colname,
<BR> (SWORD)sizeof(colname), &colnamelen,
<BR> &coltype, &collen[i], &scale,
<BR> &nullable);
<BR> collen[i] = display_size(coltype, collen[i],
<BR> colname);
<BR> printf("%*.*s", collen[i], collen[i],
<BR> colname);
<BR> data[i] = (UCHAR *) malloc(collen[i] + 1);
<BR> SQLBindCol(hstmt, i + 1, SQL_C_CHAR,
<BR> data[i], collen[i], &outlen[i]);
<BR> }
<BR> while (TRUE) {
<BR> rc = SQLFetch(hstmt);
<BR> if (rc == SQL_SUCCESS || rc ==
<BR> SQL_SUCCESS_WITH_INFO) {
<BR> errmsg[0] = '\0';
<BR> for (i = 0; i < nresultcols; i++)
<BR> if (outlen[i] == SQL_NULL_DATA)
<BR> {
<BR> lstrcpy(data[i], "NULL");
<BR> }
<BR> else if (outlen[i] >= collen[i])
<BR> {
<BR> sprintf(&errmsg[strlen(errmsg)],
<BR> "%d chars truncated, col %d\n",
<BR> outlen[i] - collen[i] + 1,
<BR> colnum);
<BR> }
<BR> printf("%*.*s ", collen[i], collen[i],
<BR> data[i]);
<BR> }
<BR> printf("\n%s", errmsg);
<BR> } else {
<BR> break;
<BR> }
<BR> }
<BR>}
/* Free the data buffers. */
<BR>for (i = 0; i < nresultcols; i++) {
<BR> free(data[i]);
<BR>}
/* Free the statement handle. */
<BR>SQLFreeStmt(hstmt, SQL_DROP );
<BR>/* Disconnect from the data source. */
<BR>SQLDisconnect(hdbc);
<BR>/* Free the connection handle. */
<BR>SQLFreeConnect(hdbc);
<BR>/* Free the environment handle. */
<BR>SQLFreeEnv(henv);
return(0);
<BR>}
/******************************************************//* The following function is included for */
<BR>/* completeness,but is not relevant for understanding */
<BR>/* the function of ODBC. */
<BR>/******************************************************/
#define MAX_NUM_PRECISION 15
<BR>/* Define max length of char string representation of */
<BR>/* number as: = max(precision) + leading sign + E + */
<BR>/* exp sign + max exp length */
<BR>/* = 15 + 1 + 1 + 1 + 2 */
<BR>/* = 15 + 5 */
#define MAX_NUM_STRING_SIZE (MAX_NUM_PRECISION + 5)
UDWORD display_size(coltype, collen, colname)
<BR>SWORD coltype;
<BR>UDWORD collen;
<BR>UCHAR * colname;
<BR>{
<BR>switch (coltype) {
<BR>
<BR> case SQL_CHAR:
<BR> case SQL_VARCHAR:
<BR> return(max(collen, strlen(colname)));
case SQL_SMALLINT:
<BR> return(max(6, strlen(colname)));
case SQL_INTEGER:
<BR> return(max(11, strlen(colname)));
case SQL_DECIMAL:
<BR> case SQL_NUMERIC:
<BR> case SQL_REAL:
<BR> case SQL_FLOAT:
<BR> case SQL_DOUBLE:
<BR> return(max(MAX_NUM_STRING_SIZE, strlen(colname)));
/* Note that this function only supports the */ /* core data types. */
<BR> default:
<BR> printf("Unknown datatype, %d\n", coltype);
<BR> return(0);
<BR> }
<BR>}</PRE></BLOCKQUOTE>
<A NAME="E10E41"></A>
<H2>
<FONT FACE="Arial"><B>Testing and Debugging an Application</B><A NAME="I16"></A><A NAME="I17"></A><A NAME="I18"></A></FONT></H2>
<BLOCKQUOTE>
<P>The ODBC SDK provides the following tools for application development:<A NAME="I19"></A>
</BLOCKQUOTE>
<UL>
<BLOCKQUOTE>
<LI>ODBC Test, an interactive utility that enables you to perform ad hoc and automated testing on drivers. A sample test DLL (the Quick Test) is included which covers basic areas of ODBC driver conformance.<A NAME="I20"></A>
</BLOCKQUOTE>
<BLOCKQUOTE>
<LI>ODBC Spy, a debugging tool with which you can capture data source information, emulate drivers, and emulate applications.<A NAME="I21"></A>
</BLOCKQUOTE>
<BLOCKQUOTE>
<LI>Sample applications, including source code and makefiles.<A NAME="I22"></A>
</BLOCKQUOTE>
<BLOCKQUOTE>
<LI>A <B>#define</B>, ODBCVER, to specify which version of ODBC you want to compile your application with. By default, the SQL.H and SQLEXT.H files include all ODBC 2.0 constants and prototypes. To use only the ODBC 1.0 constants and prototypes, add the following line to your application code before including SQL.H and SQLEXT.H:
</BLOCKQUOTE></UL>
<BLOCKQUOTE>
<PRE>#define ODBCVER 0x0100<A NAME="I23"></A></PRE></BLOCKQUOTE>
<BLOCKQUOTE>
<P>For additional infomation about the ODBC SDK tools, see the <I>Microsoft</I> <I>ODBC SDK Guide</I>.
</BLOCKQUOTE>
<A NAME="E10E42"></A>
<H2>
<FONT FACE="Arial"><B>Installing and Configuring ODBC Software</B><A NAME="I24"></A></FONT></H2>
<BLOCKQUOTE>
<P>Users install ODBC software with a driver-specific setup program (built with the Driver Setup Toolkit that is shipped with the ODBC SDK) or an application-specific setup program. They configure the ODBC environment with the ODBC Administrator (also shipped with the ODBC SDK) or an application-specific administration program. Application developers must decide whether to redistribute these programs or write their own setup and administration programs. For more information about the Driver Setup Toolkit and the ODBC Administrator, see the <I>Microsoft ODBC SDK Guide</I>.<A NAME="I25"></A>
</BLOCKQUOTE>
<BLOCKQUOTE>
<P>A setup program written by an application developer uses the installer DLL to retrieve information from the ODBC.INF file, which is created by a driver developer and describes the disks on which the ODBC software is shipped. The setup program also uses the installer DLL to retrieve the target directories for the Driver Manager and the drivers, record information about the installed drivers, and install ODBC software.<A NAME="I26"></A>
</BLOCKQUOTE>
<BLOCKQUOTE>
<P>Administration programs written by application developers use the installer DLL to retrieve information about the available drivers, to specify default drivers, and to configure data sources.<A NAME="I27"></A>
</BLOCKQUOTE>
<BLOCKQUOTE>
<P>Application developers who write their own setup and administration programs must ship the installer DLL and the ODBC.INF file.
</BLOCKQUOTE><P ALIGN=CENTER>
<A HREF="prguide9.htm" TARGET="_self"><IMG SRC="graprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="httoc.htm" TARGET="_self"><IMG SRC="gratoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="htindex.htm" TARGET="_self"><IMG SRC="graindex.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Index"></A>
<A HREF="prguid11.htm" TARGET="_self"><IMG SRC="granext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<center><p><font SIZE=-2>Copyright © 1992-1997 Solid Information Technology Ltd All rights reserved.</font></p></center>
</BODY></HTML>
|