Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

How do i write a code that connects to SQL and takes the data and display it.

I want to do this in usng C/C++.

I have done lots of googling on it, didnt got any thing.

Any one have any solution. Pls let me know.

Thanks
:)
Posted
Comments
AshwinK4 17-Sep-13 5:05am    
How can this be done in MFC dialog based app?

The following program takes all data from a table called TestTable in the database TestDB and displays to the user
Here you need to replace the Data Source(Your server name), Initial Catalog(your DB name) and table name
C++
#include "stdafx.h"
#include <stdio.h>
#include <ole2.h>
#include <iostream>

#import "C:/Program Files/Common Files/System/ado/msado15.dll"  rename( "EOF", "AdoNSEOF" )

using namespace std; 
using namespace ADODB;

_bstr_t bstrConnect("Provider='SQLOLEDB'; Data Source='PC001\\SQLEXPRESS'; Initial Catalog='TestDB'; Integrated Security='SSPI';");
int main() 
{

	HRESULT hResult = CoInitialize( 0 );
    if( FAILED( hResult ))
    {
        return hResult;
    }
    try
    {
        ADODB::_ConnectionPtr pConnect("ADODB.Connection");
        hResult = pConnect->Open( bstrConnect, "admin", "", ADODB::adConnectUnspecified );
        if (SUCCEEDED(hResult))
        {
            _bstr_t query = "SELECT * FROM TestTable;";
            ADODB::_RecordsetPtr pRecSet( "ADODB.Recordset" );
            hResult = pRecSet->Open( query, _variant_t((IDispatch *) pConnect, true), ADODB::adOpenUnspecified,
                                     ADODB::adLockUnspecified, ADODB::adCmdText);
            if( SUCCEEDED( hResult ))
            {
                ADODB::Fields* pFields = NULL;
                hResult = pRecSet->get_Fields( &pFields );
                
                while( !pRecSet->AdoNSEOF )
                {
                    for( long lIndex=0; lIndex < pFields->GetCount(); lIndex++ )
                    {
                        cout<<" | ";
						cout << _bstr_t(pFields->GetItem(lIndex)->GetValue());
                    }
					cout<< "\n----------------------------------------------------------------------------\n";
                    pRecSet->MoveNext();
                }
            }
            pRecSet->Close();
            pConnect->Close();
        }
    }
    catch( _com_error& e )
    {
        // Handle Exception
    }

    // Release COM
    CoUninitialize();
	getchar();
    return hResult;
}
 
Share this answer
 
v2
Comments
amit28august 19-Apr-12 5:42am    
Millions of thanks to you........i was struggling for last 2 days.....
thanks a lot dear.....
:)
Resmi Anna 19-Apr-12 6:17am    
so sad..someone really downvoted:-( :-( :-(
amit28august 19-Apr-12 6:30am    
hey dosent matter.......It solves my prob, for me its always up only.......
Resmi Anna 19-Apr-12 6:45am    
Nice to hear that:-)
Prasad_Kulkarni 19-Apr-12 8:19am    
Hey Amit, do you know GIT[^]
 
Share this answer
 
v2
Comments
amit28august 19-Apr-12 5:17am    
hi, thanks for reply, here i am not able to locate these file :sqlfront.h or sqldb.h, where do i get these files.
Also dont want to go for third party tools.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900