C + + package to access the database tired

Category: C/C++ -> C++ Author: shuai826051563 Date: 2011-01-27 19:37:07
 
shuai826051563
2011-01-27 19:37:07
Members heroes, who can provide a C + + class to access the database package template, thank you!
zll0401
2011-01-27 19:51:26
ADO class package should include the following functions and data structures:

1) establish a database connection function OpenDataBase ()

ADO connect to the database first use to the Connection object, which is a physical connection to the database, manages application and database communication. For most database systems, each physical connection takes a lot of system memory, system resources are valuable, not abuse. Taking into account the efficiency of the application, each with a database client only physical connection, and the application should be released after the end.

2) close the database connection function CloseDataBase ()

3) SQL query function Select ()

set of records creation and query The results obtained have to get that affect the efficiency of the application was the key issue. Since every query results may be different, so each query should establish a record set object.

4) execute SQL statements that return results without function Excute ()

5) array of query results pData

convenient for data type conversion, We will record set data types are converted to CString type, stored in the array of query results pData, because the C + + CString is an efficient and powerful class, we can easily read the record set query results.

6) result array size nResultRow and nResultCol

2.3 ADO wrapper class header file

wrapper class header file ADODB.H defined as follows :

class CADODB: public CObject
{
public:
CADODB (); / / constructor
~ CADODB (); / / destructor
public:
int nResultRow; / / query result rows
int nResultCol; / / number of columns in query results
CStringArray pData; / / store query results array size nRow * nCol
CString errormessage; / / ADO error message
bool OpenDataBase (CString DsnName , CString sUserID, CString sPassword);
/ / Open Database Connectivity enter the DSN name, user ID and password
int Select (CString Sql); / / execute SQL query The result is placed in the array pData
int Excute (CString Sql); / / execute SQL statements deserve no return
bool CloseDataBase (); / / close the database connection
protected:
_ConnectionPtr m_pConnection; / / define the connection pointer
};

2.4 wrapper class implementation file

Package class implementation file ADODB.CPP as follows:

# include "stdafx.h"
# include "ADODB.h"

CADODB :: CADODB () / / constructor to initialize
{
nResultRow = 0; / /
nResultCol = 0; / / record set the number of rows and columns
m_pConnection = NULL;
}

CADODB :: ~ CADODB () / / close the database physical connection
{
if (m_pConnection) m_pConnection-> Close ();
m_pConnection = NULL;
}

bool CADODB :: OpenDataBase (CString DsnName, CString sUserID, CString ; sPassword)
{/ / Open Database Connectivity
...
HRESULT hr = m_pConnection.CreateInstance ("ADODB.Connection") ;/ / create a connection object
hr = m_pConnection-> Open ((_bstr_t) DsnName, (_bstr_t) sUserID, (_bstr_t) sPassword, adOpenUnspecified); / / connect to database
...
}

int ; CADODB :: Select (CString SqlStr) / / SQL query function
{...
try
{
hr = m_pRecordset.CreateInstance (" ; ADODB.Recordset ") ;/ / create a record set object instance
hr = m_pRecordset-> putref_ActiveConnection (m_pConnection) ;/ / set the connection object
hr = m_pRecordset-> Open (vSqlString , vNull, adOpenDynamic, adLockOptimistic, adCmdText); / / Open the record set
nResultCol = m_pRecordset-> Fields-> GetCount () ;/ / get the total number of columns
record set while (! m_pRecordset-> adoEOF)
{
for (j = 0; j <nResultCol; j + +) / / fetch a data
{
vValue = m_pRecordset-> Fields-> Item [(long) j] -> Value ;/ / get the current record field data
if (vValue.vt ! = 1) / / Data not empty
{
int type = vValue.vt;
if (VariantChangeType (& vValue1, & vValue , 0, VT_BSTR) == S_OK) / / convert the data type (string)
str = vValue1.bstrVal; / / save the result value to a temporary variable str in
}
if (pData.GetSize () <i * nResultCol + j +1) / / dynamically set the size of the array
m_pResult.SetSize (i * nResultCol + j + 1);
pData [i * nResultCol + j] = str; / / save the results into an array
m_pRecordset-> MoveNext (); / / move the cursor to the next row record set
i + +;
}
m_pRecordset-> Close ();
m_pRecordset = NULL;
nResultRow = i;
}
catch ( _com_error e) / / / catch exception
{
...
}
pData-> FreeExtra () ;/ / release extra memory space
return ; nResultRow;
}
...

3

wrapper class using ADO ADO wrapper classes established after the operation is very simple to use database the. The need to use a database query CPP file contains ADO wrapper class header file "ADODB.H", and then define a CADODB class pointer; Then there is the open database connection. Using encapsulation class member function OpenDataBase (...), enter the DSN to connect to the database name, user name and password, then establish a connection with the database. Note that if you simultaneously connect to multiple databases must declare multiple CADODB object pointer, ensure that the database connection object is global and unique. The next step is to call a member function Select database query results achieved, after the implementation of query results on CADODB character array pointer pData member, the array size can be from member variables nResultRow and nResultCol derived. Basic steps are as follows:

in "stdafx.h" added the ADO library import statements

# import "c: \ program files \ common files \ system \ ado \ msado15.dll "no_namespace rename (" EOF "," adoEOF ")

in the application class initialize the COM object in InitInstance () to add the following statement

AfxOleInit ();

added wrapper class declaration file

# include "ADODB.H"
...

defined wrapper class object

CADODB ADO1; / / connect to database 1
CADODB ADO2; / / connect to database 2
...
ADO1.OpenDataBase ("Data Source Name", "User ID", "password") / / establish a connection, the first connection call
ADO1.Select ("select * from table name"); / / execute SQL query
int nRow = ADO1.GetResultRow (); / / Get number of rows
int nCol = ADO1.GetResultCol (); / / get record number of columns
for (i = 0; i <nRow; i + +) / / get query results
for (j = 0; j <nCol; j + +)
printf ("line% d of% d column value is% s \ n", i, j, ADO1.pData [i * nCol + j];
...
ADO1.CloseDataBase (); / / close the database connection after use

4 Conclusions and recommendations

using encapsulation ADO class, operate the database in the application process becomes simple for general database applications, but also in a large amount of data is not the case, the wrapper classes have been able to cope, in use efficiency is also quite satisfactory, but the package did not consider data access optimization, database uses dynamic connection, there is no record set paging processing, ADO cursor type buffer size and the default settings are used, the actual use of these may reduce the performance of the enclosing class, readers can have a variety of applications to be modified for

Source:
binggunhaobing
2011-01-27 19:54:51
this is also a more detailed

dangler
2011-01-27 20:06:46


Upstairs, the top one.
one of my very simple:

/*
* :CData
* :
* :
*BLOG:
*/
#pragma once
// DLL
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","rsEOF")
class CData
{
public:
CData(void);
//
bool GetRs(CString strSql,_RecordsetPtr& rs);
// SQL
bool ExecuteSql(CString strSql);
public:
~CData(void);
private:
_ConnectionPtr m_conn;
};


#include "Data.h"

CData::CData(void)
{
// COM
::CoInitialize(NULL);
try
{
m_conn.CreateInstance("ADODB.Connection");
CString conn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=AngleFinance.mdb";
//SQL SERVER
//CString conn="Driver={SQL Server};server=(local);uid=sa;database=pubs";//local 127.0.0.1
if(FAILED(m_conn->Open((_bstr_t)conn,"","",-1)))
{
::MessageBox(NULL," "," ",MB_ICONEXCLAMATION);
return;
}
}
catch(...)
{
::MessageBox(NULL," "," ",MB_ICONEXCLAMATION);
}

}

CData::~CData(void)
{
m_conn->Close();
m_conn.Release();

::CoUninitialize();
}
bool CData::GetRs(CString strSql,_RecordsetPtr& rs)
{

try
{
rs=m_conn->Execute((_bstr_t)strSql,NULL,adCmdText);
return true;
}
catch(...)
{
return false;
}
}
bool CData::ExecuteSql(CString strSql)
{
try
{
    m_conn->Execute((_bstr_t)strSql,NULL,adCmdText);
return true;
}
catch(...)
{
return false;
}
}
Z287627698
2011-01-27 20:21:24
with ADO connect MS SQL, google the bar, the landlord.
smallflate
2011-01-27 20:29:20
xx basically some of the online operation of the database management systems have The next analysis package
just wants to follow suit next
bhjk7962
2011-01-27 20:34:10
best to buy a book to look
book_linstime
2011-01-27 20:43:07
MFC in ODBC, ADO and other ways to access the database classes are packaged

MySql, sqlite and other databases provide access to the DLL and other documents, has provided an interface
hbbs100
2011-01-27 20:46:49
MFC wrapper classes have odbc , very good use