使用Visual Studio 2008使用C ++连接到MS Access数据库 [英] Connecting to MS Access database using C++ using Visual Studio 2008

查看:124
本文介绍了使用Visual Studio 2008使用C ++连接到MS Access数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些认真的帮助,试图使用VS 2008的C ++连接到Access数据库.我已经在C#中完成了此操作,但在C ++中却无法解决.我需要使用C ++进行连接,因为我正在使用预编译的C ++代码获取数据.我真的很感谢您的帮助.谢谢 我想使用odbc,但是如果您还有其他建议,那么我可以改变主意.我尝试通过以下示例连接到Access数据库(罗斯文示例数据库)

I need some serious help trying to connect to an Access database using VS 2008's C++. I have done this in C# but I cant figure this out in C++. I need to use C++ for the connection because I am grabbing data using pre-compiled C++ code. I would really appreciate some help with this. Thanks I would like to odbc, but if you have another recommendation then I could change my mind.I am trying to connect to an Access database, the Northwind sample database, by following this example,

http://msdn.microsoft.com/en-us/library /cc811599.aspx

我将Windows 7 OS与Visual C ++ 2008一起用于编译器和IDE.该程序是一个控制台应用程序.为Access 2007 .accdb文件类型指定了此示例.一旦它正确运行,我将把路径名,查询和表名切换到我的数据库.下面是无法构建的代码.我不知道是什么原因造成的:

I am using a Windows 7 OS with Visual C++ 2008 for the compiler and IDE. The program is a console application. This example is specified for Access 2007 .accdb file types. Once I get it running correctly I will switch the path name, queries, and table names to my database. Below is the code that fails to build. I don't know what is causing this:

Includes-->
   fstream
cmath
complex
iostream
iomanip
vector
limits
stdlib.h
stdio.h
time.h
fcntl.h
string.h
ctype.h
icrsint.h

using namespace std;



#import C:\\Program Files\\Common Files\\system\\ado\\msado15.dll rename("EOF",
      "AdoNSEOF")

_bstr_t bstrConnect="Provider=Microsoft.ACE.OLEDB.12.0;Data " 
                    "Source=C:\\Users\\lriley\\Documents\\Northwind 2007.mdb;";

HRESULT hr;

int main()
{
::CoInitialize(NULL);
const char* DAM = "ADO";

ADODB::_ConnectionPtr pConn("ADODB.Connection");
hr = pConn->Open(bstrConnect, "admin", "", ADODB::adConnectUnspecified);
if(SUCCEEDED(hr))
{
    cout<<DAM<<": Successfully connected to database. Data source name:\n  "
        <<pConn->GetConnectionString()<<endl;

    // Prepare SQL query
    _bstr_t query = "SELECT Customers.[Company], Customers.[First Name] FROM "
                            "Customers;";
    cout <<DAM<<": SQL query \n  "<<query<<endl;

    // Execute
    ADODB::_RecordsetPtr pRS("ADODB.Recordset");
    hr = pRS->Open(query,
        _variant_t((IDispatch *) pConn, true),
        ADODB::adOpenUnspecified,
        ADODB::adLockUnspecified,
        ADODB::adCmdText);
    if(SUCCEEDED(hr))
    {
        cout<<DAM<<": Retrieve schema info for the given result set: "<< endl;
        ADODB::Fields* pFields = NULL;
        hr = pRS->get_Fields(&pFields);
        if(SUCCEEDED(hr) && pFields && pFields->GetCount() > 0)
        {
            for(long nIndex=0; nIndex < pFields->GetCount(); nIndex++)
            {
                cout << " | "<<_bstr_t(pFields->GetItem(nIndex)->GetName());
            }
            cout << endl;
        }
        else
        {
            cout << DAM << ": Error: Number of fields in the " <<
                           "result is set to zero." << endl;
        }
        cout<<DAM<<": Fetch the actual data: " << endl;
        int rowCount = 0;
        while (!pRS->AdoNSEOF)
        {
            for(long nIndex=0; nIndex < pFields->GetCount(); nIndex++)
            {
                cout<<" | "<<_bstr_t(pFields->GetItem(nIndex)->GetValue());
            }
            cout<< endl;
            pRS->MoveNext();
            rowCount++;
        }
        cout<<DAM<<": Total Row Count:  " << rowCount << endl;
    }
    pRS->Close();
    pConn->Close();
    cout<<DAM<<": Cleanup Done" << endl;
}
else
{
    cout<<DAM<<" : Unable to connect to data source: "<<bstrConnect<<endl;
}
::CoUninitialize();
return 0;
}

当我尝试构建它时,我收到以下错误:

I recieve the following error when I try to build it:

fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add #include "stdafx.h" to your source?   

c:\users\lriley\documents\visual studio 2008\projects\test12\test12\test12.cpp  

任何帮助将不胜感激.

谢谢 但丁

推荐答案

嗯,已经有一段时间了,但是您将需要以下内容:

Well, it's been a while, but you are going to need something like: http://msdn.microsoft.com/en-us/library/ms714562%28v=vs.85%29.aspx, look at SQLConnect..., a lot of variations to a theme, but the 2nd parameter is basically the path to your access db (*.mdb) file.

祝你好运.

这篇关于使用Visual Studio 2008使用C ++连接到MS Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆