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

📄 adodlg.cpp

📁 ADO数据库编程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		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
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAdoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAdoDlg::OnGridClick(NMHDR* pNMHDR, LRESULT* pResult)
{//处理由CCustomGrid对象发送的鼠标单击事件
	NMHDR32 *nmgv = (NMHDR32 *)pNMHDR;// NMHDR32 在"CellEdit.h"文件中定义

	if(!m_nModified){
		//当前为非修改模式(允许用户选择行)
		//nmgv->nCol 为栅格列标号
		//nmgv->nRow 为栅格行编号
		//nmgv->strText 为栅格内容
		m_nSelectedRow = nmgv->nRow;//记录用户选择的行
	}
}

void CAdoDlg::OnGridDBClick(NMHDR* pNMHDR, LRESULT* pResult)
{//处理由CCustomGrid对象发送的鼠标双击事件
	NMHDR32 *nmgv = (NMHDR32 *)pNMHDR;// NMHDR32 在"CellEdit.h"文件中定义

	if(!m_Grid.IsEditable()){
		//显示栅格内容,可多行显示
		AfxMessageBox(nmgv->strText);
	}
}

void CAdoDlg::OnDatabase() 
{
	// TODO: Add your control notification handler code here
	CString Database, FileType;
	CDialogDBType dlgtype;
	if(dlgtype.DoModal() != IDOK || dlgtype.m_nDataType < 0){
		return;
	}
	switch(dlgtype.m_nDataType){
	case 0://Access *.mdb
		Database = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=";
		FileType = "Microsoft Access (*.mdb)|*.mdb||";
		break;
	case 1://VFP *.dbc
		Database = "DRIVER={Microsoft Visual FoxPro Driver};SourceType=DBC;SourceDB=";
		FileType = "Visual FoxPro 数据库 (*.dbc)|*.dbc||";
		break;
	case 2://VFP *.dbf
		Database = "DRIVER={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=";
		FileType = "Visual FoxPro 自由表 (*.dbf)|*.dbf||";
		break;
	case 3://Dbase *.dbf
		Database = "DRIVER=Microsoft dBase Driver (*.dbf);UID=admin;FIL=dBase 5.0;DBQ=";
		FileType = "DataBase 表 (*.dbf)|*.dbf||";
		break;
	}
	CFileDialog dlg(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, FileType);
	if(dlg.DoModal() != IDOK)
	{//
		return;
	}
	//DatabaseName += dlg.GetPathName();
	switch(dlgtype.m_nDataType){
	case 0:
	case 1:
		FileType = dlg.GetPathName();
		Database += FileType;
		break;
	case 2:
	case 3:
		FileType = dlg.GetPathName().Left(dlg.GetPathName().GetLength() - dlg.GetFileName().GetLength());
		Database += FileType;
		break;
	}

	//CString DatabaseName = "DRIVER={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=d:\\VC\\Poem";
	CloseDatabase();
	try{
		m_piConnection->ConnectionString = _bstr_t(Database);
        m_piConnection->Open( "", "", "", -1 );
		m_bDataOpened = true;
		//m_strDatabaseName = dlg.GetFileName();
	}
	catch( _com_error &e){
		_bstr_t bstrSource(e.Source());
		_bstr_t bs =  _bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
			+ _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
			+ _bstr_t(e.Description());
		m_bDataOpened = false;
		MessageBox(bs, bstrSource);
		return;
	}
	//设置数据库名称显示
	FileType = "当前数据库:" + FileType;
	GetDlgItem(IDC_STATIC1)->SetWindowText(FileType);
	GetTablesName();//显示Table列表
}

void CAdoDlg::OnSelchangeTablelist() 
{
	// TODO: Add your control notification handler code here
	long lFld, i;
	CComboBox* list = (CComboBox*)GetDlgItem(IDC_TABLELIST);
	CString strSelected;
	CString cName;
	COleVariant	vNull, vValue, vValue1;

	list->GetLBText(list->GetCurSel(), strSelected);

	strSelected = "select * from " + strSelected;
	try{
		CloseTable();
		m_piRecordset->putref_ActiveConnection(m_piConnection);//设置连接对象
		m_piRecordset->Open(_bstr_t(strSelected), vtMissing, adOpenDynamic, adLockOptimistic, adCmdText); //打开纪录集
		m_bTableOpened = true;
		m_nFieldCount = m_piRecordset->Fields->GetCount();//获得纪录集总列数
		m_pFieldsType = new UINT[m_nFieldCount];//申请数组内存空间
		memset(m_pFieldsType, 0, m_nFieldCount * sizeof(UINT));
		for (lFld = 0; lFld < m_nFieldCount; lFld++){
			m_pFieldsType[lFld] = m_piRecordset->Fields->Item[lFld]->Type;
			ChangeVariantType(m_pFieldsType[lFld]);
		}
	}
	catch( _com_error &e){
		m_bTableOpened = false;
		_bstr_t bstrSource(e.Source());
		_bstr_t bs =  _bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
			+ _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
			+ _bstr_t(e.Description());
		MessageBox(bs, bstrSource);
		return;
	}
	m_nSelectedRow = -1;
	m_bEditable = true;

	m_Grid.SetColumnCount(m_nFieldCount+1);//设置列
	m_Grid.SetRowCount(1);//设置行

	for(lFld = 0; lFld < m_nFieldCount; lFld++)
	{//显示字段名
		cName = (PCSTR)m_piRecordset->Fields->Item[lFld]->Name;//字段名
		m_Grid.SetItemString(0, lFld+1, cName);
	}
	while(!m_piRecordset->EndOfFile) 
	{//显示数据
		i = m_Grid.GetRowCount();
		cName.Format("记录%i:", i);
		m_Grid.AppendRow();
		m_Grid.SetItemString(i, 0, cName);
		for (lFld = 0; lFld < m_nFieldCount; lFld++){
			vValue = RsITEM(m_piRecordset, lFld);//当前记录字段数据(可用MoveNext等命令移动)
			if(vValue.vt != 1)
			{//数据非空
				int type = vValue.vt;
				if(	VariantChangeType(&vValue1, &vValue, 0, VT_BSTR) == S_OK){//转换数据类型(为字符串)	
					cName = vValue1.bstrVal;
					m_Grid.SetItemString(i, lFld+1, cName);
				}
			}
		}
		m_piRecordset->MoveNext();
	}
}

void CAdoDlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	long i;
	COleVariant vValue, vValue1;

	if(m_nModified)
	{//当前状态为“确定”
		if(m_nModified == 1)
			m_piRecordset->AddNew();
		for(i=0; i< m_nFieldCount; i++){
			vValue = m_Grid.GetItemString(m_nSelectedRow, i+1);
			if( m_pFieldsType[i] == VT_BSTR && strlen((char*)vValue.bstrVal)>0 )
				m_piRecordset->Fields->Item[i]->Value = vValue;
			else if( m_pFieldsType[i] != VT_BSTR && VariantChangeType(&vValue1, &vValue, 0, m_pFieldsType[i]) == S_OK )
				m_piRecordset->Fields->Item[i]->Value = vValue1;
		}
		try{
			m_piRecordset->Update();
		}
		catch( _com_error &e){
			m_piRecordset->CancelUpdate();
			_bstr_t bstrSource(e.Source());
			_bstr_t bs =  _bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
				+ _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
				+ _bstr_t(e.Description());
			MessageBox(bs, bstrSource);
		}
		m_Grid.SetEditable(false);
		m_nModified = 0;
		SetButtonCtrl();
		m_nSelectedRow = -1;
	}
	else
	{//按钮当前状态为“增加”
		if(!m_bEditable){
			AfxMessageBox("必须首先选择数据表");
			return;
		}
		m_Grid.SetEditable(true);
		m_nSelectedRow = m_Grid.GetRowCount();
		CString name;
		name.Format("记录%d:", m_nSelectedRow);
		m_Grid.AppendRow();
		m_Grid.SetItemString(m_nSelectedRow, 0, name);
		//显示正在修改的行
		m_Grid.VisibleCell(m_nSelectedRow, 0);
		m_nModified = 1;
		SetButtonCtrl();
	}
}

void CAdoDlg::OnModify() 
{
	// TODO: Add your control notification handler code here
	if(m_nModified)
	{//按钮当前状态为“取消”
		m_Grid.SetEditable(false);
		m_nModified = 0;
		SetButtonCtrl();
		OnSelchangeTablelist();
		m_nSelectedRow = -1;
	}
	else
	{//按钮当前状态为“修改”
		if(!m_bEditable){
			AfxMessageBox("必须首先选择数据表");
			return;
		}
		if(m_nSelectedRow>0){
			m_piRecordset->MoveFirst();
			m_piRecordset->Move(m_nSelectedRow-1);
			m_Grid.SetEditable(true);
			m_nModified = 2;
			SetButtonCtrl();
		}
		else{
			AfxMessageBox("必须先选择一条记录,才能修改");
		}
	}
}

void CAdoDlg::OnDelete() 
{
	// TODO: Add your control notification handler code here
	if(!m_bEditable){
		AfxMessageBox("必须首先选择数据表");
		return;
	}
	if(m_nSelectedRow>0){
		m_piRecordset->MoveFirst();
		m_piRecordset->Move(m_nSelectedRow-1);
			//m_nSelectedRow变量在OnSelChanged函数中设置
		m_piRecordset->Delete(adAffectCurrent);//删除当前记录
		OnSelchangeTablelist();//更新列表显示
		m_nSelectedRow = -1;
	}
	else{
		AfxMessageBox("必须先选择一条记录,才能删除");
	}
}

⌨️ 快捷键说明

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