VS 2008のC++を使用してAccessデータベースに接続しようとすると、深刻な助けが必要です。私はこれをC#でやったことがありますが、C++でこれを理解することはできません。事前コンパイル済みのC++コードを使用してデータを取得しているので、接続にC++を使用する必要があります。私は本当にこれにいくつかの助けに感謝します。おかげ は、私は、ODBCしたいと思いますが、あなたは別の勧告を持っているならば、私は私のmind.Iは、この例に従うことによって、Accessデータベース、Northwindサンプルデータベースに接続しようとしています変更することができ、Visual Studio 2008を使用してC++を使用してMS Accessデータベースに接続
http://msdn.microsoft.com/en-us/library/cc811599.aspx
私はコンパイラとIDEのためのVisual C++ 2008とWindows 7 OSを使用しています。プログラムはコンソールアプリケーションです。この例は、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
任意の助けをいただければ幸いです。
おかげで ダンテ
をxxxxを良い出発点です:http://msdn.microsoft.com/en-us/library /kd4ck1tt(v=vs.80).aspx –
エラーメッセージを繰り返します: "#include" stdafx.h "をソースに追加するのを忘れましたか?"プリコンパイルされたヘッダー '#include"を使用している場合は、stdafx.h "はすべてのコンパイル単位の最初の非コメント行でなければなりません。 – IInspectable