使用ADO操作MDB,关注数据类型

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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值