www.pudn.com > testAccessData.rar > testDataView.cpp


// testDataView.cpp : implementation of the CTestDataView class 
// 
 
#include "stdafx.h" 
#include "testData.h" 
 
#include "testDataDoc.h" 
#include "testDataView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CTestDataView 
 
IMPLEMENT_DYNCREATE(CTestDataView, CFormView) 
 
BEGIN_MESSAGE_MAP(CTestDataView, CFormView) 
	//{{AFX_MSG_MAP(CTestDataView) 
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1) 
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2) 
	//}}AFX_MSG_MAP 
	// Standard printing commands 
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CTestDataView construction/destruction 
 
CTestDataView::CTestDataView() 
	: CFormView(CTestDataView::IDD) 
{ 
	//{{AFX_DATA_INIT(CTestDataView) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// TODO: add construction code here 
 
} 
 
CTestDataView::~CTestDataView() 
{ 
} 
 
void CTestDataView::DoDataExchange(CDataExchange* pDX) 
{ 
	CFormView::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CTestDataView) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
BOOL CTestDataView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CFormView::PreCreateWindow(cs); 
} 
 
void CTestDataView::OnInitialUpdate() 
{ 
	CFormView::OnInitialUpdate(); 
	GetParentFrame()->RecalcLayout(); 
	ResizeParentToFit(); 
 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CTestDataView printing 
 
BOOL CTestDataView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CTestDataView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CTestDataView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
void CTestDataView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add customized printing code here 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CTestDataView diagnostics 
 
#ifdef _DEBUG 
void CTestDataView::AssertValid() const 
{ 
	CFormView::AssertValid(); 
} 
 
void CTestDataView::Dump(CDumpContext& dc) const 
{ 
	CFormView::Dump(dc); 
} 
 
CTestDataDoc* CTestDataView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDataDoc))); 
	return (CTestDataDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CTestDataView message handlers 
 
void CTestDataView::OnButton1()  
{ 
	//add a record into table 
 
	ADODB::_ConnectionPtr cnChen; 
	ADODB::_RecordsetPtr rsChen; 
 
	COleVariant vValue; 
	CString sConnect; 
	CString sPath; 
 
	//得到当前路径 
	char AppPathName[255]; 
	GetModuleFileName(NULL,AppPathName,255); 
	sPath = AppPathName; 
	if (sPath.Find("Debug")<0&&sPath.Find("DEBUG")<0) 
	{ 
		sPath = sPath.Left(sPath.ReverseFind('\\')+1); 
	} 
	else 
	{ 
		sPath = sPath.Left(sPath.ReverseFind('\\')); 
		sPath = sPath.Left(sPath.ReverseFind('\\')+1); 
	}	 
 
	sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sPath +"chen.mdb;Persist Security Info=False"; 
 
	try 
	{ 
		//连接数据库 
		cnChen=NULL; 
		cnChen.CreateInstance("ADODB.Connection"); 
		cnChen->ConnectionTimeout=60; 
 
		cnChen->Open(_bstr_t(sConnect),"","",NULL); 
 
		//创建数据集 
		rsChen.CreateInstance("ADODB.Recordset");	 
 
		//打开表 
		rsChen->Open((_variant_t)"select * from chen", 
			cnChen.GetInterfacePtr(), 
			ADODB::adOpenDynamic, 
			ADODB::adLockOptimistic, 
			ADODB::adCmdText); 
	 
		//添加一条新纪录 
		rsChen->AddNew(); 
		vValue.vt=VT_BSTR; 
 
		//给字段赋值 
		vValue="value"; 
		rsChen->Fields->Item["chen"]->Value=vValue; 
 
		rsChen->Update(); 
		rsChen->Close(); 
 
		ADODB::_CommandPtr pCmd=NULL; 
		pCmd.CreateInstance("ADODB.Command"); 
		pCmd->ActiveConnection=cnChen; 
		pCmd->CommandType = ADODB::adCmdText; 
 
		//insert a record into table 
		pCmd->CommandText = "insert into chen values ('明白了吗?')"; 
		pCmd->Execute(NULL,NULL,NULL); 
 
 
		cnChen->Close();	 
	} 
	catch(...) 
	{ 
		AfxMessageBox("save"); 
		return; 
	} 
 
 
} 
 
void CTestDataView::OnButton2()  
{ 
	//delete a record from table 
 
	ADODB::_ConnectionPtr cnChen; 
	ADODB::_RecordsetPtr rsChen; 
 
	CString sConnect; 
	CString sPath; 
 
	//得到当前路径 
	char AppPathName[255]; 
	GetModuleFileName(NULL,AppPathName,255); 
	sPath = AppPathName; 
	if (sPath.Find("Debug")<0&&sPath.Find("DEBUG")<0) 
	{ 
		sPath = sPath.Left(sPath.ReverseFind('\\')+1); 
	} 
	else 
	{ 
		sPath = sPath.Left(sPath.ReverseFind('\\')); 
		sPath = sPath.Left(sPath.ReverseFind('\\')+1); 
	}	 
 
	sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sPath +"chen.mdb;Persist Security Info=False"; 
 
	try 
	{ 
		//连接数据库 
		cnChen=NULL; 
		cnChen.CreateInstance("ADODB.Connection"); 
		cnChen->ConnectionTimeout=60; 
 
		cnChen->Open(_bstr_t(sConnect),"","",NULL); 
 
		//创建数据集 
		rsChen.CreateInstance("ADODB.Recordset"); 
		 
		//打开表 
		rsChen->Open((_variant_t)"select * from chen", 
			cnChen.GetInterfacePtr(), 
			ADODB::adOpenDynamic, 
			ADODB::adLockOptimistic, 
			ADODB::adCmdText); 
	 
		if (!(rsChen->adoBOF&&rsChen->adoEOF)) 
		{ 
			rsChen->MoveFirst(); 
			rsChen->Delete(ADODB::adAffectCurrent); 
			rsChen->Update();			 
		} 
		 
		rsChen->Close(); 
 
		ADODB::_CommandPtr pCmd=NULL; 
		pCmd.CreateInstance("ADODB.Command"); 
		pCmd->ActiveConnection=cnChen; 
		pCmd->CommandType = ADODB::adCmdText; 
 
		//sql 
		pCmd->CommandText = "delete from chen where chen='value'"; 
		pCmd->Execute(NULL,NULL,NULL); 
 
 
	} 
	catch(...) 
	{ 
		AfxMessageBox("del"); 
		return; 
	} 
 
}