You need to look into using the CDatabase[^] class. Below is come code to read some records from an Access DB which can be easily modified for MS SQL Server. Using those two classes you can update, read, create, etc. I believe I got much of this original code from CP but I cannot for the life of me remember where so I can't credit the author.
CDatabase m_database; // Our database
CRecordset *m_recset; // Pointer to a record
bool Read()
{
CString SqlString;
CString sVideoName, sFileLocationRelative;
CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
CString sDsn;
CString sFile = "Database\\VideoDB2.mdb";
// You must change above path if it's different
// Build ODBC connection CString
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
// Open the database
m_database.Open(NULL,false,false,sDsn);
// Allocate the recordset
m_recset = new CRecordset( &m_database );
SqlString = "SELECT * FROM VIDEOS";
// Execute the query
m_recset->Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
// Loop through each record
while( !m_recset->IsEOF() )
{
VideoRecord tVideo;
CDBVariant vID;
CDBVariant vRunTime;
CDBVariant vName;
CDBVariant vImagePath;
CDBVariant vVideoPath;
CDBVariant vDescription;
// Copy each column into a variable
m_recset->GetFieldValue("VIDEO_ID", vID);
m_recset->GetFieldValue("VIDEO_RUN_TIME", vRunTime);
m_recset->GetFieldValue("VIDEO_NAME", vName);
m_recset->GetFieldValue("VIDEO_IMAGE_PATH",vImagePath);
m_recset->GetFieldValue("VIDEO_PATH", vVideoPath);
m_recset->GetFieldValue("VIDEO_DESCRIPTION",vDescription);
tVideo.iVideoID = vID.m_iVal;
tVideo.lRunTime = vRunTime.m_lVal;
tVideo.stVideoName = *vName.m_pstring;
tVideo.stImagePath = *vImagePath.m_pstring;
tVideo.stVideoPath = *vVideoPath.m_pstring;
tVideo.stDescription = *vDescription.m_pstring;
m_vVideos.push_back(tVideo);
// goto next record
m_recset->MoveNext();
}
m_recset->Close();
}
CATCH(CDBException, e)
{
// If a database exception occured, show error msg
AfxMessageBox("Database error: "+e->m_strError);
return false;
}
END_CATCH;
return true;
}
By the way, that VideoRecord structure was just a part of my program