Custom Database Class: class database

Source: Internet
Author: User
Class Database
{
PRIVATE:
_ Connectionptr m_pconnection;
_ Commandptr m_pcommand;
_ Recordsetptr m_res;
_ Variant_t rec_affected;
Char m_cconstr [16];
Char m_cusername [32];
Char m_cpassword [32];
Bool m_bopentype;
Public:
Database ();
~ Database ();
Void reopen ();
Bool isopen ();
Bool opendb (char * pcontstr, char * psbname, char * psbpw );
Bool opendb (char * pcontstr );
Bool getdata (char * psqlstr, _ recordsetptr & res );
Bool execute (char * psqlstr );
Bool noneretopen (char * psqlstr );
Bool commandpro (char * pproname );
_ Connectionptr & getconnection ();
Void closedb ();
};

<CPP>
# Import "C:/program files/common files/system/ADO/msado15.dll" no_namespace Rename ("EOF", "endoffile") Rename ("Bof", "firstoffile ")
# Include <objbase. h>
# Include "cdatabase. H"

Database: Database ()
{
Coinitialize (null );
M_pconnection = NULL;
M_pcommand = NULL;
M_pconnection.createinstance (_ uuidof (connection ));
M_res.createinstance (_ uuidof (recordset ));
}

Database ::~ Database ()
{
Closedb ();
If (m_res-> getstate () = adstateopen)
M_res-> close ();
M_res.release ();
M_res = NULL;
M_pconnection.release ();
M_pconnection = NULL;
Couninitialize ();
}

_ Connectionptr & Database: getconnection ()
{
Return m_pconnection;
}

Bool Database: opendb (char * pcontstr, char * psbname, char * psbpw)
{
Try
{
If (m_pconnection-> state)
Return true;
// Open the local access library text. MDB
M_pconnection-> open (_ bstr_t) pcontstr, psbname, psbpw, admodeunknown );
If (m_pconnection-> state! = Adstateopen)
Return false;
M_pconnection-> put_connectiontimeout (long (10); // set timeout
M_pconnection-> put_commandtimeout (long (25 ));

M_pcommand.createinstance (_ uuidof (command ));
M_pcommand-> activeconnection = m_pconnection;
Strcpy (m_cconstr, pcontstr );
Strcpy (m_cusername, psbname );
Strcpy (m_cpassword, psbpw );

M_bopentype = true;
}
Catch (_ com_error E)
{
Return false;
}
Return true;
}

Bool Database: opendb (char * pcontstr)
{
Try
{
If (m_pconnection-> state)
Return true;

M_pconnection-> connectionstring = "file name =. // dbconn. udl ";
M_pconnection-> putconnectiontimeout (10 );
M_pconnection-> putcommandtimeout (25 );

M_pconnection-> open ("", null );

If (m_pconnection-> state! = Adstateopen)
Return false;

M_bopentype = false;
M_pcommand.createinstance (_ uuidof (command ));
M_pcommand-> activeconnection = m_pconnection;
Strcpy (m_cconstr, pcontstr );
}
Catch (_ com_error E)
{
Return false;
}
Return true;
}

Void Database: closedb ()
{
If (m_pconnection = NULL) return;
If (isopen ())
M_pconnection-> close ();
}

Bool Database: Execute (char * psqlstr)
{
If (! Isopen () return false;
Try
{
M_pconnection-> execute (psqlstr, & rec_affected, 0 );
}
Catch (_ com_error E)
{
Return false;
}
Return true;
}

Bool Database: noneretopen (char * psqlstr)
{
If (! Isopen () return false;

If (m_res-> getstate () = adstateopen)
M_res-> close ();
Try
{
M_res-> open (psqlstr, m_pconnection.getinterfaceptr (), adopenforwardonly, adlockreadonly, adcmdtext );
}
Catch (_ com_error E)
{
Return false;
}
Return true;
}

Bool Database: commandpro (char * pproname)
{
If (! Isopen () return false;
Try
{
M_pcommand-> commandtext = pproname;
M_pcommand-> execute (null, null, ad1_text );
}
Catch (_ com_error * E)
{
MessageBox (null, e-> errormessage (), "", mb_ OK );
Return false;
}
Return true;
}
/*
Bool Database: commandpro (char * pproname)
{
If (! Isopen () return false;
Try
{
M_pcommand-> commandtext = pproname;
M_pcommand-> execute (null, null, ad1_text );
}
Catch (_ com_error * E)
{
MessageBox (null, e-> errormessage (), "", mb_ OK );
Return false;
}
Return true;
}
*/

Bool Database: isopen ()
{
Try
{
Return (m_pconnection! = NULL & (m_pconnection-> State & adstateopen ));
}
Catch (_ com_error E)
{
Return false;
}
}

Void Database: reopen ()
{
Try
{
If (m_pconnection-> state! = Adstateopen)
M_pconnection-> open (m_cconstr, m_cusername, m_cpassword, admodeunknown );
}
Catch (_ com_error E)
{
}
}

Bool Database: getdata (char * psqlstr, _ recordsetptr & res)
{
If (! Isopen () return false;

If (res-> getstate () = adstateopen)
Res-> close ();
Try
{
Res-> open (psqlstr, m_pconnection.getinterfaceptr (), adopenforwardonly, adlockreadonly, adcmdtext );
}
Catch (_ com_error E)
{
Return false;
}
Return true;
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.