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

📄 recordquery.cpp

📁 我自己用VC编的
💻 CPP
字号:
// RecordQuery.cpp : implementation file
//

#include "stdafx.h"
#include "MyRecord.h"
#include "RecordQuery.h"
#include "RecordFile.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CMyRecordApp theApp;       // 在此引用应用类中的theApp来获取库连接指针
/////////////////////////////////////////////////////////////////////////////
// CRecordQuery dialog


CRecordQuery::CRecordQuery(CWnd* pParent /*=NULL*/)
	: CDialog(CRecordQuery::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRecordQuery)
	m_name = _T("");
	m_time = _T("");
	//}}AFX_DATA_INIT
	Num=0;
}


void CRecordQuery::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRecordQuery)
	DDX_Control(pDX, IDC_LIST1, m_list1);
	DDX_Text(pDX, IDC_NAME, m_name);
	DDX_Text(pDX, IDC_TIME, m_time);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CRecordQuery, CDialog)
	//{{AFX_MSG_MAP(CRecordQuery)
	ON_BN_CLICKED(IDC_QUERY, OnQuery)
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRecordQuery message handlers

BOOL CRecordQuery::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	//------------------------初始化ListCtrl控件----------------------------------//
	//----------------------------------------------------------------------------//
	
	DWORD dwStyle = m_list1.GetExtendedStyle();
	dwStyle |= LVS_EX_GRIDLINES;		//网格线(只适用与report风格的listctrl)
	m_list1.SetExtendedStyle(dwStyle);	//设置扩展风格
	CString Field[4]={"姓名","手术时间","手术项目","备注"};
	for(int j=0;j<4;j++)
	{
		m_list1.InsertColumn(j,Field[j],LVCFMT_LEFT,100);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CRecordQuery::OnQuery() 
{
	// TODO: Add your control notification handler code here
	m_list1.DeleteAllItems();
	
	LV_ITEM lvitem;
	_variant_t var;

	lvitem.pszText="";
	lvitem.mask=LVIF_TEXT;
	lvitem.iSubItem=0;

	CString strname,strtime,stritem,strnote;
	GetDlgItem(IDC_NAME)->GetWindowText(strname);
	GetDlgItem(IDC_TIME)->GetWindowText(strtime);

	//检测用户
	CString strSQL;
	//取得数据
	strSQL.Format("Select * from info where patientname like '%%%s%%' and operationtime like '%%%s%%' ",strname,strtime);
	try
	{
		m_pRecordset.CreateInstance("ADODB.Recordset");	//连接Access数据库
		m_pRecordset->Open((_variant_t)strSQL,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);	//执行SQL语句
		if(m_pRecordset->adoEOF)		//查询数据时,数据的存在的条件
		{
			CString temp="此数据不存在!";
			AfxMessageBox(temp);
		}
		while(!m_pRecordset->adoEOF)
		{
			var=m_pRecordset->GetCollect("patientname");
			if(var.vt!=VT_NULL)
				strname=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("operationtime");
			if(var.vt!=VT_NULL)
				strtime=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("operationitem");
			if(var.vt!=VT_NULL)
				stritem=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("note");
			if(var.vt!=VT_NULL)
				strnote=(LPCSTR)_bstr_t(var);

			//将各个控件的值传递给记录数组
			Num+=1;
			GName1[Num-1]=strname;
			GTime1[Num-1]=strtime;
			GItem1[Num-1]=stritem;
			GNote1[Num-1]=strnote;

			m_list1.DeleteAllItems();

			//重新在列表框中列出所有记录
			for(int i=0;i<Num;i++)
			{
				m_list1.InsertItem(i,GName1[i]);
				m_list1.SetItemText(i,1,GTime1[i]);
				m_list1.SetItemText(i,2,GItem1[i]);
				m_list1.SetItemText(i,3,GNote1[i]);
			}
			m_pRecordset->MoveNext();
		}
		m_pRecordset->Close();
	}
	catch(_com_error e)
	{
		CString temp;
		temp.Format("查询数据出错:%s",e.ErrorMessage());
		AfxMessageBox(temp);
		return;
	}
	m_name = _T("");
	m_time = _T("");
	
	UpdateData(FALSE);
}

void CRecordQuery::OnOpen() 
{
	// TODO: Add your control notification handler code here
	POSITION pos = m_list1.GetFirstSelectedItemPosition();      //得到选定的位置
	int m_nIndex = m_list1.GetNextSelectedItem(pos);            // 得到项目索引
	CRecordFile filedlg;
	filedlg.namestr =m_list1.GetItemText(m_nIndex,0);          //得到第一列所对应的姓名
	filedlg.DoModal();
}

void CRecordQuery::OnDelete() 
{
	// TODO: Add your control notification handler code here
	int m_nIndex;
	POSITION pos = m_list1.GetFirstSelectedItemPosition();	//得到选定的位置
	m_nIndex = m_list1.GetNextSelectedItem(pos);  // 得到项目索引
	CString strName =m_list1.GetItemText(m_nIndex,0);
    
	if(m_nIndex==-1)
	{
		MessageBox("请选择一项再删除!","提示",MB_ICONINFORMATION);
		return;
	}
	
	CString sql;
	
	sql="select * from info where patientname='"+strName+"'";
	try
	{ 
		m_pRecordset.CreateInstance("ADODB.Recordset");
		m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
		
		if(AfxMessageBox("确定删除此联系人信息吗?",MB_YESNO)==IDYES)
		{
			m_pRecordset->Delete(adAffectCurrent);///删除当前记录
		}
		else
			return;				
		m_pRecordset->Update();   
	}
	catch(_com_error e)///捕捉异常
	{
		CString temp;
		temp.Format("删除联系人信息出错:%s",e.ErrorMessage());
		AfxMessageBox(temp);
		return;
	}
	InitListData();

}

void CRecordQuery::InitListData()
{
/*	m_list1.DeleteAllItems();
	int Num=0;
	_variant_t var;

	CString strname,strtime,stritem,strnote;
	//检测用户
	CString strSQL;
	//取得数据
	strSQL.Format("Select * from info where patientname like '%%%s%%' and operationtime like '%%%s%%' ",strname,strtime);
	try
	{
		m_pRecordset.CreateInstance("ADODB.Recordset");	//连接Access数据库
		m_pRecordset->Open((_variant_t)strSQL,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);	//执行SQL语句
		if(m_pRecordset->adoEOF)		//查询数据时,数据的存在的条件
		{
			CString temp="此数据不存在!";
			AfxMessageBox(temp);
		}
		while(!m_pRecordset->adoEOF)
		{
			var=m_pRecordset->GetCollect("patientname");
			if(var.vt!=VT_NULL)
				strname=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("operationtime");
			if(var.vt!=VT_NULL)
				strtime=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("operationitem");
			if(var.vt!=VT_NULL)
				stritem=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("note");
			if(var.vt!=VT_NULL)
				strnote=(LPCSTR)_bstr_t(var);

			//将各个控件的值传递给记录数组
			Num+=1;
			GName1[Num-1]=strname;
			GTime1[Num-1]=strtime;
			GItem1[Num-1]=stritem;
			GNote1[Num-1]=strnote;

			m_list1.DeleteAllItems();

			//重新在列表框中列出所有记录
			for(int i=0;i<Num;i++)
			{
				m_list1.InsertItem(i,GName1[i]);
				m_list1.SetItemText(i,1,GTime1[i]);
				m_list1.SetItemText(i,2,GItem1[i]);
				m_list1.SetItemText(i,3,GNote1[i]);
			}
			m_pRecordset->MoveNext();
		}
		m_pRecordset->Close();
	}
	catch(_com_error e)
	{
		CString temp;
		temp.Format("查询数据出错:%s",e.ErrorMessage());
		AfxMessageBox(temp);
		return;
	}*/
	//删除所有list中的数据。
	m_list1.DeleteAllItems();
}

⌨️ 快捷键说明

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