[英]How to connect ms access 2013 database to visual studio 2013 using c++
[英]Connecting to MS Access database using C++ using Visual Studio 2008
我需要一些认真的帮助,试图使用VS 2008的C ++连接到Access数据库。 我已经在C#中完成了此操作,但是在C ++中却无法解决。 我需要使用C ++进行连接,因为我正在使用预编译的C ++代码获取数据。 我真的很感谢您的帮助。 谢谢,我想使用odbc,但是如果您还有其他建议,那么我可以改变主意。通过以下示例,我尝试连接到Access数据库(罗斯文示例数据库),
http://msdn.microsoft.com/zh-CN/library/cc811599.aspx
我将Windows 7 OS与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
任何帮助,将不胜感激。
谢谢但丁
好吧,已经有一段时间了,但是您将需要类似以下内容: http : //msdn.microsoft.com/zh-cn/library/ms714562%28v=vs.85%29.aspx ,请看一下SQLConnect ... ,对主题有很多变化,但是第二个参数基本上是访问db(* .mdb)文件的路径。
祝好运。
您只需要提供文件属性*。 Cpp
->预编译头-没有预编译头
使用预编译的头文件-Stdafx.h xxxx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.