利用ADOX、ADO操纵MDB文件(ACCESS)

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




vc++利用ADOX创建数据库 // ADOXCreateDatabaseDlg.cpp : implementation file // #include "stdafx.h" #include "ADOXCreateDatabase.h" #include "ADOXCreateDatabaseDlg.h" #include "Shlwapi.h" #pragma comment(lib,"shlwapi.lib") // Download by http://www.codefans.net #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CADOXCreateDatabaseDlg dialog CADOXCreateDatabaseDlg::CADOXCreateDatabaseDlg(CWnd* pParent /*=NULL*/) : CDialog(CADOXCreateDatabaseDlg::IDD, pParent) { //{{AFX_DATA_INIT(CADOXCreateDatabaseDlg) m_dbName = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CADOXCreateDatabaseDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CADOXCreateDatabaseDlg) DDX_Text(pDX, IDC_DBNAME, m_dbName); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CADOXCreateDatabaseDlg, CDialog) //{{AFX_MSG_MAP(CADOXCreateDatabaseDlg) ON_WM_SYSCOMM
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值