?? recordquery.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; // 在此引用應(yīng)用類中的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; //網(wǎng)格線(只適用與report風(fēng)格的listctrl)
m_list1.SetExtendedStyle(dwStyle); //設(shè)置擴(kuò)展風(fēng)格
CString Field[4]={"姓名","手術(shù)時間","手術(shù)項目","備注"};
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;
//取得數(shù)據(jù)
strSQL.Format("Select * from info where patientname like '%%%s%%' and operationtime like '%%%s%%' ",strname,strtime);
try
{
m_pRecordset.CreateInstance("ADODB.Recordset"); //連接Access數(shù)據(jù)庫
m_pRecordset->Open((_variant_t)strSQL,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText); //執(zhí)行SQL語句
if(m_pRecordset->adoEOF) //查詢數(shù)據(jù)時,數(shù)據(jù)的存在的條件
{
CString temp="此數(shù)據(jù)不存在!";
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);
//將各個控件的值傳遞給記錄數(shù)組
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("查詢數(shù)據(jù)出錯:%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); //得到第一列所對應(yīng)的姓名
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("確定刪除此聯(lián)系人信息嗎?",MB_YESNO)==IDYES)
{
m_pRecordset->Delete(adAffectCurrent);///刪除當(dāng)前記錄
}
else
return;
m_pRecordset->Update();
}
catch(_com_error e)///捕捉異常
{
CString temp;
temp.Format("刪除聯(lián)系人信息出錯:%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;
//取得數(shù)據(jù)
strSQL.Format("Select * from info where patientname like '%%%s%%' and operationtime like '%%%s%%' ",strname,strtime);
try
{
m_pRecordset.CreateInstance("ADODB.Recordset"); //連接Access數(shù)據(jù)庫
m_pRecordset->Open((_variant_t)strSQL,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText); //執(zhí)行SQL語句
if(m_pRecordset->adoEOF) //查詢數(shù)據(jù)時,數(shù)據(jù)的存在的條件
{
CString temp="此數(shù)據(jù)不存在!";
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);
//將各個控件的值傳遞給記錄數(shù)組
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("查詢數(shù)據(jù)出錯:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}*/
//刪除所有l(wèi)ist中的數(shù)據(jù)。
m_list1.DeleteAllItems();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -