⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newadodoc.cpp

📁 随着计算机信息技术的飞速发展
💻 CPP
字号:
// NewADODoc.cpp : implementation of the CNewADODoc class
//

#include "stdafx.h"
#include "NewADO.h"

#include "NewADODoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CNewADODoc

IMPLEMENT_DYNCREATE(CNewADODoc, CDocument)

BEGIN_MESSAGE_MAP(CNewADODoc, CDocument)
	//{{AFX_MSG_MAP(CNewADODoc)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNewADODoc construction/destruction

CNewADODoc::CNewADODoc()
{
	// TODO: add one-time construction code here
	m_IsConnectionOpen= FALSE;
	m_sMDBFile="h:\\Northwind.mdb";
}

CNewADODoc::~CNewADODoc()
{
}

BOOL CNewADODoc::OnNewDocument()
{
	//if (!CDocument::OnNewDocument())
	//	return FALSE;
	//H:\\My VCProgram\\2003.11.1\\My-Ado\\My-Ado\\database.mdb
	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	OpenConnection();
	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CNewADODoc serialization

void CNewADODoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CNewADODoc diagnostics

#ifdef _DEBUG
void CNewADODoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CNewADODoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CNewADODoc commands

void CNewADODoc::OnCloseDocument() 
{
	if(m_IsConnectionOpen){
		m_IsConnectionOpen = FALSE;
		m_pConnection->Close();
	}

	CDocument::OnCloseDocument();
}

/*
BOOL CNewADODoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	//CNewADODoc::OnCloseDocument();
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	if( -1!=m_sMDBFile.Compare(lpszPathName)){
		m_sMDBFile=lpszPathName;
		AfxMessageBox( m_sMDBFile );
	}
	return TRUE;
}
*/

BOOL CNewADODoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	// TODO: Add your specialized creation code here
	if( 0 != m_sMDBFile.Compare(lpszPathName)){
		if(m_IsConnectionOpen){
			m_IsConnectionOpen = FALSE;
			m_pConnection->Close();
		}
		m_sMDBFile=lpszPathName;
	}

	return OpenConnection();	
}

BOOL CNewADODoc::OpenConnection()
{
	HRESULT hr;
	
	try
	{
		hr = m_pConnection.CreateInstance( __uuidof( Connection ) );
		if (SUCCEEDED(hr))
		{
			CString sCon="Provider=Microsoft.Jet.OLEDB.4.0;\
				Data Source="+m_sMDBFile+';';
			hr = m_pConnection->Open(
				_bstr_t(sCon),\
				_bstr_t(L""),\
				_bstr_t(L""),\
				adModeUnknown);
			if (SUCCEEDED(hr))
			{
				m_IsConnectionOpen = TRUE;
				//OnShowTable();
				return TRUE;
			}
		}
	}
	catch( _com_error &e )
	{
		// Get info from _com_error
		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());
		TRACE( "Exception thrown for classes generated by #import" );
		TRACE( "\tCode = %08lx\n", e.Error());
		TRACE( "\tCode meaning = %s\n", e.ErrorMessage());
		TRACE( "\tSource = %s\n", (LPCTSTR) bstrSource);
		TRACE( "\tDescription = %s\n", (LPCTSTR) bstrDescription);
		return FALSE;
	}
	catch(...)
	{
		TRACE( "*** Unhandled Exception ***" );
		return FALSE;
	}
}
/*
void CNewADODoc::OnShowTable() 
{
	m_SelDlg.m_IsConnectionOpen = m_IsConnectionOpen;
	m_SelDlg.m_pConnection = m_pConnection;
	SelectTable();
	if(m_SelDlg.DoModal()==IDOK&&!m_SelDlg.m_sTablename.IsEmpty()){
		m_sTablename=m_SelDlg.m_sTablename;
		SelectTable();
	}
}

void CNewADODoc::SelectTable()
{
	if(m_IsConnectionOpen){

		try
		{
			_RecordsetPtr pRstSchema;
			pRstSchema = m_pConnection->OpenSchema(adSchemaTables);
			
			while(!pRstSchema->adoEOF)
			{
				_bstr_t table_name = pRstSchema->Fields->GetItem("TABLE_NAME")->Value;
				
				m_SelDlg.m_ListBox.InsertString(-1,(LPCSTR)table_name);
				//printf("Table Name: %s\n",(LPCSTR) table_name);
				
				pRstSchema->MoveNext();
				
			}
			pRstSchema->Close();
		}
		catch (_com_error &e)
		{
			// Notify the user of errors if any.
			// Pass a connection pointer accessed from the Connection.        
			
			ErrorPtr    pErr  = NULL;
			
			if( (m_pConnection->Errors->Count) > 0)
			{
				long nCount = m_pConnection->Errors->Count;
				// Collection ranges from 0 to nCount -1.
				for(long i = 0;i < nCount;i++)
				{
					pErr = m_pConnection->Errors->GetItem(i);
					TRACE("\t Error number: %x\t%s", pErr->Number,\
						pErr->Description);
				}
			}
		}
		//m_m_pConnection->OpenSchema(adSchemaColumns), vtMissing, vtMissing);
		//_variant_t value = m_pRecordset->GetCollect(_variant_t(LPCTSTR(strFieldName)));
	}
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -