CdbDBEngine::RegisterDatabase Method

Enters connection information for an ODBC data source in the Windows registry. The ODBC driver needs connection information when the ODBC data source is opened during a session.

Syntax

VOIDRegisterDatabase(LPCTSTR pstrDatabase,

LPCTSTR pstrDriver,

BOOL bSilent,

LPCTSTR pstrAttributes);

Parameters

Type Example Description
LPCTSTR pstrDatabase A pointer to a string that is the name used in the OpenDatabase method. It refers to a block of descriptive information about the data source. For example, if the data source is an ODBC remote database, it could be the name of the server.
LPCTSTR pstrDriver A pointer to a string that is the name of the ODBC driver. This isn't the name of the ODBC driver DLL file. For example, SQL Server is a driver name, but SQLSRVR.DLL is the name of a DLL file. You must have ODBC and the appropriate driver already installed.
BOOL bSilent A Boolean.

TRUEDoesn't display the ODBC driver dialog boxes that prompt for driver-specific information. The next parameter, Attributes, must contain all the necessary driver-specific information or the dialog boxes are displayed anyway.

FALSEDisplays the ODBC driver dialog boxes.

LPCTSTR pstrAttributes A pointer to a string that is a list of keywords to be added to the Windows registry. The keywords are in a carriage-return-delimited string.

Usage

/* 
   Register a Microsoft SQL Server data source named 'Publishers'
   in the Windows registry. (The Windows ODBC Control Panel icon 
   is the preferred way to create, modify, or delete data source    names.)
   Notes:
   1) Specify IniPath, DefaultUser, DefaultPassword, etc. as 
      desired in place of the ellipses.
*/
#include <afxole.h>
#include <dbdao.h>

CdbDBEngine      dben(FALSE,TRUE,...,...,...,dbUseODBC); // note 1
CdbDatabase       dbsRegister;
LPCTSTR         
   lpctstrAttribute = 
      _T("Database=pubs\rDescription=Publishers\r
      OemToAnsi=No\rServer=Server1"),
   lpctstrNotice =
      _T("Use regedit.exe to view changes:
      HKEY_CURRENT_USER\Software\ODBC\ODBC.INI");

dbsRegister = dben.OpenDatabase(_T("Publishers"));
try 
{
dben.RegisterDatabase(_T("Publishers"),
                      _T("SQL Server"),
                      TRUE, lpctstrAttribute );
printf(lpctstrNotice);
}
catch (CdbException)
{
for (long ct = 0; ct < dben.Errors.GetCount(); ct++)
   {
   printf(_T("Error #%ld: #%ld -- %s\n"),
      ct,
      dben.Errors[ct].GetNumber(),
      dben.Errors[ct].GetDescription());
   }
}