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

📄 accessview.cpp

📁 ado封装类
💻 CPP
字号:
// AccessView.cpp : implementation of the CAccessView class
//

#include "stdafx.h"
#include "Access.h"
#include "LogoDig.h"

#include "AccessDoc.h"
#include "AccessView.h"
#include "MainFrm.h"
#include "Ado.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAccessView

IMPLEMENT_DYNCREATE(CAccessView, CFormView)

BEGIN_MESSAGE_MAP(CAccessView, CFormView)
	//{{AFX_MSG_MAP(CAccessView)
	ON_WM_SIZE()
	ON_COMMAND(ID_RUN, OnRun)
	ON_WM_CREATE()
	ON_WM_NCPAINT()
	ON_WM_KEYDOWN()
	ON_COMMAND(ID_FILE_CONNECT, OnFileConnect)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAccessView construction/destruction

CAccessView::CAccessView()
	: CFormView(CAccessView::IDD)
{
	//{{AFX_DATA_INIT(CAccessView)
	m_strSQL = _T("select * from ");
	m_strError = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CAccessView::~CAccessView()
{
}

void CAccessView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAccessView)
	DDX_Control(pDX, IDC_ERROR_EDIT, m_editError);
	DDX_Control(pDX, IDC_SQL_EDIT, m_editSQL);
	DDX_Control(pDX, IDC_MSFLEXGRID, m_wndGrid);
	DDX_Text(pDX, IDC_SQL_EDIT, m_strSQL);
	DDX_Text(pDX, IDC_ERROR_EDIT, m_strError);
	//}}AFX_DATA_MAP
}

BOOL CAccessView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CAccessView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	((CMainFrame*)GetParentFrame())->m_wndLeftBar.InitTree();
}

/////////////////////////////////////////////////////////////////////////////
// CAccessView diagnostics

#ifdef _DEBUG
void CAccessView::AssertValid() const
{
	CFormView::AssertValid();
}

void CAccessView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CAccessDoc* CAccessView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAccessDoc)));
	return (CAccessDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAccessView message handlers

void CAccessView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	if (m_wndGrid.GetSafeHwnd() != NULL)
	{
		m_editError.MoveWindow(0, 10, cx, cy - 50);
		m_wndGrid.MoveWindow(0, 0, cx, cy - 40);
		m_editSQL.MoveWindow(0, cy - 40, cx, 40);
	}
	
}


void CAccessView::UpdateGrid()
{
	if (!GetDocument()->m_adoConnection.IsOpen()) 
	{
		AfxMessageBox("数据库没有打开或已经关闭!");
		return;
	}
	m_wndGrid.ShowWindow(SW_HIDE);
	m_editError.ShowWindow(SW_HIDE);
	CAdoRecordSet rset;
	rset.SetAdoConnection(&(GetDocument()->m_adoConnection));

	if (rset.Open(m_strSQL, adCmdText) != 1)
	{
		m_strError = GetDocument()->m_adoConnection.GetLastError();
		UpdateData(FALSE);
		m_editError.ShowWindow(SW_SHOW);
		return;
	}
	
	try
	{
	
		int nrow = rset.GetRecordCount();
		int ncol = rset.GetFields()->Count;
		
		m_wndGrid.SetCols(ncol);
		m_wndGrid.SetRows(nrow + 1);
		m_wndGrid.SetFixedCols(0);
		
		CString value;
		
		m_wndGrid.SetRow(0);
		for (int i = 0; i < ncol; i++)
		{
			m_wndGrid.SetCol(i);
			m_wndGrid.SetText(LPCSTR(rset.GetFieldName(i)));
			int nwidth = rset.GetFieldDefineSize(i) * 200;
			nwidth = nwidth > 2009 ? 2009 : nwidth;
			m_wndGrid.SetColWidth(i, nwidth);
		}
		
		int n = 1;
		while (!rset.IsEOF()) 
		{
			m_wndGrid.SetRow(n);
			n++;
			for (int i = 0; i < ncol; i++)
			{
				m_wndGrid.SetCol(i);
				rset.GetValueString(value, (long)(i));
				m_wndGrid.SetText(LPCTSTR(value));
			}
			rset.MoveNext();
		}
		m_wndGrid.ShowWindow(SW_SHOW);
	}
	catch (_com_error) 
	{
		return;
	}
}

void CAccessView::OnRun() 
{
	UpdateData();
	UpdateGrid();
}

int CAccessView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	
	return 0;
}

void CAccessView::OnNcPaint() 
{
	CWindowDC dc(this);
	CRect rect;
	this->GetWindowRect(&rect);
	this->ScreenToClient(&rect);
	rect.OffsetRect(2,2);
	dc.Draw3dRect(rect, ::GetSysColor(COLOR_3DFACE), ::GetSysColor(COLOR_3DFACE));
	rect.DeflateRect(1,1);
	dc.Draw3dRect(rect, ::GetSysColor(COLOR_3DFACE), ::GetSysColor(COLOR_3DFACE));
}

void CAccessView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if (nChar == VK_F5)
	{
		OnRun();
	}
	CFormView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CAccessView::OnFileConnect() 
{
	CLogoDig dlg;
	if (dlg.DoModal() == IDOK)
	{
		if (dlg.m_nSrcType == 0)
		{
			CString strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dlg.m_strSrcName;
			GetDocument()->m_adoConnection.Disconnect();
			if (!GetDocument()->m_adoConnection.Connect(LPCSTR(strConnect)))
			{
				AfxMessageBox("连接数据库失败!");
				return;
			}
		}
		else if (dlg.m_nSrcType == 1)
		{
			CString strConnect = "Provider=SQLOLEDB.1;Data Source=" + dlg.m_strSrcName + 
							";Initial Catalog=" + dlg.m_strDbName  +
							";User ID=" + dlg.m_strUserName + "; PWD=" + dlg.m_strPassWord;
			GetDocument()->m_adoConnection.Disconnect();
			if (!GetDocument()->m_adoConnection.Connect(LPCSTR(strConnect)))
			{
				AfxMessageBox("连接数据库失败!");
				return;
			}
		}
		((CMainFrame*)GetParentFrame())->m_wndLeftBar.InitTree();
	}
}

⌨️ 快捷键说明

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