Help: C++ and Microsoft Access Database

Hello all!
I am currently working on a project for class that involves using programs to interact with a database. I have chosen to use C++ (which may not have been the best option for interacting with DB) and am struggling some trying to connect to the database I have created.

What exactly it is I am trying to do is this:
I have created a VB program that takes data entered by a user and stores it into a Microsoft Access DB. After storing said data, the VB program then opens up a C++ program which takes the data from the database and stores it into variables inside of C++ to use throughout the program. The part I'm having the most trouble with right now, is getting my C++ program to read that data from the DB and store it into variables.

I have been reading up a lot on the different ways to do this, but have not come across anything solid to help me. I'm aware of OLE DB, ADO, etc., but can't seem to grasp the best way to utilize them. I'm simply looking for a way to have my C++ program connect to the database, read the data stored in each field, and store that data into its own variable. I am not trying to use the C++ program to write anything into the database, I'm only looking for a solution on how to read it and store it inside of C++.

Like I said above, I have researched this as fully as I could before coming here to post. I have read up on: https://msdn.microsoft.com/en-us/library/cc811599(v=office.12).aspx, but cannot find anything conclusive and am not very experienced with this sort of coding. I'm aware C++ is not the best language for this particular program, but it is the language I went with for this project.

Any and all help will be greatly appreciated!

Last edited on
Does anyone else have any other suggestions or help they could provide? I've check out that link, but I'm afraid I can't seem to apply it directly to what I'm trying to do.
Would MFC or .NET be an option ?
I'm not as familiar with those, but I am willing to look into/learn about them if they would be able to solve my problem. I'm currently building my C++ program inside of Visual Studio 2013.
Very good that you have Visual Studio so you don't need to install anything.
In C++/CLI it's very easy. Here is a simple demo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "stdafx.h"
#using <mscorlib.dll>
#using <System.Data.dll>
#using <System.dll>

using namespace System;
using namespace System::Data::OleDb;

int main (void)
{
  String^ sqlstr = "SELECT * FROM [Categories]";

  OleDbConnection^ conn = nullptr;
  OleDbCommand^ cmd = nullptr;

  try
  {
    conn = gcnew OleDbConnection ("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Temp\\Northwind.MDB");
    conn->Open ();
    cmd = gcnew OleDbCommand (sqlstr, conn);

    OleDbDataReader^ reader = cmd->ExecuteReader (System::Data::CommandBehavior::CloseConnection);
    String^ Sep = gcnew String ('*', 60);

    while (reader->Read ())
    {
        Console::WriteLine ("CategoryID: " + reader["CategoryID"]);
        Console::WriteLine ("CategoryName: " + reader["CategoryName"] + "\t");
        Console::WriteLine ("Description: " + reader["Description"]);
        Console::WriteLine (Sep);
    }
  }
  catch (Exception^ ex)
  {
    Console::WriteLine (ex->ToString ());
  }
  Console::ReadLine ();
  return 0;
}


Just create a simple Console Application under File->New Project->Visual C++->CLR and copy the the above code into the file where the main function is.

Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
CategoryID: 1
CategoryName: Beverages
Description: Soft drinks, coffees, teas, beers, and ales
************************************************************
CategoryID: 2
CategoryName: Condiments
Description: Sweet and savory sauces, relishes, spreads, and seasonings
************************************************************
CategoryID: 3
CategoryName: Confections
Description: Desserts, candies, and sweet breads
************************************************************
CategoryID: 4
CategoryName: Dairy Products
Description: Cheeses
************************************************************
CategoryID: 5
CategoryName: Grains/Cereals
Description: Breads, crackers, pasta, and cereal
************************************************************
CategoryID: 6
CategoryName: Meat/Poultry
Description: Prepared meats
************************************************************
CategoryID: 7
CategoryName: Produce
Description: Dried fruit and bean curd
************************************************************
CategoryID: 8
CategoryName: Seafood
Description: Seaweed and fish
************************************************************


If you don't have the northwind database you can download it here:
http://www.filewatcher.com/m/Northwind.MDB.3424256-0.html
This is perfect!! Thank you! I just have two questions following this.
First, am I able to take that information from the database and store it into variables which can then be used throughout the rest of my program? It looks like right now all it's doing is reading the database and displaying it in output via the C++ program.

Second, is there a way to link this console application up with the current C++ program I have written so that my program can access the variables that the database information will be stored in? Or am I going to have to write the whole program inside of this CLR?
Or am I going to have to write the whole program inside of this CLR?

I am afraid so. However you can have ordinary C++ inside the CLR code.
Well if I'm able to code it in regular C++ I don't think that will be a problem. Just gonna do a little researching to see the best way to use this CLR to store the data from the database into variables.
Thanks for your responses and help!
I do not recommend using Thomas1965's code as-is because the connection, command, and reader objects are not being properly cleaned up. Since the reader is never explicitly closed, the CommandBehavior setting is irrelevant to the connection. Even if it was, an exception could occur between connection initialization and reader execution, resulting in a resource leak. Regardless, the command object's resources are simply left unaddressed.
Is there a way to assign this data it is pulling from the database to variables inside of the program?

while (reader->Read ())
{
Console::WriteLine ("CategoryID: " + reader["CategoryID"]);
Console::WriteLine ("CategoryName: " + reader["CategoryName"] + "\t");
Console::WriteLine ("Description: " + reader["Description"]);
Console::WriteLine (Sep);
}
Sure there is - for example:
String ^ CategoryName = reader["CategoryName"]->ToString();
Topic archived. No new replies allowed.