使用ADO操作MDB,关注数据类型
2014年4月12日10:42:44
源代码内容:
#include "stdafx.h"
#include <stdio.h>
#include <io.h>
using namespace ADODB;
using namespace ADOX;
int _tmain(int argc, _TCHAR* argv[])
{
setlocale( LC_CTYPE, "" );
::CoInitialize(NULL);
char ch;//
if(0 == access("d://test.mdb",0))
{
wcout<<L"文件存在"<<endl;
cin>>ch;
if(remove("d://test.mdb"))
{
wcout<<L"删除失败"<<endl;
}
else
wcout<<L"删除成功"<<endl;
}
else
{
wcout<<L"文件不存在"<<endl;
}
cin>>ch;
_bstr_t strConnect=L"Provider=Microsoft.JET.OLEDB.4.0;Data source= d://test.mdb";
{
_CatalogPtr m_pCatalog = NULL;
try
{
m_pCatalog.CreateInstance(__uuidof(Catalog));
m_pCatalog->Create(strConnect);
wcout<<L"新建库成功"<<endl;
}
catch(_com_error &e)
{
wcout<<e.ErrorMessage()<<endl;
}
}
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,name varchar(40),InTime time)";
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,name,InTime) values(1,1,1,'12345','2014-04-12 10:34:23')";
try
{
pConnection->Execute(sSQL1,NULL,adCmdText);
wcout<<L"插入成功"<<endl;
}
catch (_com_error e)
{
wcout<<L"出错"<<endl;
}
cin>>ch;
_bstr_t sSQL2=L"insert into LWW_TEST (SN,x,y,name,InTime) values(2,2,2,'12345','2014-04-12')";
try
{
pConnection->Execute(sSQL2,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;
}