User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

    Using ADO from C++



    Now, let's implement the member functions of Database class one by one.

    Database::Database()
    {
       m_Cnn=NULL;
    }
    

    Here, I set m_Cnn to NULL because at this point there is no established connection.

    bool Database::Open(char* UserName, char* Pwd, char* CnnStr)
    {
    
       HRESULT hr;
       try
       {
          if(m_Cnn==NULL)
             return 0;
          hr    = m_Cnn.CreateInstance(
             __uuidof( ADODB::Connection ) );
          m_Cnn->Open(CnnStr, UserName, Pwd, NULL);
       }
       catch(_com_error &e)
       {
          //   Handle errors
          return 0;
       }
       return 1;
    }
    

    Here, __uuidof( ADODB::Connection ) returns the interface id that is used to create an instance of a Connection object. If a connection object is created successfully, the Open() method tries to establish a physical connection to the data source. Open() takes three parameters: connection string, user name, and password. If a physical connection is established, this function returns 1. I used a catch handler to catch the exception thrown by the methods of Connection object.

    RecPtr Database::Execute(char* CmdStr)
    {
       try
       {
          if(m_Cnn==NULL)
             return NULL;
          return m_Cnn->Execute(CmdStr,NULL,1);
       }
       catch(_com_error &e)
       {
          //   Handle errors
          return NULL;
       }
    }
    

    The Execute() method takes a valid SQL query and returns a smart pointer to the Recordset object.

    bool Database::Close()
    {
       //is there any established connection?
       //if no, return 0
       if (m_Con==NULL)
          return 0;
       //if any, close that connection
       try
       {
          m_Con->Close();
          m_Con=NULL;
       }
       catch(_com_error &e)
       {
          //   Handle errors
          return 0;
       }
       return hr==S_OK;
    }
    
    
    Database::~Database()
    {
       try
       {
       //close connection, if not yet closed
          if (m_Con)
          {
             m_Con->Close();
             m_Con=NULL;
          }
       }
       catch(_com_error &e)
       {
          //   Handle errors
       }
    }
    

    Notice that you must initialize COM before using this class. You can do this by adding the following code before using this class.

       if(FAILED(::CoInitialize(NULL)))
          return;
    

    It is good programming practice if you uninitialize COM. You can do this by using ::CoUninitialize() at the end of program.

    In this article, I have tried to demonstrate a C++ class by which a C++ programmer can enjoy a VB-like ADO programming model. Download and then review the source code attached with this article; you will see how this technique simplyfies working with ADO. For further information, you can mail me. I will try my best to help you.

    About the Author

    computer science & engineering, CUET

    Downloads

  • Database.zip

  • IT Offers


    Top Authors