CodeGuru
Earthweb Search
Login Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

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

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Data >> Database >> ADO


Using ADO from C++
Rating:

Prasun Paul (view profile)
March 25, 2004

Environment:  Visual C++ 6

Go to page: Prev  1  2  

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

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

(continued)




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

Go to page: Prev  1  2  

Downloads

  • Database.zip - example source code for this article

    Tools:
    Add www.codeguru.com to your favorites
    Add www.codeguru.com to your browser search box
    IE 7 | Firefox 2.0 | Firefox 1.5.x
    Receive news via our XML/RSS feed







  • RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

    (You must be signed in to rank an article. Not a member? Click here to register)

    Latest Comments:
    uuupss - Piotr Wolski (08/16/2009)
    Database::Execute() fails with "SELECT" at second attempt - susiriss (08/31/2007)
    Once #import second time #include - alekper (12/21/2006)
    How to get "Data/Time" field of table? - murick (09/15/2006)
    Is is possible to import data into Access from an external file in C++? - MelStalcup (06/07/2006)

    View All Comments
    Add a Comment:
    Title:
    Comment:
    Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



    (You must be signed in to comment on an article. Not a member? Click here to register)

    internet.commediabistro.comJusttechjobs.comGraphics.com

    Search:

    WebMediaBrands Corporate Info

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs