Click here to Skip to main content
Click here to Skip to main content
Go to top

How to use RecordSets without using the AppWizard at the creation of your project.

, 17 Nov 1999
Rate this:
Please Sign up or sign in to vote.

All the books that I´ve read have the official chapter " Database programming", a little chapter in which there is the typical example of: Create a new project, with App wizard select the option use database and select what database will you use...

Ok, I begin my application, and one month later I realize "mmm...., I need one database for this project", or... " I need to add another Recordset to the database", or... " I need to add another database", must I begin again with my application? The answer is NO, and the solution is easy ( a dummy like me uses it, so it must be easy Wink | ;-) ).

Steps to do this:

  • Create a normal project without database support ( or if you have one created use that...).
  • In stdafx.h add this include:
    #include "afxdb.h"
  • In the class in wich you want to use the database ( for example in the View), add this member variable:
    CDatabase m_MyDatabase
  • When you init that class ( for example if we use the View in on Initial Update), init your database, calling
    m_MyDatabase.Open(NULL,FALSE,FALSE,os)
    (In which "os" is only a CString with the parameters to open your database, for example: CString os="ODBC;DSN=MyDb;UID=MyUser;PWD=MyPassword;") In this string we put:
    • ODBC (using ODBC database),
    • DSN=... The name of the datasource ( this is the name that you put when you add to the database the ODBC administrator your database),
    • UID=... Is the name of the user of the database,
    • PWD=... Is the password of that user.
  • Now we need an special recordset to get the data from our database. This is easy. Create a class ( with AppWizard ( Ctrl+W Key...), Add New Class), that inherits from CRecordSet then select your database and the table that you want to get from that database, and the fields that you need, ( you can create a lot of classes inherited from CRecordSet, as many "views" of your database as you need ( for example one recordset for each table or...).
  • Include the ".h" file of the recordset in the file in wich you are going to use it, and input variables of that recordset class in the classes that you need...
    CMyRecordSet *MyRecordSet; 
  • Then to open the recordSet:
    pMyRecordSet=new CMyRecordSet(&m_MyDatabase);
    pMyRecordSet->Open() // You can open it in many ways, see the help
    .
    .
    .
    pMyRecordSet->Close() 
    
  • Don´t forget when you exit from the program to close the database
    m_MyDatabase.Close();
    

And...,If you want to see more things, take a look at the help, this is only the starting point ( made by a dummy... Smile | :) ).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Braulio Dez
Web Developer
Spain Spain
Braulio is a developer specialized on Ms Technologies (Silverlight, ASP .net, SSRS, SSAS, SSIS). Currently he is working for Avanade in Málaga and as a journalist for the Spanish .net magazine Dotnetmania. He also is the webmaster of an specializad in .net technologies site Tipsdotnet.com, and Silverlight Based DB Schema Editor (www.dbschemaeditor.com).

Comments and Discussions

 
GeneralSuper article Pinmemberdanielthompson8521-Jun-09 1:01 
Generalthank you Pinmemberdesirejoy18-May-09 12:54 
GeneralAbout the translation. Pinmembergreatghoul29-Mar-09 2:45 
GeneralRe: About the translation. PinmemberBraulio Dez29-Mar-09 2:56 
GeneralRe: About the translation. Pinmembergreatghoul29-Mar-09 3:56 
QuestionHow to Connect two Clients which are in different LANs Pinmemberpengpcz17-Jan-08 21:35 
GeneralSynchronization Pinmemberibelca5-Jun-07 6:01 
GeneralOn Database support Pinmemberswapna_signsin11-May-06 0:14 
QuestionHow do u get the data intothe edit boxes(Beginner) Pinmemberswapna_signsin7-Apr-06 2:45 
QuestionHow to use RecordSets without using the AppWizard at the creation of your project PinmemberAshwani_218-Jul-05 21:41 
QuestionHow to retrieve a integer or number Pinmemberanupkumar8-Sep-03 0:20 
GeneralClerical error! PinmemberPole23-Aug-02 17:52 
QuestionWhere is the best place for CMyRecordSet *MyRecordSet? PinmemberKanada5-Apr-02 1:12 
AnswerRe: Where is the best place for CMyRecordSet *MyRecordSet? PinmemberKanada5-Apr-02 1:39 
GeneralRe: Where is the best place for CMyRecordSet *MyRecordSet? PinmemberCarlos Antollini5-Apr-02 3:36 
Generalnew article:) no HTML PinmemberAnonymous3-Apr-02 0:01 
GeneralRe: new article:) no HTML PinmemberBraulio Díez26-Apr-02 1:17 
Generalnew article:) PinmemberAnonymous2-Apr-02 23:59 
Generalmore help PinmemberMalicia3-Feb-02 7:56 
thank's for that useful method, I need more help, I create an MFC application with database functionnalities so I do everything you describe until creating the class inherited from recordset, then I found some difficulties.
I have many dialog boxes which are different forms from which I get edit control's values and put them in the data base record, how can I do that with your method (where do I declare the pointer to the recordset, and how to add a new record, (put every CString value I get from the edit control and put it in it's corresponding database variable)
 
thank's in advance.
GeneralThanks Pinmemberfeedback4-Oct-01 20:25 
QuestionCDatabase :: How to?? PinmemberFarah Mansor21-May-01 16:58 
AnswerRe: CDatabase :: How to?? Pinmemberhoheisel14-Aug-01 8:19 
GeneralRegd DataBase Sample PinsussPrakash21-Mar-00 17:22 
GeneralRe: Regd DataBase Sample PinmemberSteven Gregg31-May-01 0:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 18 Nov 1999
Article Copyright 1999 by Braulio Dez
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid