Thursday, August 23, 2012

Read SQL table with VC++ MFC


/***********************************************************************
/* Description: /* : Use the following code to open the database and read the data from the required table */
/* include the header 'afxdb.h' */
***********************************************************************/


CDatabase db;
db.OpenEx(L"server-connection-string");
CRecordset rs(&db);
rs.Open(CRecordset::dynaset,L"select * from [tablename]");
CString strVal;
rs.GetFieldValue(L"CoulmnName",strVal); // you have the value in 'strVal'

Wednesday, August 22, 2012

Execute SQL query with VC++ MFC



/***********************************************************************
/* Description: /* : Use the following code to open the database and execute the SQL query */
/* include the header 'afxdb.h' */
***********************************************************************/



CDatabase db;

try
{
db.OpenEx(L"server-connection-string");
db.ExecuteSQL(L"query");
}
catch(CDBException & e)
{
// handle the exception here in case of querying failures
}