home *** CD-ROM | disk | FTP | other *** search
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FILE: genchar.sqc
- //
- // Generic Embedded SQL for C application
- //
- // FUNCTIONS:
- //
- // main() - Main program
- // ErrorHandler - Embedded SQL for C error handler
- //
- // COMMENTS:
- //
- // Copyright (C) 1992 - 1994 Microsoft Corporation
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- #include <stddef.h> // standard C run-time header
- #include <stdio.h> // standard C run-time header
- #include "gcutil.h" // utility header
- #include "genchar.h" // specific to this program
-
- // GLOBAL VARIABLES
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: main()
- //
- // Main application
- //
- // PARAMETERS:
- //
- // argc - count of command line args
- // argv - array of command line argument strings
- // envp - array of environment strings
- //
- // RETURNS: 0 if successful, 1 if error
- //
- // COMMENTS:
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- int main (
- int argc,
- char** argv,
- char** envp)
- {
- int nRet; // for return values
-
- EXEC SQL BEGIN DECLARE SECTION;
- // for CONNECT TO
- char szServerDatabase[(SQLID_MAX * 2)+2] = "";
- char szLoginPassword[(SQLID_MAX * 2)+2] = "";
- // for query of pubs..authors
- char szLastName[50] = "";
- char szFirstName[50] = "";
- EXEC SQL END DECLARE SECTION;
-
- // install Embedded SQL for C error handler
- EXEC SQL WHENEVER SQLERROR CALL ErrorHandler();
- // set Embedded SQL for C options
- EXEC SQL SET OPTION LOGINTIME 10;
- EXEC SQL SET OPTION QUERYTIME 100;
-
- // display logo
- printf("Generic Embedded SQL for C application\n");
-
- // get info for CONNECT TO statement
- nRet = GetConnectToInfo(argc, argv,
- szServerDatabase,
- szLoginPassword);
- if (!nRet)
- {
- return (1);
- }
-
- // attempt connection to SQL Server
- EXEC SQL CONNECT TO :szServerDatabase
- USER :szLoginPassword;
- if (SQLCODE == 0)
- {
- printf("Connection to SQL Server established\n");
- }
- else
- {
- // problem connecting to SQL Server
- printf("ERROR: Connection to SQL Server failed\n");
- return (1);
- }
-
- // prompt user for query criteria
- printf("Type an author's last name: ");
- gets(szLastName);
-
- // query the authors table
- EXEC SQL SELECT au_fname INTO :szFirstName
- from authors where au_lname = :szLastName;
- if (SQLCODE == 0)
- {
- printf("That author's first name is: %s", szFirstName);
- }
- else
- {
- if (SQLCODE == 100)
- {
- // no rows match the given criteria
- printf("That author was not found\n");
- }
- else
- {
- // problem executing query
- printf("ERROR: Executing SELECT query\n");
- }
- }
-
- // disconnect from SQL Server
- EXEC SQL DISCONNECT ALL;
-
- return (0);
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: ErrorHandler()
- //
- // Called on Embedded SQL for C error, displays fields from SQLCA
- //
- // PARAMETERS: none
- //
- // RETURNS: none
- //
- // COMMENTS:
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- void ErrorHandler (void)
- {
- // display error information from SQLCA
- printf("Error Handler called:\n");
- printf(" SQL Code = %li\n", SQLCODE);
- printf(" SQL Server Message %li: '%Fs'\n", SQLERRD1, SQLERRMC);
- }
-