www.pudn.com > vc应用ADO读取数据库中内容包括图片.rar > StudentAccessDlg.cpp


// StudentAccessDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "StudentAccess.h" 
#include "StudentAccessDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
extern CStudentAccessApp theApp;       // 在此引用应用类中的theApp来获取库连接指针 
///////////////////////////////////////////////////////////////////////////// 
// 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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CStudentAccessDlg dialog 
 
CStudentAccessDlg::CStudentAccessDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CStudentAccessDlg::IDD, pParent) 
{ 
	//初始化变量 
	m_hPhotoBitmap = NULL; 
	m_pBMPBuffer = NULL; 
 
	//{{AFX_DATA_INIT(CStudentAccessDlg) 
	m_nAge = 0; 
	m_strName = _T(""); 
	m_strMajor = _T(""); 
	m_strHome = _T(""); 
	m_strCollege = _T(""); 
	m_nLock = -1; 
	m_nSecret = -1; 
	m_strFileMsg = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CStudentAccessDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CStudentAccessDlg) 
	DDX_Control(pDX, IDC_SECRET, m_comboSecret); 
	DDX_Control(pDX, IDC_LOCK2, m_comboLock); 
	DDX_Control(pDX, IDC_DATETIMEPICKER_DATE, m_dataTimedoc); 
	DDX_Control(pDX, IDC_BUTTON_PARAMETER, m_btnParameter); 
	DDX_Control(pDX, IDC_BUTTONREAD, m_btnRead); 
	DDX_Control(pDX, IDC_BUTTONDELETE, m_btnDelete); 
	DDX_Control(pDX, IDC_BUTTONEDIT, m_btnEdit); 
	DDX_Control(pDX, IDC_BUTTONADD, m_btnAdd); 
	DDX_Control(pDX, IDC_SEX, m_ComboBoxSex); 
	DDX_Control(pDX, IDC_LISTACCESS, m_listAccess); 
	DDX_Text(pDX, IDC_AGE, m_nAge); 
	DDX_Text(pDX, IDC_NAME, m_strName); 
	DDX_Text(pDX, IDC_MAJOR, m_strMajor); 
	DDX_Text(pDX, IDC_HOME, m_strHome); 
	DDX_Text(pDX, IDC_COLLEGE, m_strCollege); 
	DDX_CBIndex(pDX, IDC_LOCK2, m_nLock); 
	DDX_CBIndex(pDX, IDC_SECRET, m_nSecret); 
	DDX_Text(pDX, IDC_FILENAME, m_strFileMsg); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CStudentAccessDlg, CDialog) 
	//{{AFX_MSG_MAP(CStudentAccessDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTONREAD, OnReadAccess) 
	ON_LBN_SELCHANGE(IDC_LISTACCESS, OnSelchangeListaccess) 
	ON_BN_CLICKED(IDC_BUTTONDELETE, OnDelete) 
	ON_BN_CLICKED(IDC_BUTTONEDIT, OnEdit) 
	ON_BN_CLICKED(IDC_BUTTONADD, OnAdd) 
	ON_BN_CLICKED(IDC_BUTTONFILE, OnSelectFile) 
	ON_BN_CLICKED(IDC_BUTTON_PARAMETER, OnEditParameter) 
	ON_WM_CTLCOLOR() 
	ON_BN_CLICKED(IDC_BUTTONHELP, OnHelp) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CStudentAccessDlg message handlers 
 
BOOL CStudentAccessDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	 
	// --------------------------------------------------------------------------------------------------- 
	// 初始化COM,创建ADO连接等操作 
	if(!AfxOleInit()) 
	{ 
		AfxMessageBox("OLE初始化出错!"); 
		return FALSE; 
	} 
 
	m_pConnection.CreateInstance(__uuidof(Connection)); 
	// 使用ADO创建数据库记录集 
	m_pRecordset.CreateInstance(__uuidof(Recordset)); 
	m_pRecordsetPara.CreateInstance(__uuidof(Recordset)); 
 
	GetSystemTime(&m_CurTime);	//获得当前时间 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CStudentAccessDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CStudentAccessDlg::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
	if (IsIconic()) 
	{ 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		//在屏幕上显示图像 
		DrawUserPhoto(170,300,&dc); 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CStudentAccessDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
////////////////////////////////////////////////////////////////////////////// 
//名称:OnReadAccess 
//功能:用ADO从Access文件中读取相应数据并显示出来 
///////////////////////////////////////////////////////////////////////////// 
void CStudentAccessDlg::OnReadAccess()  
{ 
	_variant_t var; 
	CString strName; 
 
	// 清空列表框 
	m_listAccess.ResetContent(); 
	strName=""; 
 
	try 
	{ 
		if(!m_pRecordset->BOF) 
			m_pRecordset->MoveFirst(); 
		else 
		{ 
			AfxMessageBox("表内数据为空"); 
			return; 
		} 
 
		// 读入库中各字段并加入列表框中 
		while(!m_pRecordset->adoEOF) 
		{ 
			var = m_pRecordset->GetCollect("Name"); 
			if(var.vt != VT_NULL) 
				strName = (LPCSTR)_bstr_t(var); 
			 
			m_listAccess.AddString( strName ); 
 
			m_pRecordset->MoveNext(); 
		} 
 
		// 默认列表指向第一项,同时移动记录指针并显示 
		m_listAccess.SetCurSel(0); 
		OnSelchangeListaccess(); 
	} 
	catch(_com_error *e) 
	{ 
		AfxMessageBox(e->ErrorMessage()); 
	} 
 
	//界面修饰 
	this->m_btnAdd.EnableWindow(TRUE); 
	this->m_btnEdit.EnableWindow(TRUE); 
	this->m_btnDelete.EnableWindow(TRUE); 
} 
 
////////////////////////////////////////////////////////////////////////////// 
//名称:OnSelchangeListaccess 
//功能:当每次选择新的记录时,都会移动指针到新的记录位置并将值显示出来 
///////////////////////////////////////////////////////////////////////////// 
void CStudentAccessDlg::OnSelchangeListaccess()  
{ 
	int curSel = m_listAccess.GetCurSel();		 
	_variant_t var,varIndex; 
 
	if(curSel < 0) 
		return; 
 
	try 
	{ 
		DestroyPhoto();	///清除原图像 
 
		// 先将指针移向第一条记录,然后就可以相对第一条记录来随意移动记录指针 
		m_pRecordset->MoveFirst(); 
		m_pRecordset->Move(long(curSel)); 
		 
		var = m_pRecordset->GetCollect("Name"); 
		if(var.vt != VT_NULL) 
			//m_strName = (LPCSTR)_bstr_t(var); 或 
			m_strName = var.bstrVal; 
		else 
			m_strName = "";  
 
		var = m_pRecordset->GetCollect("Sex"); 
		if(var.vt != VT_NULL) 
		{ 
			m_nSex = var.iVal; 
			if( m_nSex == 2 ) 
				m_nSex = -1;	//-1代表未选择 
		} 
		this->m_ComboBoxSex.SetCurSel(m_nSex);		//下拉列表定位 
		 
#ifdef _DEBUG 
		CString temp; 
		temp.Format("m_nSex值为:%d",m_nSex); 
		TRACE(temp); 
#endif 
		 
		var = m_pRecordset->GetCollect("Age"); 
		if(var.vt != VT_NULL) 
			m_nAge = var.iVal; 
		else 
			m_nAge = 0; 
 
		var = m_pRecordset->GetCollect("Home"); 
		if(var.vt != VT_NULL) 
			m_strHome = (LPCSTR)_bstr_t(var);  
		else 
			m_strHome = "";  
 
		var = m_pRecordset->GetCollect("College"); 
		if(var.vt != VT_NULL) 
			m_strCollege = (LPCSTR)_bstr_t(var); 
		else 
			m_strCollege = ""; 
		 
		var = m_pRecordset->GetCollect("Major"); 
		if(var.vt != VT_NULL) 
			m_strMajor = (LPCSTR)_bstr_t(var);  
		else 
			m_strMajor = ""; 
 
		//取出图像数据,复制到内存,并生产BITMAP对象,以便显示 
		long lDataSize = m_pRecordset->GetFields()->GetItem("photo")->ActualSize; 
		if(lDataSize > 0) 
		{ 
			_variant_t			varBLOB; 
			varBLOB = m_pRecordset->GetFields()->GetItem("photo")->GetChunk(lDataSize); 
			if(varBLOB.vt == (VT_ARRAY | VT_UI1)) 
			{ 
				if(m_pBMPBuffer = new char[lDataSize+1])				///重新分配必要的存储空间 
				{	 
					char *pBuf = NULL; 
					SafeArrayAccessData(varBLOB.parray,(void **)&pBuf); 
					memcpy(m_pBMPBuffer,pBuf,lDataSize);				///复制数据到缓冲区m_pBMPBuffer 
					SafeArrayUnaccessData (varBLOB.parray); 
					m_nFileLen = lDataSize; 
					m_hPhotoBitmap = BufferToHBITMAP();					///生成BITMAP对象 
				} 
			} 
		} 
 
		UpdateData(false);    //更新变量到控件显示值 
 
		Invalidate();	//更新视图区,调用OnPaint(); 
	} 
	catch(_com_error *e) 
	{ 
		AfxMessageBox(e->ErrorMessage()); 
	}	 
} 
 
 
/////////////加载BMP文件到内存////////////// 
BOOL CStudentAccessDlg::LoadBMPFile(const char *pBMPPathname) 
{ 
	CFile file; 
	if( !file.Open( pBMPPathname, CFile::modeRead) ) 
		return FALSE; 
	m_nFileLen = file.GetLength(); 
	m_pBMPBuffer = new char[m_nFileLen + 1]; 
	if(!m_pBMPBuffer) 
		return FALSE; 
	if(file.ReadHuge(m_pBMPBuffer,m_nFileLen) != m_nFileLen) 
		return FALSE; 
	return TRUE; 
} 
 
///////////将内存中的BMP文件内容转换到HBITMAP/////// 
HBITMAP CStudentAccessDlg::BufferToHBITMAP() 
{ 
	HBITMAP				hBmp; 
	LPSTR				hDIB,lpBuffer = m_pBMPBuffer; 
	LPVOID				lpDIBBits; 
	BITMAPFILEHEADER	bmfHeader; 
	DWORD				bmfHeaderLen; 
 
	bmfHeaderLen = sizeof(bmfHeader); 
	strncpy((LPSTR)&bmfHeader,(LPSTR)lpBuffer,bmfHeaderLen); 
//	if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B')) return NULL; 
	if (bmfHeader.bfType != (*(WORD*)"BM")) return NULL;//我copy《Windows程序设计》上的做法。 
	hDIB = lpBuffer + bmfHeaderLen; 
	BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)hDIB ; 
	BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ; 
/*	int nColors = bmiHeader.biClrUsed ? bmiHeader.biClrUsed : 1 << bmiHeader.biBitCount;  
	if( bmInfo.bmiHeader.biBitCount > 8 ) 
		lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors + bmInfo.bmiHeader.biClrUsed) +  
			((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0)); 
	else 
		lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors); 
*///原来的代码。 
	lpDIBBits=(lpBuffer)+((BITMAPFILEHEADER *)lpBuffer)->bfOffBits;//这行功能和上面被注释掉的代码相同,容易理解。 
	CClientDC dc(this); 
	hBmp = CreateDIBitmap(dc.m_hDC,&bmiHeader,CBM_INIT,lpDIBBits,&bmInfo,DIB_RGB_COLORS); 
	return hBmp; 
} 
 
/////////////////在屏幕上显示图像/////////////////// 
void CStudentAccessDlg::DrawUserPhoto(int x, int y, CDC *pDC) 
{ 
	if(!m_hPhotoBitmap) return; 
	HBITMAP OldBitmap; 
	CDC MemDC; 
	MemDC.CreateCompatibleDC(pDC); 
	OldBitmap=(HBITMAP)MemDC.SelectObject(m_hPhotoBitmap); 
	pDC->BitBlt(x,y,100,130,&MemDC,0,0,SRCCOPY); 
	MemDC.SelectObject(OldBitmap); 
} 
 
/////////////清除分配的对象///////////// 
void CStudentAccessDlg::DestroyPhoto() 
{ 
	if(m_hPhotoBitmap) 
	{ 
		DeleteObject(m_hPhotoBitmap); 
		m_hPhotoBitmap = NULL; 
	} 
	if(m_pBMPBuffer) 
	{ 
		delete m_pBMPBuffer; 
		m_pBMPBuffer = NULL; 
	} 
} 
 
////////////////////////////////////////////////////////////////////////////// 
//名称:OnDelete 
//功能:删除并更新当前记录 
///////////////////////////////////////////////////////////////////////////// 
void CStudentAccessDlg::OnDelete()  
{ 
	if(m_listAccess.GetCount() == 0) 
		return; 
	else if(m_listAccess.GetCurSel() < 0 || m_listAccess.GetCurSel() > m_listAccess.GetCount()) 
		m_listAccess.SetCurSel(0); 
 
	try 
	{ 
		// 删除当前行记录 
		m_pRecordset->Delete(adAffectCurrent); 
		m_pRecordset->Update(); 
 
		// 删除列表中当前值 
		int nCurSel = m_listAccess.GetCurSel(); 
		m_listAccess.DeleteString(nCurSel); 
		if(nCurSel == 0 && (m_listAccess.GetCount() != 0)) 
			m_listAccess.SetCurSel(nCurSel); 
		else if(m_listAccess.GetCount() != 0) 
			m_listAccess.SetCurSel(nCurSel-1); 
		 
		// 移动记录指针到新的位置 
		OnSelchangeListaccess(); 
	} 
	catch(_com_error *e) 
	{ 
		AfxMessageBox(e->ErrorMessage()); 
	}	 
} 
 
void CStudentAccessDlg::OnEdit()  
{ 
	CEditAdd dlgEditAdd; 
	dlgEditAdd.m_bKind = 2; 
 
	dlgEditAdd.m_strName = this->m_strName; 
	dlgEditAdd.m_nSex = this->m_nSex;		//传递下拉表变量 
	dlgEditAdd.m_nAge = this->m_nAge; 
	dlgEditAdd.m_strHome = this->m_strHome; 
	dlgEditAdd.m_strCollege = this->m_strCollege; 
	dlgEditAdd.m_strMajor = this->m_strMajor; 
	dlgEditAdd.m_hPhotoBitmap = this->m_hPhotoBitmap;		//传递HBITMAP变量 
 
	if(dlgEditAdd.DoModal() == IDOK) 
	{ 
		//dlgEditAdd.UpdateData(TRUE);	本应该更新控件输入到其变量,但对话框的DoModal()将完成此工作 
		if( dlgEditAdd.m_strName == "" || dlgEditAdd.m_nAge == 0) 
		{ 
			AfxMessageBox("姓名、年龄信息不能为空!"); 
			return; 
		} 
 
		// 修改当前记录的字段值 
		try 
		{ 
			_variant_t var;	 
			 
			m_pRecordset->PutCollect( "Name", _variant_t(dlgEditAdd.m_strName) ); 
			 
			if( dlgEditAdd.m_nSex != -1) 
			{ 
				var.iVal = dlgEditAdd.m_nSex;	         //注意int的插入方式 
				m_pRecordset->PutCollect( "Sex", var.iVal ); 
			} 
			 
			var.iVal = dlgEditAdd.m_nAge; 
			m_pRecordset->PutCollect( "Age", var.iVal ); 
			 
			//这里为空为什么会出错? 
			if( dlgEditAdd.m_strHome == "") 
				dlgEditAdd.m_strHome = " "; 
			m_pRecordset->PutCollect( "Home", _variant_t(dlgEditAdd.m_strHome) ); 
			 
			if( dlgEditAdd.m_strCollege == "") 
				dlgEditAdd.m_strCollege = " "; 
			m_pRecordset->PutCollect( "College", _variant_t(dlgEditAdd.m_strCollege) ); 
			 
			if( dlgEditAdd.m_strMajor == "") 
				dlgEditAdd.m_strMajor = " "; 
			m_pRecordset->PutCollect( "Major", _variant_t(dlgEditAdd.m_strMajor) ); 
			 
			//插入bmp数据 
			char			*pBuf = dlgEditAdd.m_pBMPBuffer; 
			VARIANT			varBLOB; 
			SAFEARRAY		*psa; 
			SAFEARRAYBOUND	rgsabound[1]; 
			if(pBuf) 
			{     
				rgsabound[0].lLbound = 0; 
				rgsabound[0].cElements = dlgEditAdd.m_nFileLen; 
				psa = SafeArrayCreate(VT_UI1, 1, rgsabound); 
				for (long i = 0; i < (long)dlgEditAdd.m_nFileLen; i++) 
					SafeArrayPutElement (psa, &i, pBuf++); 
				varBLOB.vt = VT_ARRAY | VT_UI1; 
				varBLOB.parray = psa; 
				m_pRecordset->GetFields()->GetItem("Photo")->AppendChunk(varBLOB); 
			} 
 
			m_pRecordset->Update(); 
			 
			AfxMessageBox("成功更新用户信息!"); 
 
			// 重新读入库记录更新显示 
			int nCurSel = m_listAccess.GetCurSel(); 
			OnReadAccess(); 
			m_listAccess.SetCurSel(nCurSel); 
			// 移动记录指针到新的位置 
			OnSelchangeListaccess(); 
		} 
		catch(_com_error *e) 
		{ 
			AfxMessageBox(e->ErrorMessage()); 
		} 
	} 
 
} 
 
////////////////////////////////////////////////////////////////////////////// 
//OnAdd 
//功能:用ADO来写入插入的字段值 
///////////////////////////////////////////////////////////////////////////// 
void CStudentAccessDlg::OnAdd()  
{ 
	CEditAdd dlgEditAdd; 
	dlgEditAdd.m_bKind = 1; 
 
	if(dlgEditAdd.DoModal() == IDOK) 
	{ 
		//dlgEditAdd.UpdateData(TRUE);	本应该更新控件输入到其变量,但对话框的DoModal()将完成此工作 
		if( dlgEditAdd.m_strName == "" || dlgEditAdd.m_nAge == 0) 
		{ 
			AfxMessageBox("姓名、年龄信息不能为空!"); 
			return; 
		} 
/*		if(dlgEditAdd.m_pBMPBuffer == NULL || dlgEditAdd.m_hPhotoBitmap == NULL || dlgEditAdd.m_strName == "" || dlgEditAdd.m_nAge == 0) 
		{ 
			AfxMessageBox("姓名、年龄、照片信息不能为空!"); 
			return; 
		} 
*/ 
		try 
		{ 
			_variant_t var;	 
 
			// 写入各字段值 
			m_pRecordset->AddNew(); 
 
			m_pRecordset->PutCollect( "Name", _variant_t(dlgEditAdd.m_strName) ); 
			 
			if( dlgEditAdd.m_nSex != 2) 
				var.iVal = dlgEditAdd.m_nSex;	         //注意int的插入方式 
			else 
				var.iVal = 2; 
			m_pRecordset->PutCollect( "Sex", var.iVal ); 
 
			var.iVal = dlgEditAdd.m_nAge; 
			m_pRecordset->PutCollect( "Age", var.iVal ); 
 
			if( dlgEditAdd.m_strHome != "") 
				m_pRecordset->PutCollect( "Home", _variant_t(dlgEditAdd.m_strHome) ); 
 
			if( dlgEditAdd.m_strCollege != "") 
				m_pRecordset->PutCollect( "College", _variant_t(dlgEditAdd.m_strCollege) ); 
 
			if( dlgEditAdd.m_strMajor != "") 
				m_pRecordset->PutCollect( "Major", _variant_t(dlgEditAdd.m_strMajor) ); 
			 
			//插入bmp数据 
			char			*pBuf = dlgEditAdd.m_pBMPBuffer; 
			VARIANT			varBLOB; 
			SAFEARRAY		*psa; 
			SAFEARRAYBOUND	rgsabound[1]; 
			if(pBuf) 
			{     
				rgsabound[0].lLbound = 0; 
				rgsabound[0].cElements = dlgEditAdd.m_nFileLen; 
				psa = SafeArrayCreate(VT_UI1, 1, rgsabound); 
				for (long i = 0; i < (long)dlgEditAdd.m_nFileLen; i++) 
					SafeArrayPutElement (psa, &i, pBuf++); 
				varBLOB.vt = VT_ARRAY | VT_UI1; 
				varBLOB.parray = psa; 
				m_pRecordset->GetFields()->GetItem("Photo")->AppendChunk(varBLOB); 
			} 
 
			m_pRecordset->Update(); 
			 
			AfxMessageBox("插入成功!"); 
			 
			// 更新显示其库内容 
			int nCurSel = m_listAccess.GetCurSel(); 
			OnReadAccess(); 
			m_listAccess.SetCurSel(nCurSel); 
			// 移动记录指针到新的位置 
			OnSelchangeListaccess(); 
		} 
		catch(_com_error *e) 
		{ 
			AfxMessageBox(e->ErrorMessage()); 
		} 
	} 
} 
 
////////////////////////////////////////////////////////////////////////////// 
//名称:DestroyWindow 
//功能:关闭对话框时清除工作 
///////////////////////////////////////////////////////////////////////////// 
BOOL CStudentAccessDlg::DestroyWindow() 
{ 
	// 关闭记录集 
	if(m_pConnection->State) 
		m_pRecordset->Close(); 
	m_pRecordset = NULL; 
	if(m_pConnection->State) 
		m_pRecordsetPara->Close(); 
	m_pRecordsetPara = NULL; 
 
	// 关闭ADO连接状态 
	if(m_pConnection->State) 
		m_pConnection->Close(); 
	m_pConnection= NULL; 
 
	DestroyPhoto(); 
 
	return CDialog::DestroyWindow(); 
} 
 
////////////////////////////////////////////////////////////////////////////// 
//名称:OnSelectFile 
//功能:选择数据库文件,并进行连接,执行select到记录集 
///////////////////////////////////////////////////////////////////////////// 
void CStudentAccessDlg::OnSelectFile()  
{ 
	///////////选择数据库文件///////////// 
	CString strFileName; 
	static char BASED_CODE szFilter[] = "Access Files (*.mdb)|*.mdb||"; 
	CFileDialog dlg(TRUE,"mdb",NULL,  OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter); 
	if(dlg.DoModal() == IDOK) 
	{ 
		strFileName = dlg.GetFileName(); 
	} 
	 
	//链接成功 
	if( this->OpenConnection(strFileName) ) 
	{ 
		this->m_strFileMsg = "成功连接" + strFileName; 
		 
		this->m_btnRead.EnableWindow(TRUE); 
		this->m_btnParameter.EnableWindow(TRUE); 
			 
		SelectRecordset( m_pRecordsetPara, "Parameter" );	//选择Parameter表数据到记录集 
 
		ReadParameter();	// 读取文件参数表parameter并显示在控件中 
 
		SelectRecordset( m_pRecordset, "Student" );		//选择Student表数据到记录集 
	} 
	else 
	{ 
		this->m_strFileMsg = "无法连接" + strFileName; 
	} 
	UpdateData(FALSE);		//更新控件值 
} 
 
bool CStudentAccessDlg::OpenConnection(CString strFileName) 
{ 
	try                  
	{ 
		// 关闭记录集,重新连接 
		if( m_pRecordsetPara->State ) 
			m_pRecordsetPara->Close(); 
		if( m_pRecordset->State ) 
			m_pRecordset->Close(); 
	//	m_pRecordset = NULL;	//不可清空 
 
		if( m_pConnection->State ) 
			m_pConnection->Close(); 
	//	m_pConnection = NULL;	//不可清空 
	} 
	catch(_com_error e) 
	{ 
		AfxMessageBox("数据库连接无法正常关闭!"); 
		return false; 
	}  
 
	CString strProvider; 
	strProvider.Format( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s", strFileName ); 
	try                  
	{	 
		// 打开本地Access库Student.mdb 
		m_pConnection->Open( (_bstr_t)strProvider,"","",adModeUnknown); 
	} 
	catch(_com_error e) 
	{ 
		AfxMessageBox("数据库连接失败,确认" + strFileName +"数据库是否在当前路径下!"); 
		return false; 
	}  
	return true; 
} 
 
void CStudentAccessDlg::SelectRecordset(_RecordsetPtr pRecordset, CString strTableName) 
{ 
	// 关闭记录集 
	if( pRecordset->State ) 
		pRecordset->Close(); 
 
	CString strSql; 
	strSql.Format( "SELECT * FROM %s", strTableName ); 
	 
	try 
	{ 
		pRecordset->Open((_bstr_t)strSql,                // 查询表中所有字段 
			                m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针 
							adOpenDynamic, 
							adLockOptimistic, 
							adCmdText); 
	} 
	catch(_com_error e) 
	{ 
		AfxMessageBox( "确认数据库中是否存在正确格式的数据表" + strTableName ); 
		return; 
	} 
} 
 
// 读取文件参数表parameter,并显示在控件中 
void CStudentAccessDlg::ReadParameter() 
{ 
	try 
	{ 
		if(!m_pRecordsetPara->BOF) 
			m_pRecordsetPara->MoveFirst(); 
		else 
		{ 
			AfxMessageBox("文件参数表为空"); 
			return; 
		} 
		// 读入参数表字段并显示在控件中 
		_variant_t var; 
		while(!m_pRecordsetPara->adoEOF) 
		{ 
			var = m_pRecordsetPara->GetCollect("Lock"); 
			if(var.vt != VT_NULL) 
			{ 
				m_nLock = var.iVal; 
			} 
			this->m_comboLock.SetCurSel(m_nLock);		//下拉列表定位 
			 
			var = m_pRecordsetPara->GetCollect("Secret"); 
			if(var.vt != VT_NULL) 
			{ 
				m_nSecret = var.iVal; 
			} 
			this->m_comboSecret.SetCurSel(m_nSecret);		//下拉列表定位 
			 
			var = m_pRecordsetPara->GetCollect("Timedoc"); 
			CString strTimedoc; 
			if(var.vt != VT_NULL) 
			{ 
				strTimedoc = (LPCSTR)_bstr_t(var); 
			} 
			//struct _SYSTEMTIME 几个属性都得赋值 在初始化中已经赋予当前时间 
			m_CurTime.wYear = atoi(strtok(strTimedoc.GetBuffer(strTimedoc.GetLength()),"-"));	//strtok  
			m_CurTime.wMonth = atoi(strtok(NULL,"-")); 
			m_CurTime.wDay = atoi(strtok(NULL,"-")); 
			m_dataTimedoc.SetTime(&m_CurTime); 
			strTimedoc.ReleaseBuffer(); 
			 
			m_pRecordsetPara->MoveNext(); 
		} 
	} 
	catch(_com_error *e) 
	{ 
		AfxMessageBox(e->ErrorMessage()); 
	} 
} 
 
//更改Parameter参数表 
void CStudentAccessDlg::OnEditParameter()  
{ 
	CSelectFileDlg dlgFilePara; 
 
	dlgFilePara.m_nLock = this->m_nLock;		//传递下拉表变量 
	dlgFilePara.m_nSecret = this->m_nSecret; 
 
	this->m_dataTimedoc.GetWindowText(dlgFilePara.m_strTimedoc); 
 
	if(dlgFilePara.DoModal() == IDOK) 
	{	 
		CString strSql; 
		strSql.Format("UPDATE Parameter SET Lock='%d',Secret='%d',Timedoc='%s' WHERE ID=1", 
					  dlgFilePara.m_nLock,dlgFilePara.m_nSecret,dlgFilePara.m_strTimedoc); 
		 
		this->ExecuteSql(strSql); 
 
		SelectRecordset( m_pRecordsetPara, "Parameter" );	//选择Parameter表数据到记录集 
 
		ReadParameter();	// 读取文件参数表parameter并显示在控件中 
	} 
} 
 
//执行sql语句 
void CStudentAccessDlg::ExecuteSql(CString strSql) 
{ 
	_variant_t vAffected; 
	 
	try 
	{ 
		m_pConnection->Execute(_bstr_t(strSql),&vAffected,adCmdText); 
		AfxMessageBox("成功更新数据库!"); 
	} 
	catch(_com_error e) 
	{ 
		AfxMessageBox("无法执行Sql语句"); 
		return; 
	}  
} 
 
//设置各控件前景、背景色 
HBRUSH CStudentAccessDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  
{ 
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); 
	 
	if(nCtlColor==CTLCOLOR_LISTBOX) 
	{ 
		//pDC->SetBkMode(TRANSPARENT); 
		pDC->SetTextColor(RGB(0,0,0)); 
		pDC->SetBkColor(RGB(233,233,220)); 
		HBRUSH b=CreateSolidBrush(RGB(233,233,220)); 
		return b; 
	} 
	else if(nCtlColor==CTLCOLOR_SCROLLBAR) 
	{ 
		//pDC->SetBkMode(TRANSPARENT); 
		pDC->SetTextColor(RGB(0,0,0)); 
		pDC->SetBkColor(RGB(233,233,220)); 
		HBRUSH b=CreateSolidBrush(RGB(233,233,220)); 
		return b; 
	} 
	else if(nCtlColor==CTLCOLOR_EDIT) 
	{ 
		//pDC->SetBkMode(TRANSPARENT); 
		pDC->SetTextColor(RGB(0,0,0)); 
		pDC->SetBkColor(RGB(233,233,220)); 
		HBRUSH b=CreateSolidBrush(RGB(233,233,220)); 
		return b; 
	} 
	else if(nCtlColor==CTLCOLOR_STATIC) 
	{ 
		pDC->SetTextColor(RGB(0,0,0)); 
		pDC->SetBkColor(RGB(160,180,220)); 
		HBRUSH b=CreateSolidBrush(RGB(160,180,220)); 
		return b; 
	} 
	else if(nCtlColor==CTLCOLOR_DLG) 
	{ 
		pDC->SetTextColor(RGB(0,0,0)); 
		pDC->SetBkColor(RGB(160,180,220)); 
		HBRUSH b=CreateSolidBrush(RGB(160,180,220)); 
		return b; 
	} 
 
	return hbr; 
} 
 
void CStudentAccessDlg::OnHelp()  
{ 
	::ShellExecute(m_hWnd, NULL, "help.htm", NULL, NULL, SW_SHOWNORMAL);	 
}