Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Databases in Visual C++

Status
Not open for further replies.

darioars

Technical User
Oct 30, 2002
21
0
0
DE
Hi guys!

Does any of you any hint about how to implement databases within Visual C++ applications? Or just how to implement a database that can be accessed from a Visual C++ programme?

Any help would be great! Thanks in advance.

Darío.
 
This is a broad question. There are many ways of connecting to a database in visual c++: DAO, ADO, ODBC

Which one you use depends on your requirements and the type of database you are using. Here are some good tutorials using MFC with databases:

 
Code:
//using DAO
#import "G:\Program Files\Common Files\Microsoft Shared\DAO\dao360.dll"
#include "Debug\dao360.tlh"
#include<atlbase.h>
#include<iostream>

using namespace std;

int main()
{
    CoInitialize(0);
    {
        HRESULT hr = -1;
        CComPtr<IUnknown> app;
        DAO::_DBEngine* eng;
        DAO::Workspace* wr;
        DAO::Database* db;
        DAO::Recordset* rs;
        VARIANT_BOOL eof;
        VARIANT_BOOL bof;
        DAO::Fields* fs;
        DAO::_Field* f1;
        DAO::_Field* f2;
        hr = app.CoCreateInstance(L&quot;DAO.DBEngine.36&quot;);
        hr = app->QueryInterface(&eng);
        hr = eng->raw_CreateWorkspace(L&quot;TestWorkSpace1&quot;, L&quot;Admin&quot;, L&quot;&quot;, _variant_t(DAO::dbUseJet), &wr);
        //open dbname, ifexclusive, ifreadonly, connect, retdb
        hr = wr->raw_OpenDatabase(_bstr_t(L&quot;c:\\db1.mdb&quot;), _variant_t(0), _variant_t(0), _variant_t(L&quot;&quot;), &db);
        hr = db->raw_OpenRecordset(_bstr_t(L&quot;select * from Table1&quot;), 
            _variant_t(DAO::dbOpenDynaset), _variant_t(0), _variant_t(2), &rs);
        hr = rs->get_Fields(&fs);
        hr = fs->get_Item(_variant_t(0), &f1);
        hr = fs->get_Item(_variant_t(1), &f2);
        wcout<< L&quot;<&quot;<< (BSTR)f1->Name<< L&quot;><&quot; << f2->Name<< L&quot;>&quot;<< endl;
        VARIANT valx, valy;
        while(true)
        {
            hr = rs->get_EOF(&eof);
            hr = rs->get_BOF(&bof);
            if(eof)break;
            if(bof)break;
            f1->get_Value(&valx);
            f2->get_Value(&valy);
            wcout<< L&quot;{&quot;<< (wchar_t*)_bstr_t(_variant_t(valx))<< L&quot;}{&quot;;
            wcout<< (wchar_t*)_bstr_t(_variant_t(valy))<< L&quot;}&quot;<< endl;
            hr = rs->MoveNext();
        }

        hr = hr;
       
    }
    CoUninitialize();
	
	return 0;
}

// using DAO
#import &quot;G:\Program Files\Common Files\System\ADO\msado15.dll&quot;
#include &quot;Debug\msado15.tlh&quot;
#include<atlbase.h>
#include<iostream>

using namespace std;

int main()
{
    CoInitialize(0);
    {
        HRESULT hr = -1;
        CComPtr<IUnknown> cnnobj;
        CComPtr<IUnknown> rdsnobj;
        CComPtr<IUnknown> errobj;
        hr = cnnobj.CoCreateInstance(L&quot;ADODB.Connection.2.7&quot;);
        ADODB::_Connection* cnn;
        ADODB::_Recordset* rds;
        ADODB::Error* err;
        BSTR berr;
        ADODB::Fields* fds;
        ADODB::Field* fd1;
        ADODB::Field* fd2;

        
        hr = cnnobj->QueryInterface(&cnn);
        hr = cnn->Open(L&quot;driver={Microsoft Access Driver (*.mdb)};dbq=[c:\\db1.mdb];&quot;, L&quot;&quot;, L&quot;&quot;, -1);
        
        hr = rdsnobj.CoCreateInstance(L&quot;ADODB.Recordset.2.7&quot;);
        hr = rdsnobj->QueryInterface(&rds);

        hr = errobj.CoCreateInstance(L&quot;ADODB.Error.2.7&quot;);
        hr = errobj->QueryInterface(&err);

        hr = rds->put_ActiveConnection(_variant_t(cnn));
        hr = rds->Open(_variant_t(&quot;select * from Table1&quot;), _variant_t(cnn),  ADODB::adOpenUnspecified, ADODB::adLockUnspecified, -1);

        VARIANT_BOOL eof;
        VARIANT_BOOL bof;
        hr = rds->MoveFirst();
        hr = rds->get_Fields(&fds);
        hr = fds->get_Item(_variant_t(L&quot;X&quot;), &fd1);
        hr = fds->get_Item(_variant_t(L&quot;Y&quot;), &fd2);
        BSTR namex;
        BSTR namey;
        hr = fd1->get_Name(&namex);
        hr = fd2->get_Name(&namey);

        cout<< &quot;filends count: &quot;<< fds->GetCount()<< endl;
        wcout<< L&quot;<&quot;<< namex<< L&quot;><&quot;<< namey<< L&quot;>&quot;<< endl;
        VARIANT valx, valy;
        while(true)
        {
            hr = rds->get_EOF(&eof);
            hr = rds->get_BOF(&bof);
            if(eof)break;
            if(bof)break;
            fd1->get_Value(&valx);
            fd2->get_Value(&valy);
            wcout<< L&quot;{&quot;<< (wchar_t*)_bstr_t(_variant_t(valx))<< L&quot;}{&quot;;
            wcout<< (wchar_t*)_bstr_t(_variant_t(valy))<< L&quot;}&quot;<< endl;

            hr = rds->MoveNext();
        }
        hr = rds->Close();
        hr = cnn->Close();

    }
	CoUninitialize();
	
	return 0;
}

//using ODBC
#include<windows.h>
#include<iostream>
#include<string>
#include<sql.h>
#include<sqlext.h>
using namespace std;
int main()
{
    HENV hEnv;
    HDBC hDbc;
    RETCODE rc;
    int iOut;
    char strOut[256];
    char szDSN[256] = &quot;driver={Microsoft Access Driver (*.mdb)};dbq=[c:\\db1.mdb];&quot;;
    //dsn samples:
    //&quot;driver={Microsoft Access Driver (*.mdb)};dbq=[f:\\db1.mdb];&quot;
    //&quot;driver={SQL Server};pwd={password there};Server={server name};Database={dbname there}&quot;
    //driver names for different databases:
    //{SQL Server}
    //{Microsoft ODBC for Oracle}
    //{Oracle in oracle9}
    //{Microsoft Access Driver (*.mdb)}
    //{MySQL ODBC 3.51 Driver}

    char* szSql = &quot;select * from table1&quot;;
    rc = SQLAllocEnv(&hEnv);
    rc = SQLAllocConnect(hEnv, &hDbc);

    rc = SQLDriverConnect(hDbc, NULL, (unsigned char*)szDSN, 
        SQL_NTS, (unsigned char*)strOut, 
        255, (SQLSMALLINT*)&iOut, SQL_DRIVER_NOPROMPT);
    {
        int ival;
        char chval[128];
        int ret1;
        int ret2;
        HSTMT hStmt;
        rc = SQLAllocStmt(hDbc,&hStmt);
        rc = SQLPrepare(hStmt,(unsigned char*)szSql, SQL_NTS);//1
        //rc = SQLBindCol(hStmt, tab_column, tr_type, tr_value, tr_len, len_or_ind);
        rc = SQLBindCol(hStmt, 1, SQL_C_ULONG, &ival, 4, (SQLINTEGER*)& ret1);
		rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval, 128, (SQLINTEGER*)&ret2);
        rc = SQLExecute(hStmt); //2
        //if you have queries like drop/create/many update... 
        //instead of //1 //2 and //3 you could use
        //rc=SQLExecDirectA(hStmt,(unsigned char*)szSql,SQL_NTS);
        cout<< &quot;>table:&quot;<< endl;
        while(1) //3
        {
            rc = SQLFetch(hStmt);
            if(rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)break;
            cout<< &quot;{&quot;<< ival<<&quot;}{&quot;<< chval<< &quot;}&quot;<< endl;
        }
        rc=SQLFreeStmt(hStmt, SQL_DROP);
    }
    rc = SQLDisconnect(hDbc);
    rc = SQLFreeEnv(hEnv);
    return 0;
}

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top