Thread: How to add a database from access in c++

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    5

    Question How to add a database from access in c++

    Hi all,
    I have created database using MS access and i want to access it in C++ how can i do that?
    and also if this is possible on MFC too can anyone tell me how?
    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Try a Search: Key Word(s): access, database
    Moved to windows board, since it's all about the win32 API
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    For simple stuff I use ODBC.

    Look at CRecordSet. Once you have a DSN you can derive a specific record set for each table in the DB.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    5
    thanks all for your replies
    I have done all using ODBC which really works fine with me
    But there is a problem!
    I want to search for an item
    My project is so simple and the database is composed of only one table one of its columns is the name
    what i wanna do is to add a feature to search for a name which is Data dictionary
    thank you very much

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Something like...

    (Add exception and error handlers)

    Code:
    CString    SQL,UserName;
    
    
    CDataBase                          DB;
    //must open DB
    
    //then connect recordset
    CMyDBTableRecordSet        Table(&DB);
    
    //get username from an edit?
    UserName.Format("Justin Case");
    
    //set up SQL string to send to DB
    // get whole record from the MyDBTable for 'Justin Case'
    SQL.Format("SELECT * FROM MyDBTable WHERE UserName=%s",UserName); Table.Open(AFX_DB_USE_DEFAULT_TYPE, SQL ,CRecordset::none);
    
    if(!Table.IsBOF())//have records
    //use
    
    //close recordset
    //close db
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    5
    Thanks novacain
    thank you very much
    I have some question what is UserName? is it the variable declaring the name field?
    //must open DB

    //then connect recordset
    CMyDBTableRecordSet Table(&DB);
    How can i open DB and connect to recordset?
    and what about Table(&DB) is Table is a predefined function?
    AFX_DB_USE_DEFAULT_TYPE
    what is the default type for the database is it the primary key?
    sorry for my newbie questions and thanks again for your great help

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I'm not going to write this for you, have a go yourself.

    Post code if you get stuck and I am happy to help (if I have time).


    UserName is a variable with the search criteria (in this case the name to look for)

    To open the database, declare a CDatabase variable and call the member function Open() or OpenEx()

    Table in my example is a variable. Its type/class is that of a CRecordset derived from your DB's table. MSVC will do this for you (if you push the right buttons).

    By specifing a pointer to an open DB (when constructing the Table variable ie Table(&DB) ) we conect the Table to the open DB.

    AFX_DB_USE_DEFAULT_TYPE

    Read CRecordset::Open() for more info.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    5
    It's done
    Thank you novacain for your great efforts

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CGI with MS Access database integration
    By Tarran in forum Tech Board
    Replies: 12
    Last Post: 01-11-2005, 09:47 AM
  2. C and database access
    By afisher in forum Linux Programming
    Replies: 4
    Last Post: 09-30-2004, 02:33 AM
  3. connecting to ms access database
    By fkheng in forum Windows Programming
    Replies: 12
    Last Post: 07-06-2003, 07:49 PM
  4. online database access in C?
    By sks24 in forum C Programming
    Replies: 0
    Last Post: 03-21-2002, 06:08 PM