VC++ and MS SQL Server 2005 database
-
I have been searching the net for a simple database application using visual c++. I want somebody to help me with how to develop just a small application for entering records, deleting, updating and searching using visual c++ and ms sql server 2005. Even if it can be for one record, I can extend that to whatever the number of columns I have in my database. I have been developing some but all in vain. Nothing seems to work. Please!:((
phokojoe
-
I have been searching the net for a simple database application using visual c++. I want somebody to help me with how to develop just a small application for entering records, deleting, updating and searching using visual c++ and ms sql server 2005. Even if it can be for one record, I can extend that to whatever the number of columns I have in my database. I have been developing some but all in vain. Nothing seems to work. Please!:((
phokojoe
phokojoe wrote:
Nothing seems to work
What have you tried that didn't work? Is the problem UI related? Database access related?
phokojoe wrote:
how to develop just a small application for entering records, deleting, updating and searching
Maybe break that down into managable chunks and show some sample code that you're having trouble with and we can hopefully help you out. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
I have been searching the net for a simple database application using visual c++. I want somebody to help me with how to develop just a small application for entering records, deleting, updating and searching using visual c++ and ms sql server 2005. Even if it can be for one record, I can extend that to whatever the number of columns I have in my database. I have been developing some but all in vain. Nothing seems to work. Please!:((
phokojoe
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