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

📄 数据库dlg.cpp

📁 最新visualC++编程200例书籍源码包括对数据库的操作
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 数据库Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "数据库.h"
#include "数据库Dlg.h"
#include "datadlg.h"

#include "deletedlg.h"
#include "finddlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
	m_thing = _T("");
	m_patient = _T("");
	m_pill = _T("");
	m_doctor = _T("");
	m_money = 0.0;
	m_eatpill = _T("");
couters=0;
zuozhe=0;
	m_givenmoney = 0.0;
	//m_zuozhe=0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
database1="d:\\m.mdb";
/////////////////////////////
m_table=NULL;
rs=	NULL;
m_table2=NULL;
rs2=NULL;

/////////////////////////////////
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Control(pDX, IDC_picture, m_picture);
	DDX_Control(pDX, IDC_BUTTON1, m_getmoney);
	DDX_Control(pDX, IDC_LIST6, m_list2);
	DDX_Control(pDX, IDC_LIST5, m_list1);
	DDX_Control(pDX, IDC_givenmoney, m_givenmoneycontrol);
	DDX_Control(pDX, IDC_doctor, m_doctorcontrol);
	DDX_Control(pDX, IDC_eatpill, m_eatpillcontrol);
	DDX_Control(pDX, IDC_patient, m_patientcontrol);
	DDX_Control(pDX, IDC_thing, m_thingcontrol);
	DDX_Control(pDX, IDC_zuozhe, m_zuozhe);
	DDX_Control(pDX, IDC_money, m_moneys);
	DDX_Control(pDX, IDC_pill, m_pills);
	DDX_Text(pDX, IDC_thing, m_thing);
	DDX_Text(pDX, IDC_patient, m_patient);
	DDX_Text(pDX, IDC_pill, m_pill);
	DDX_Text(pDX, IDC_doctor, m_doctor);
	DDX_Text(pDX, IDC_money, m_money);
	DDX_Text(pDX, IDC_eatpill, m_eatpill);
	DDX_Text(pDX, IDC_givenmoney, m_givenmoney);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_newdef, Onnewdef)
	ON_BN_CLICKED(IDC_opendef, Onopendef)
	ON_BN_CLICKED(IDB_firstrecord, Onfirstrecord)
	ON_BN_CLICKED(IDB_lastrecord, Onlastrecord)
	ON_BN_CLICKED(IDB_nextrecord, Onnextrecord)
	ON_BN_CLICKED(IDB_pre, Onpre)
	ON_BN_CLICKED(IDC_findrecord, Onfindrecord)
	ON_BN_CLICKED(IDC_insetrecord, Oninsetrecord)
	ON_BN_CLICKED(IDC_maderecord, Onmaderecord)
	ON_BN_CLICKED(IDC_printrecord, Onprintrecord)
	ON_BN_CLICKED(IDC_recordcouter, Onrecordcouter)
	ON_BN_CLICKED(IDC_CLOSE, OnClose)
	ON_BN_CLICKED(IDC_deleterecord, Ondeleterecord)
	ON_BN_CLICKED(IDB_firstrecord2, Onfirstrecord2)
	ON_BN_CLICKED(IDB_lastrecord2, Onlastrecord2)
	ON_BN_CLICKED(IDB_nextrecord2, Onnextrecord2)
	ON_BN_CLICKED(IDB_pre2, Onpre2)
	ON_BN_CLICKED(IDC_CLOSE2, OnClose2)
	ON_BN_CLICKED(IDC_deleterecord2, Ondeleterecord2)
	ON_BN_CLICKED(IDC_findrecord2, Onfindrecord2)
	ON_BN_CLICKED(IDC_insetrecord2, Oninsetrecord2)
	ON_BN_CLICKED(IDC_maderecord2, Onmaderecord2)
	ON_BN_CLICKED(IDC_opendef2, Onopendef2)
	ON_BN_CLICKED(IDC_printrecord2, Onprintrecord2)
	ON_BN_CLICKED(IDC_recordcouter2, Onrecordcouter2)
	ON_WM_MOUSEMOVE()
	ON_WM_CTLCOLOR()
	ON_WM_CREATE()
	ON_EN_CHANGE(IDC_pill, OnChangepill)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
GetModuleFileName(NULL,filepath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
filepath.ReleaseBuffer();
int np;
np=filepath.ReverseFind('\\');
filepath=filepath.Left(np);
fileprint=filepath+"\\text.txt";
filepath+="\\数据库.mdb";
UpdateData(false);

/*NOTIFYICONDATA tnid;
tnid.cbSize=sizeof(NOTIFYICONDATA);
tnid.hWnd=this->m_hWnd;
tnid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
tnid.uCallbackMessage=MY_NOTIFYICON;
GetWindowText(tnid.szTip,sizeof(tnid.szTip));
tnid.uID=IDR_MAINFRAME;
HICON hcon;
hcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
tnid.hIcon=hcon;
::Shell_NotifyIcon(NIM_ADD,&tnid);
if(hcon)
::DestroyIcon(hcon);*/	
	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);
		}
	}
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::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 CMyDlg::OnPaint() 
{
	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 CMyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
void CMyDlg::Onnewdef() 
{
	
	try{
		m_db.Create(filepath);
	}
	catch(CDaoException *e)
	{
		e->Delete();
		return;
}
CDaoTableDef m_table(&m_db);
CDaoTableDef m_table2(&m_db);

if(m_db.IsOpen())
{
	
	try
	{
	m_table.Create("药品");
	m_table2.Create("病人");


	}
		catch(CDaoException *e)
	{
		
		e->Delete();
		return;
		}
}
try
{
	m_table.CreateField("药品",dbText,255,dbVariableField);
	m_table2.CreateField("病人",dbText,255,dbVariableField);
	m_table2.CreateField("用药",dbText,255,dbVariableField);
	m_table2.CreateField("费用",dbDouble,dbVariableField);

}
	catch(CDaoException *e)
	{
		e->Delete();
		return;
}
try
{
	m_table2.CreateField("病情",dbText,255,dbVariableField);
}
	catch(CDaoException *e)
	{
		
		e->Delete();
		return;
}
try
{
m_table.CreateField("价格",dbDouble,dbVariableField);

	m_table2.CreateField("医生",dbText,255,dbVariableField);

	m_table2.CreateField("日期",dbDate,dbVariableField);

}
	catch(CDaoException *e)
	{
	
		e->Delete();
		return;
}
try
{
	m_table.Append();
m_table2.Append();

}
	catch(CDaoException *e)
	{
		
		e->Delete();
		return;
}

	m_table.Close();
m_table2.Close();

	m_db.Close();
	
}

void CMyDlg::Onopendef() 
{

try{
	if(!m_db.IsOpen())
	CMyDlg::open();
m_table=new CDaoTableDef(&m_db);

		m_table->Open("药品");
	}
	catch(CDaoException *e)
	{
		e->Delete();
		return;
}

try{
rs=new CDaoRecordset (&m_db);
		rs->Open(m_table,dbOpenDynaset );
	}
	catch(CDaoException *e)
	{
		e->Delete();
		return;
	}
		try{
CWnd* pwnd=GetDlgItem(IDC_opendef);
pwnd->SetWindowText("数据库已打开");
pwnd->ShowWindow(true);

pwnd->ModifyStyle(0,WS_DISABLED);
///////////////////////////
  pwnd=GetDlgItem(IDB_pre);
pwnd->ShowWindow(true);
 pwnd=GetDlgItem(IDB_nextrecord);
pwnd->ShowWindow(true);
 pwnd=GetDlgItem(IDB_firstrecord);
pwnd->ShowWindow(true);
 pwnd=GetDlgItem(IDB_lastrecord);
pwnd->ShowWindow(true);
 pwnd=GetDlgItem(IDC_insetrecord);
pwnd->ShowWindow(true);
 pwnd=GetDlgItem(IDC_maderecord);
pwnd->ShowWindow(false);
 pwnd=GetDlgItem(IDC_recordcouter);
pwnd->ShowWindow(true);
 pwnd=GetDlgItem(IDC_printrecord);
pwnd->ShowWindow(true);
pwnd=GetDlgItem(IDC_findrecord);
pwnd->ShowWindow(true);
pwnd=GetDlgItem(IDC_deleterecord);
pwnd->ShowWindow(true);
pwnd=GetDlgItem(IDC_CLOSE);
pwnd->SetWindowText("关闭数据库");
pwnd->ShowWindow(true);
m_list1.ShowWindow(true);
m_picture.ShowWindow(false);


//m_list1.EnableWindow();
//m_picture.SetWindowPos(this,rect.left,rect.top,rect.right,rect.bottom,SW_SHOW);
/////////////////////////////////SW_HIDE
Invalidate();
m_pills.SetReadOnly(false);
m_moneys.SetReadOnly(false);
rs->MoveFirst();
COleVariant temp;
rs->GetFieldValue(1,temp);
m_money=V_R8(&temp);
CString str;
rs->GetFieldValue(0,temp);
const VARIANT *variant=LPCVARIANT(temp);
if(variant->vt & VT_BYREF)
return;
if(variant->vt==VT_BSTR)
{
	 str=V_BSTRT(&temp);	 
}
m_pill=str;
UpdateData(false);

		}
	catch(CDaoException *e)
	{
		e->Delete();
		return;
}
/////////////////////////////////
CString strs;
try
{

rs->MoveLast();
while(!rs->IsBOF())
{
COleVariant temp;
CString str;
rs->GetFieldValue(0,temp);
const VARIANT *variant=LPCVARIANT(temp);
if(variant->vt & VT_BYREF)
return;
if(variant->vt==VT_BSTR)
{
	 str=V_BSTRT(&temp);	 
}
strs=str;
double d;
rs->GetFieldValue(1,temp);
d=V_R8(&temp);
str.Format("    %.2f元",d);
strs+=str;
m_list1.AddString(strs);

rs->MovePrev();
}

}
catch(CDaoException *e)
	{


		e->Delete();
		return;	
}
///////////////

UpdateData(false);


	///////////////////
}

void CMyDlg::open()
{
	try{
		m_db.Open(filepath);
	}
	catch(CDaoException *e)
	{
		e->Delete();
		return;
}


}

void CMyDlg::Onfirstrecord() 
{
	try{

rs->MoveFirst();

COleVariant temp;

rs->GetFieldValue(1,temp);
m_money=V_R8(&temp);
CString str;
rs->GetFieldValue(0,temp);
const VARIANT *variant=LPCVARIANT(temp);
if(variant->vt & VT_BYREF)
return;
if(variant->vt==VT_BSTR)
{
	 str=V_BSTRT(&temp);	 
}
m_pill=str;
UpdateData(false);
	}
	catch(CDaoException *e)
	{
		e->Delete();
		return;
}
	
}

void CMyDlg::Onlastrecord() 
{
	try{
rs->MoveLast();
COleVariant temp;
rs->GetFieldValue(1,temp);
m_money=V_R8(&temp);
CString str;
rs->GetFieldValue(0,temp);
const VARIANT *variant=LPCVARIANT(temp);
if(variant->vt & VT_BYREF)
return;
if(variant->vt==VT_BSTR)
{
	 str=V_BSTRT(&temp);	 
}
m_pill=str;
UpdateData(false);
	}
		catch(CDaoException *e)
	{
		e->Delete();
		return;
}
}

void CMyDlg::Onnextrecord() 
{
	try{
	if(!rs->IsEOF())
rs->MoveNext();
else
{
rs->MoveFirst();
}
COleVariant temp;
rs->GetFieldValue(1,temp);
m_money=V_R8(&temp);
CString str;
rs->GetFieldValue(0,temp);
const VARIANT *variant=LPCVARIANT(temp);
if(variant->vt & VT_BYREF)
return;
if(variant->vt==VT_BSTR)
{
	 str=V_BSTRT(&temp);	 
}
m_pill=str;
UpdateData(false);
	}
	catch(CDaoException *e)
	{
		e->Delete();
		return;
}
}

void CMyDlg::Onpre() 
{
	try{
if(!rs->IsBOF())
rs->MovePrev();
if(couters==0)
couters=rs->GetAbsolutePosition();

COleVariant temp;
rs->GetFieldValue(1,temp);
m_money=V_R8(&temp);
CString str;
rs->GetFieldValue(0,temp);
const VARIANT *variant=LPCVARIANT(temp);
if(variant->vt & VT_BYREF)
return;
if(variant->vt==VT_BSTR)
{
	 str=V_BSTRT(&temp);	 
}
m_pill=str;

UpdateData(false);
	}
	catch(CDaoException *e)
	{
rs->MoveLast();
		e->Delete();
		return;
}

}

void CMyDlg::Onfindrecord() 
{
Cfinddlg dlg;
dlg.DoModal();	
}

void CMyDlg::Oninsetrecord() 
{
try{
rs->AddNew();
COleVariant temp;
UpdateData();
temp=COleVariant(m_money);
rs->SetFieldValue("价格",temp);
rs->SetFieldValue("药品",(LPCTSTR)m_pill);


UpdateData(false);
}
	catch(CDaoException *e)
	{
		e->Delete();
		return;
}	
try{
rs->Update();
MessageBox("插入成功");

	}
	catch(CDaoException *e)
	{
		e->Delete();
		return;
}	
	m_list1.ResetContent();	
	CMyDlg::Onopendef();
}

void CMyDlg::Onmaderecord() 
{	// TODO: Add your control notification handler code here

}
void CMyDlg::Onprintrecord() 
{

	// TODO: Add your control notification handler code here

CStdioFile f1;
if( !f1.Open( fileprint, CFile::modeCreate
       | CFile::modeWrite | CFile::typeText ) ) {
   #ifdef _DEBUG
      afxDump << "Unable to open file" << "\n";
   #endif
   exit( 1 );
}
CString strs;
strs="                    药品价格一览表 \n";
f1.WriteString(strs);
strs="药品                     价格 \n";
f1.WriteString(strs);


try
{
rs->MoveFirst();
while(!rs->IsEOF())
{
COleVariant temp;
CString str;
rs->GetFieldValue(0,temp);
const VARIANT *variant=LPCVARIANT(temp);
if(variant->vt & VT_BYREF)
return;
if(variant->vt==VT_BSTR)
{
	 str=V_BSTRT(&temp);	 
}
strs=str;
double d;
rs->GetFieldValue(1,temp);
d=V_R8(&temp);
str.Format("                      %.2f元\n",d);
strs+=str;
f1.WriteString(strs);
rs->MoveNext();


}

}
catch(CDaoException *e)
	{
		e->Delete();
		return;	
}

f1.SetLength(f1.GetPosition());
f1.Close();
	CString sr;
	sr="notepad ";
	
sr+=fileprint;
	::WinExec(sr,SW_SHOW);
}

void CMyDlg::Onrecordcouter() 
{
	
	// TODO: Add your control notification handler code here
	try{
	rs->MoveLast();
long cc=rs->GetRecordCount();
CString str;
str.Format("共%d条纪录",cc);
MessageBox(str);
	}
	catch(CDaoException *e)
	{
	MessageBox("没有纪录");
		e->Delete();
	return;	
	}
}

void CMyDlg::OnClose() 
{
	if(rs->IsOpen())

⌨️ 快捷键说明

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