I would like to query an SQL Server database inside of C++<br><br>Books mention CDarabase and CRecordset but no decent examples.<br><br>Any help much appreciated.<br><br>
you can use ADO in VC++ to access anytype of database( as long as its supported by the ODBC32 driver)<br><br>for example you load the ADO libraries(I think there's a header that does this as well)<br><br>#import "C:\Program Files\Common Files\System\ADO\msado15.dll" rename_namespace("ADOCG" rename("EOF","EndOfFile"<br>using namespace ADOCG;<br>#include <icrsint.h><br><br>then to create a connection you need to declare and start a connection pointer<br><br>_ConnectionPtr pConnection;<br>pConnection.CreateInstance( __uuidof(Connection) );<br><br>to open the connection you use<br>m_pConnection->Open(L"DSN=ldap", L"", L"", -1);<br><br>DSN is whatever name you gave it when you put your database in the ODBC32 manager.<br>the two L"" first is user, second is password (keep the Ls attatched to front of "<br><br>then you create the recordset pointer<br><br>::CoInitialize(NULL); <br><br>_RecordsetPtr m_pRecordSet; <br>m_pRecordSet.CreateInstance( __uuidof(Recordset) );<br><br><br>(use ::CoInitialize(NULL); before using any COM objects<br>and ::CoUninitialize(); after using the objects )<br><br><br>heres an example on how to load a recordset using the Select Query<br><br>wsprintf(sqlexec,"select * from Users where Email = '%s' and Password = '%s'", CkPass.Email, CkPass.Password);<br>m_pRecordSet->Open( sqlexec, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText );<br><br>I copy the string I want into a variable(makes it easier to set parameters thru code) in this one it gets all records from the User table, where the Email is = to whatever i wanted and password the same etc. (just use select * from table to get all)<br><br>if(!m_pRecordSet->EndOfFile)<br>{<br> strncpy(m_user,CkPass.Email,MAX_PATH-1);<br> strcpy(CkPass.UID,FieldToChar(m_pRecordSet,L"UserID");<br> m_pRecordSet->Close(); ::CoUninitialize(); return TRUE;<br>}<br>m_pRecordSet->Close(); ::CoUninitialize(); return FALSE; <br>}<br><br>first line checks to make sure there were any records<br>must close a recordset (only if it remained opened from select query, using Insert, delete, update, etc anything that doesnt require the recordset to remain open, will automatically close itself, so dont try to close it unless you opened it with a select query)<br>My function FieldToChar which I made looks like this so you know how exactly to get a string out of a record<br><br>char* CUT_FTPThread::FieldToChar(_RecordsetPtr recset, _variant_t Fieldname) {<br>_variant_t tmpvariant; char tmpChar[255];<br>tmpvariant = recset->GetCollect(Fieldname); strcpy(tmpChar,(_bstr_t)tmpvariant);<br>return (tmpChar);<br>}<br><br>That help any? I assume you know SQL commands so this shouldnt be hard at all.<br> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href=
</a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.