Call-level Method

Embedded SQL for C and SQL Server

Embedded SQL for C and SQL Server

Call-level Method

Microsoft® SQL Server™ 2000 programs, written using the DB-Library or ODBC API methods, communicate directly with SQL Server through C function calls. DB-Library or ODBC functions pass SQL statements to SQL Server and return the results of queries. The call-level method of programming requires no precompiler; however, you cannot include Transact-SQL statements in your C programs, as you can with Embedded SQL for C (ESQL/C).

For example, to use DB-Library to connect to SQL Server and execute a simple query against the pubs sample database requires source code similar to the following:

#define DBNTWIN32
#include <sqlfront.h>
#include <sqldb.h>

main()
{
   DBPROCESS *dbproc;
   LOGINREC *login;
   RETCODE r;

   dbinit();
   login = dblogin();
   if (login == NULL)
      return (1);
   DBSETLUSER(login, "my_login");
   DBSETLPWD(login, "my_password");
   dbproc = dbopen(login, "my_server");
   dbfreelogin(login);
   if (dbproc == NULL)
      return (1);
   dbuse(dbproc, "pubs");
   dbcmd(dbproc,
      "select au_fname from authors where au_lname = 'White'");
   r = dbsqlexec(dbproc);
   if (r == FAIL)
      return (1);
   while (1)
   {
      r = dbresults(dbproc);
      if (r == SUCCEED)
      {
         /* Process the rows with dbnextrow() */
      }
      if ((r == FAIL) || (r == NO_MORE_RESULTS))
         break;
   }
   return (0);
}

The DB-Library approach, using C function calls, is more verbose and more flexible than the ESQL/C approach, and it can be loosely coupled to any database structure. Because a great deal of the program's behavior can be changed dynamically, DB-Library programs are often general-purpose applications. DB-Library is well-suited for environments where the database structure is not known in advance.