Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ To Access ODBC Database ??? 1

Status
Not open for further replies.

dlx

Programmer
Apr 11, 2000
2
US
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>
 
Do you have VC++. If yes then I can help you in some manner.<br><br>Thanx<br>Siddhartha Singh
 
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 &quot;C:\Program Files\Common Files\System\ADO\msado15.dll&quot; rename_namespace(&quot;ADOCG&quot;) rename(&quot;EOF&quot;,&quot;EndOfFile&quot;)<br>using namespace ADOCG;<br>#include &lt;icrsint.h&gt;<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-&gt;Open(L&quot;DSN=ldap&quot;, L&quot;&quot;, L&quot;&quot;, -1);<br><br>DSN is whatever name you gave it when you put your database in the ODBC32 manager.<br>the two L&quot;&quot; first is user, second is password (keep the Ls attatched to front of &quot;)<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,&quot;select * from Users where Email = '%s' and Password = '%s'&quot;, CkPass.Email, CkPass.Password);<br>m_pRecordSet-&gt;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-&gt;EndOfFile)<br>{<br> strncpy(m_user,CkPass.Email,MAX_PATH-1);<br> strcpy(CkPass.UID,FieldToChar(m_pRecordSet,L&quot;UserID&quot;));<br> m_pRecordSet-&gt;Close(); ::CoUninitialize(); return TRUE;<br>}<br>m_pRecordSet-&gt;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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char tmpChar[255];<br>tmpvariant = recset-&gt;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)
 
Yes, I am using VC++.<br><br>Many thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top