Topic : ADO Programming
Author : Jason Beighel
Page : << Previous 2  
Go to page :


be used to get the actual field data.  In Value only the appropriate value will be filled in, for example if the database field type is an integer the Value member won't give the value as a string, only as an integer.  There may be a way to perform that conversion but I'm not familiar enough with the _variant_t variable type to do that.  Integer values can be accessed straing out of the Value member, strings through need to be converted into the standard NULL terminated array.  To do that you can use the function WideCharToMultiByte(), this function takes 8 parameters.  Only the third, fifth, and sixth paramters are really needed, the rest can always be the same.  The third parameter is the string to be converted, the fifth parameter is a pointer to a buffer to store the converted string, and the sixth is the size of the buffer.  Here are a few if statements that can be used to handle the data from the field variable:

if ((Field->Type ==  202)||(Field->Type == 203))
{/*Handling a string database field type*/
    char String[100];
    WideCharToMultiByte(CP_ACP, 0, Field.bstrVal, -1, String, 100, NULL, NULL);
    printf("Data: %s\n", String);
}


if ((Field->Type == 7)||(Field->Type == 11))
{/*Handling a boolean database field type*/
    printf("Data: %d\n", Field->Value.boolVal);
}


if (Field->Type == 2)
{/*Handling a integer database field type*/
    printf("Data: %d\n", Field->Value.intVal);
}



Once you have done all the work you needed with the recordset and the database you have to do a little cleanup.  The recordsets need to be closed as well as teh database connection, both have convenient member functions called Close().  Here is how they look:

RecSet->Close();
Con->Close();


This by no means covers all of the aspects of ADO, for that matter it doesn't even cover all of the aspects of the functions I mentioned.  There are a lot of options you can set in the connection string when connecting to a database, and dozens of additional database field types that will need to be dealt with.  The idea of this article was only to present the background and basics for working with ADO database connections.  Hopefully that much at least was covered.





DBtest.cpp
#include<stdio.h>
#include"C:\Program Files\Microsoft Visual Studio\VC98\mfc\SRC\stdafx.h"

#import "c:\program files\common files\system\ado\msado15.dll" rename("EOF", "EOFile")

struct StartOLEProcess
{
   StartOLEProcess()
   {
      ::CoInitialize(NULL);
   }
   ~StartOLEProcess()
   {
      ::CoUninitialize();
   }
} _start_StartOLEProcess;

void main(void)
{
   ADODB::_ConnectionPtr con = NULL;
   ADODB::_RecordsetPtr rec = NULL;
   ADODB::FieldPtr pAuthor;
   _variant_t vAuthor;
   char sAuthor[40];
   HRESULT hr = S_OK;
   char File[255], ConStr[500];
   VARIANT *vRecordsAffected = NULL;
   int ctr;

   printf("\nEnter Database Path and File name: ");
   fgets(File, 250, stdin);
   for (ctr = 0; (unsigned int)ctr < strlen(File); ctr++)
   {
      if (File[ctr] == '\n')
      {
         File[ctr] = '\0';
         break;
      }
   }
   ConStr[0] = '\0';
   strcat(ConStr, "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=");
   strcat(ConStr, File);

   hr = con.CreateInstance(__uuidof(ADODB::Connection));
   printf("\nCreateInstance result= %d uuidof= %d\n", hr, __uuidof(ADODB::Connection));
   printf("\nConnection object created.");

   con->Open(ConStr, "", "", 0);

   printf("\nConnection opened.");

   printf("\nEnter Database table name: ");
   fgets(File, 250, stdin);
   for (ctr = 0; (unsigned int)ctr < strlen(File); ctr++)
   {
      if (File[ctr] == '\n')
      {
         File[ctr] = '\0';
         break;
      }
   }
   ConStr[0] = '\0';
   strcat(ConStr, "SELECT * FROM ");
   strcat(ConStr, File);
   rec = con->Execute(ConStr, vRecordsAffected, 1);

   printf("\nSQL statement processed");

   printf("\nEnter Database field name: ");
   fgets(File, 250, stdin);
   for (ctr = 0; (unsigned int)ctr < strlen(File); ctr++)
   {
      if (File[ctr] == '\n')
      {
         File[ctr] = '\0';
         break;
      }
   }
   pAuthor = rec->Fields->GetItem(File);

   if ((pAuthor->Type == 202)||(pAuthor->Type == 203))
   {
      printf("\nGetting data now...\n");

      while (!rec->EOFile)
      {
         vAuthor.Clear();
         vAuthor = pAuthor->Value;
         WideCharToMultiByte(CP_ACP, 0, vAuthor.bstrVal, -1, sAuthor, sizeof(sAuthor), NULL, NULL);

         printf("\n%s", sAuthor);
         rec->MoveNext();
      }
      printf("\n\nEnd of data.");
   }
   else
   {
      if ((pAuthor->Type == 11)||(pAuthor->Type == 7))
      {
         printf("\nGetting data now...\n");

         while (!rec->EOFile)
         {
            vAuthor.Clear();
            vAuthor = pAuthor->Value;

            printf("\n%d", vAuthor.boolVal);
            rec->MoveNext();
         }
         printf("\n\nEnd of data.");
      }
      else
      {
         if (pAuthor->Type == 2)
         {
            printf("\nGetting data now...\n");

            while (!rec->EOFile)
            {
               vAuthor.Clear();
               vAuthor = pAuthor->Value;

               printf("\n%d", vAuthor.intVal);
               rec->MoveNext();
            }
            printf("\n\nEnd of data.");
         }
         else
         {
            printf("\nUnable to handle that data type, %d", pAuthor->Type);
         }
      }
   }

   rec->Close();
   rec = NULL;

   printf("\nClosed and removed recordset object.");

   con->Close();
   con = NULL;

   printf("\nClosed and removed connection object.");

   return;
}


Page : << Previous 2