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

All the books that I4ve 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 ;-) ).

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 wich "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...:

    MyRecordSet=new CMyRecordSet(m_Database);
    MyRecordSet.Open() // You can open it in many ways, see the help
    .
    .
    .
    MyRecordSet.Close()

  • Don4t forget when you exit from the program to close the database...

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



Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds