ODBC function calls in Win32


This article was contributed by V.Girish

Environment: Compiled using VC6.0 Sp3 and tested using Win95/98 WinNT4.0 and Win 2000

The following functions demostrate the usage of ODBC through the usage of Win32 API calls


TO FIND THE LIST OF ODBC DRIVERS IN THE MACHINE

void FindDrivers()
{
    CWaitCursor Wait;
    int nCounter = 0;
    WORD wdMaxSize = 255;
    WORD wdOut;
    CString sDriver;
    CString Drivers[256];
    char Buffer[256];
    char *pszBuf = Buffer;

    // Get Installed Drivers List
    if(!SQLGetInstalledDrivers(Buffer, wdMaxSize, &wdOut))
              return;

    do
    {
       sDriver = CString( pszBuf );            // Gets Each Driver's Name
       pszBuf = strchr( pszBuf, '\0' ) + 1;    // Check for NULL
       Drivers[nCounter] = sDriver;            // Fill Driver Storage Array
       nCounter++;
    }
    while( pszBuf[1] != '\0' );

// To prevent NULL to be sent
    nCounter--;

   return;
}

TO FIND DATA SOURCE NAMES (DSN)

void GetDSN()
{
	CWaitCursor Wait;
	SQLHENV henv;
	RETCODE RetCode;
	short len1,len2;
	CString strValue;
	
	unsigned char *ucpDSN;
	ucpDSN=(unsigned char*)malloc(256);
	
	// ucpDescription Contains description of ODBC driver for DSN
	unsigned char *ucpDescription ;
	ucpDescription=(unsigned char*)malloc(256);

	// allocate environment handle
	RetCode=::SQLAllocEnv(&henv);

	// If the allocation is succesfull
	if(RetCode == SQL_SUCCESS || RetCode == SQL_SUCCESS_WITH_INFO)
	{
	    while(RetCode!=SQL_NO_DATA)
	    {
       	       // Fetch available data sources
	       RetCode=::SQLDataSources(henv, SQL_FETCH_NEXT, ucpDSN, 256, 
                                &len1, ucpDescription , 256, &len2);

	      // If data exists
	      if (RetCode!=SQL_NO_DATA)
      	      {
	          strValue.Format("%s-%s",(LPCTSTR)ucpDSN,(LPCTSTR)ucpDescription);

        	  if(!strValue.IsEmpty())
		  {
			// Display the DSN Names
                	 AfxMessageBox(strValue);
		  }

		 // Clear the Buffer
	         memset(ucpDSN,'\0',sizeof(ucpDSN));
      	      }
	   }
	}

	delete ucpDescription;
	delete ucpDSN;

	// free environment handle
	::SQLFreeEnv(henv);
	henv = NULL;
}
Thats all folks. All luck and have a great time. I'll post more functions as soon as i can...

With warm regards,
V.Girish

History

Date Posted: July 22, 2002
Hosted by www.Geocities.ws

Hosted by www.Geocities.ws

1