Solved

Accessing MS Access database through MS Visual C++ 6

Posted on 2000-03-08
3
263 Views
Last Modified: 2010-04-02
Hi,

I'm writing a program in C++ that will retrieve and store data from a MS Access database. Any pointer on how to set up the connectivity and retrieving and modifying the data ?
0
Comment
Question by:n_chai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
Wyn earned 100 total points
ID: 2599338
You can use ODBC with sqlbase APIs
QUOTED:
class CDemoODBC
{
public:
CDemoODBC();
~CDemoODBC();

bool Init();

bool ConnectToDatabase( const char* dataSourceName, const char* databaseUser, const char* databasePassword );
void DisconnectFromDatabase();

bool Save( const char* paino, const char* halkaisija );

protected:
private:
CDemoODBC( const CDemoODBC& copyFrom );
CDemoODBC& operator=( const CDemoODBC& copyFrom );

void Close();

// data
public:
protected:
private:
HENV envHandle;
HDBC connHandle;
bool connectedODBC;
HSTMT statementHandle;
};



CDemoODBC::CDemoODBC()
: envHandle( SQL_NULL_HENV ),
connHandle( SQL_NULL_HDBC ),
connectedODBC( false ),
statementHandle( SQL_NULL_HSTMT )
{
}

CDemoODBC::~CDemoODBC()
{
DisconnectFromDatabase();

Close();
}

// -------------------------------------

bool CDemoODBC::Init()
{
if ( ::SQLAllocEnv( &envHandle ) == SQL_SUCCESS )
{
if ( ::SQLAllocConnect( envHandle, &connHandle ) == SQL_SUCCESS )
{
::SQLSetConnectOption( connHandle, SQL_LOGIN_TIMEOUT, 5 );

return true;
}
}

return false;
}

void CDemoODBC::Close()
{
if ( connHandle != SQL_NULL_HDBC )
{
::SQLFreeConnect( connHandle );
connHandle = SQL_NULL_HDBC;
}

if ( envHandle != SQL_NULL_HENV )
{
::SQLFreeEnv( envHandle );
envHandle = SQL_NULL_HENV;
}
}

bool CDemoODBC::ConnectToDatabase( const char* dataSourceName, const char* databaseUser, const char* databasePassword )
{
RETCODE rc = ::SQLConnect( connHandle,
(UCHAR*)dataSourceName, SQL_NTS,
(UCHAR*)databaseUser, SQL_NTS,
(UCHAR*)databasePassword, SQL_NTS );

if ( rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO )
{
connectedODBC = true;

if ( ::SQLAllocStmt( connHandle, &statementHandle ) == SQL_SUCCESS )
return true;
}

return false;
}

void CDemoODBC::DisconnectFromDatabase()
{
if ( statementHandle != SQL_NULL_HSTMT )
{
::SQLFreeStmt( statementHandle, SQL_DROP );
statementHandle = SQL_NULL_HSTMT;
}

if ( connectedODBC )
{
::SQLDisconnect( connHandle );
connectedODBC = false;
}
}

bool CDemoODBC::Save( const char* paino, const char* halkaisija )
{
SDWORD painoLen = SQL_NTS;
SDWORD halkaisijaLen = SQL_NTS;

if ( ::SQLPrepare( statementHandle,
(unsigned char*)"INSERT INTO Painojono " 
"(Pvmklo, Paino, Halkaisija) " 
"VALUES " 
"(GetDate(), ?, ?)", SQL_NTS ) == SQL_SUCCESS )
{
if ( ::SQLBindParameter( statementHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_FLOAT, 0, 0, (char*)paino, 0, &painoLen ) == SQL_SUCCESS )
{
if ( ::SQLBindParameter( statementHandle, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_FLOAT, 0, 0, (char*)halkaisija, 0, &halkaisijaLen ) == SQL_SUCCESS )
{
if ( ::SQLExecute( statementHandle ) == SQL_SUCCESS )
{
if ( ::SQLTransact( envHandle, SQL_NULL_HSTMT, SQL_COMMIT ) == SQL_SUCCESS )
return true;
}
}
}
}

return false;
}

 
0
 
LVL 1

Expert Comment

by:carldean
ID: 2599680
If you only ever intend to connect to an Access database (i.e. you don't have any plans to move the data to Oracle or something similar at a later date) then you should use the Microsoft DAO classes.

However, if you are not able to use MFC then you will have to go along the ODBC route and use the ODBC API as described in Wyn's reply.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2600339
yup,carldean is right .
I think you ask here than MFC area thus I suggest this.But I'd suggest you use MFC class for efficency if you can do that.
Regards
W.Yinan
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FMX StringGrid1->Canvas->FillRect Problem 3 219
C++ to C# code conversion issue 4 128
Header of docx file 17 143
How to print into std::string like sprintf using C++11? 12 181
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question