ODBC connection
cyq84 (2)Oct 6, 2008 at 4:56pm UTC
Hi all, May i know how to establish a ODBC connection to SQL sever 2005 via c++ and retrieve the data from the database at the console application? I had written the following codes: try { CDatabase cdb; CString sSQLStmt ="SELECT sequenceId FROM AuthEvents"; int bconnect; CString str = "ODBC;DSN=EARTH;UID=sa;PWD=pass#word1;" ; bconnect=cdb.Open(str); if(!bconnect) printf("failed"); else printf("connection open successfully"); CRecordset rs(&cdb); rs.m_strFilter =sSQLStmt; rs.Open(); } catch(_com_error ) { printf("Error"); } Currently, the program was able to open the connection successfuly but there is an error (runtime error) when it runs the line rs.Open().Could someone help me with this? Thanks
SteakRider (110)Oct 6, 2008 at 4:56pm UTC
How did you know it is connected or not? use this class to connect dbms I know it does not look nice, but it works, just change connection string, and run sql 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <string>
#define ENGLISH_MAX 2500
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename( "EOF", "adoEOF" )
class cReadText
{
public :
cReadText();
cReadText(char * Filename);
void ConnectionAdo();
_RecordsetPtr GetRecordset(_bstr_t SQL);
void RunCammand(_bstr_t SQL);
~cReadText();
private :
_ConnectionPtr m_pConn;
_RecordsetPtr pRecordset;
_CommandPtr pCommand;
CString sCommand;
_bstr_t strSQL;
bool bConnected;
int done;
int englishmax;
FILE* Filesourse;
char * chFilename;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#include "StdAfx.h"
#include <fstream>
#include "FileReader.h"
using namespace std;
cReadText::cReadText()
{
CoInitialize(NULL);
bConnected = false ;
englishmax = 0;
}
cReadText::cReadText(char * Filename)
{
CoInitialize(NULL);
chFilename = Filename;
bConnected = false ;
englishmax = 0;
}
void cReadText::ConnectionAdo()
{
bConnected = true ;
try
{
HRESULT hr = m_pConn.CreateInstance (__uuidof (Connection));
if (FAILED (hr))
{
AfxMessageBox ("Can't create intance of Connection" );
}
if (FAILED (m_pConn->Open (_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = EmailReminder.mdb;" ),
_bstr_t ("" ), _bstr_t ("" ), adModeUnknown)))
{
AfxMessageBox ("Can't open datasource" );
}
}
catch ( _com_error &e )
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE( "Exception thrown for classes generated by #import" );
TRACE( "\tCode = %08lx\n" , e.Error());
TRACE( "\tCode meaning = %s\n" , e.ErrorMessage());
TRACE( "\tSource = %s\n" , (LPCTSTR) bstrSource);
TRACE( "\tDescription = %s\n" , (LPCTSTR) bstrDescription);
AfxMessageBox ((LPCTSTR) bstrDescription);
AfxMessageBox ((LPCTSTR) e.Source());
bConnected = false ;
}
catch (...)
{
bConnected = false ;
}
}
void cReadText::RunCammand(_bstr_t SQL)
{
m_pConn->Execute(SQL,NULL,adExecuteNoRecords);
}
_RecordsetPtr cReadText::GetRecordset(_bstr_t SQL)
{
pCommand.CreateInstance (__uuidof (Command));
pCommand->ActiveConnection = m_pConn;
pCommand->CommandText = SQL;
pRecordset.CreateInstance (__uuidof (Recordset));
pRecordset->CursorLocation = adUseClient;
pRecordset->Open ( (IDispatch *) pCommand, vtMissing, adOpenStatic,adLockBatchOptimistic, adCmdUnknown);
return pRecordset;
}
cReadText::~cReadText()
{
if (bConnected)
{
m_pConn->Close();
}
CoUninitialize();
}
Last edited on Oct 6, 2008 at 4:56pm UTC
cyq84 (2)Oct 6, 2008 at 4:56pm UTC
Hi, I had solved it. It is calling using ODBC. Thanks anyway. CString sSQLStmt ="SELECT * FROM aaa"; int bconnect; CString str = "ODBC;DSN=xxxxx;UID=sa;PWD=pass;" ; bconnect=cdb.Open(str); if(!bconnect) printf("failed"); else printf("connection open successfully\n"); CRecordset rs(&cdb); CString varValue; CDBVariant varValue1; _bstr_t eventTime; rs.Open( CRecordset::forwardOnly, sSQLStmt ); short nFields = rs.GetODBCFieldCount( ); while( !rs.IsEOF( ) ) { rs.GetFieldValue( "[column name]", varValue); eventTime = varValue; printf("eventTime: %s\n\n", varValue); rs.MoveNext( ); } rs.Close( );
This topic is archived - New replies not allowed.