Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Call-level Method

SQL Server 2000

  New Information - SQL Server 2000 SP3.

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);
   DBSETLSECURE(login);
   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);
}

Security Note  The preceding example uses the DBSETLSECURE function for Windows Authentication. When possible, use this authentication method. If Windows Authentication is not available, prompt users to enter their credentials at run time. Avoid storing credentials in a file. If you must persist credentials, you should encrypt them with the Win32 cryptoAPI.

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.