利用ADOX、ADO操纵MDB文件(ACCESS)
2014年4月11日17:52:35
1、使用VS2008创建一个预编译头的控制台项目。
2、在stdafx.h文件结尾添加
#include<iostream>
#include <string>
#import "c:\program files\common files\system\ado\msado15.dll" rename(L"EOF", L"EndOfFile")
#import "C:\Program Files\Common Files\system\ado\msadox.dll" rename(L"EOF",L"adoEOF")
using namespace std;
3、源代码内容:
#include "stdafx.h"
using namespace ADODB;
using namespace ADOX;
int _tmain(int argc, _TCHAR* argv[])
{
setlocale( LC_CTYPE, "" );
::CoInitialize(NULL);
_CatalogPtr m_pCatalog = NULL;
_bstr_t strConnect=L"Provider=Microsoft.JET.OLEDB.4.0;Data source= d://test.mdb";
try
{
m_pCatalog.CreateInstance(__uuidof(Catalog));
m_pCatalog->Create(strConnect);
}
catch(_com_error &e)
{
wcout<<e.ErrorMessage()<<endl;
}
char ch;//
cin>>ch;
_ConnectionPtr pConnection=NULL;//连接
_RecordsetPtr pRecordset=NULL;//记录集
pConnection.CreateInstance(__uuidof(Connection));
pRecordset.CreateInstance(__uuidof(Recordset));
try
{
pConnection->Open(strConnect, L"",L"", adModeUnknown);
wcout<<L"打开成功"<<endl;
}catch (_com_error e)
{
wcout<<L"出错"<<endl;
}
cin>>ch;
_bstr_t sSQL=L"create table LWW_TEST (SN int primary key,x int,y int)";
try
{
pConnection->Execute(sSQL,NULL,adCmdText);
wcout<<L"创建表成功"<<endl;
}
catch (_com_error e)
{
wcout<<L"出错"<<endl;
}
cin>>ch;
_bstr_t sSQL1=L"insert into LWW_TEST (SN,x,y) values(1,1,1)";
try
{
pConnection->Execute(sSQL1,NULL,adCmdText);
wcout<<L"插入成功"<<endl;
}
catch (_com_error e)
{
wcout<<L"出错"<<endl;
}
cin>>ch;
if (pRecordset->State)
pRecordset->Close();
if(pConnection->State)
pConnection->Close();
::CoUninitialize();
return 0;
}