2011-06-22 194 views
2

我需要一些严重的帮助,试图使用VS 2008的C++连接到Access数据库。我在C#中完成了这个工作,但我无法用C++来解决这个问题。我需要使用C++进行连接,因为我使用预编译的C++代码来获取数据。我真的很感谢一些帮助。由于 我想ODBC,但如果你有其他建议,然后,我可能改变我的mind.I我试图连接到Access数据库,罗斯文示例数据库,通过下面这个例子中,使用C++使用Visual Studio 2008连接到MS Access数据库

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

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

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; 
} 

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

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 

任何帮助,将不胜感激。

感谢 但丁

+0

这里是一个很好的起点:http://msdn.microsoft.com/en-us/library /kd4ck1tt(v=vs.80).aspx –

+0

重复错误消息:“你忘了添加'#include”stdafx.h“'到你的源码?”。如果您使用预编译头文件“#include”stdafx.h“'必须是每个编译单元的第一个非注释行。 – IInspectable

回答

-1

你只需要给文件属性*。 CPP

- >预编译的头,没有预编译头

使用预编译的头文件 - Stdafx.h中XXXX

+2

SO上的问题和答案应该是英文的。理解它可能不是你的母语,但尽量用英语写作。 – kenrogers

+0

我试过了一个粗略的翻译,但是如果它错过了你原来的意思,请编辑它。 –

相关问题