Tuesday, March 20, 2012

Accessing a Microsoft Access database from within Visual C++

Hi there guys, I am currently trying to achieve a seemingly simple task in VC++ 2005. I have made a very simple form in Microsoft Access which I wish to serve as the beginnings of something greater. I created a db in MS Access named links.mdb containing on table-> Table1. Table1 contains 1 column, "Links", and i wish to read these strings into variables in my Visual C++ Windows Forms Application.

What I have done so far...

In Visual C++, I clicked on Data->Add new data source, and followed the wizard to add the microsoft access database to my application by the name, "linksDataSet". I can see the table in my left hand "Data Sources" pane in VC++. All I need to know is how to access my database from here so that I can read these strings stored in my table. Also, would be possible to schedule my application to log on to a http server and retrieve these links every time the application is executed? How would I go about doing this?

Thank you very much for your time
Regards
Linden.Umm... hi, my topic has been moved into this forum, even though I don't think it belongs here because my question is VC++ database related but can anyone help me? I would greatly appreciate it.|||44 Views and not one reply? Is this not a familiar concept?|||This is ridiculous! Why is this forum here? It obviously serves no purpose.|||

To read a Microsoft Access database table from VC++ 2005:

1. First I created a new Windows Console project in VC++.

2. Then Project | Add Class... then go under ATL and choose ATL OLEDB Consumer.

3. Click Data Source and choose Microsoft Jet 4.0 OLEDB Provider, Next>>> then type in database name.

4. Click OK, another dialog comes up, choose your table, it will create a single class for your table.

Then the code to read the data is like so:

#include "stdafx.h"

#include "Table1.h"

int _tmain(int argc, _TCHAR* argv[])

{

CoInitialize(NULL); // Be sure to initialize COM somewhere in your app one time...

CTable1 table1;

HRESULT hr = table1.OpenAll();

for(;;)

{

hr = table1.MoveNext();

if (S_OK != hr) break;

printf("table1.f1=%lu\n", table1.m_f1);

printf("table1.f2=%S\n", table1.m_f2);

}

return 0;

}

|||I am currently working in a windows forms application, how would the code change?

No comments:

Post a Comment