这是一款用MFC ADO编写的数据库软件

源代码在线查看: 数据库adodlg.cpp

软件大小: 4278 K
上传用户: ydyzd_2008
关键词: MFC ADO 编写 数据库软件
下载地址: 免注册下载 普通下载 VIP

相关代码

				// 数据库ADODlg.cpp : implementation file
				//
				
				#include "stdafx.h"
				#include "数据库ADO.h"
				#include "数据库ADODlg.h"
				#include "DlgProxy.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()
				
				/////////////////////////////////////////////////////////////////////////////
				// CADODlg dialog
				
				IMPLEMENT_DYNAMIC(CADODlg, CDialog);
				
				CADODlg::CADODlg(CWnd* pParent /*=NULL*/)
					: CDialog(CADODlg::IDD, pParent)
				{
					//{{AFX_DATA_INIT(CADODlg)
					//}}AFX_DATA_INIT
					// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
					m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
					m_pAutoProxy = NULL;
					catcherror=false;
					
				}
				
				CADODlg::~CADODlg()
				{
					// If there is an automation proxy for this dialog, set
					//  its back pointer to this dialog to NULL, so it knows
					//  the dialog has been deleted.
					if (m_pAutoProxy != NULL)
						m_pAutoProxy->m_pDialog = NULL;
				}
				
				void CADODlg::DoDataExchange(CDataExchange* pDX)
				{
					CDialog::DoDataExchange(pDX);
					//{{AFX_DATA_MAP(CADODlg)
					DDX_Control(pDX, IDC_LIST1, m_basic);
					//DDX_Radio(pDX, IDC_RADIO1, m_radio);
					//}}AFX_DATA_MAP
				}
				
				BEGIN_MESSAGE_MAP(CADODlg, CDialog)
					//{{AFX_MSG_MAP(CADODlg)
					ON_WM_SYSCOMMAND()
					ON_WM_PAINT()
					ON_WM_QUERYDRAGICON()
					ON_WM_CLOSE()
					ON_BN_CLICKED(IDC_BUTTON_FIND, OnButtonFind)
					ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
					ON_BN_CLICKED(IDC_BUTTON_ORDER, OnButtonOrder)
					ON_BN_CLICKED(IDC_BUTTON_DELET, OnButtonDelet)
					ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
					ON_WM_DESTROY()
					ON_BN_CLICKED(IDC_BUTTON_SHOWALL, OnButtonShowall)
					ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
					//}}AFX_MSG_MAP
				END_MESSAGE_MAP()
				
				/////////////////////////////////////////////////////////////////////////////
				// CADODlg message handlers
				
				BOOL CADODlg::OnInitDialog()
				{
					 CDialog::OnInitDialog();
					 m_DBCnt= theApp.m_pConnection;
					 MySet.CreateInstance (_uuidof(Recordset));
					 strSQL="SELECT *FROM score";
				  	try
					{
						MySet->Open(strSQL,theApp.m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); 	
					}
					catch(_com_error e) 
					{
						MessageBox(e.ErrorMessage());
					} 
				     m_basic.InsertColumn(0,"编号");
				     m_basic.InsertColumn(1,"学号");
					 m_basic.InsertColumn(2,"姓名");
					 m_basic.InsertColumn(3,"性别");
					 m_basic.InsertColumn(4,"专业");
					 m_basic.InsertColumn(5,"语文");
					 m_basic.InsertColumn(6,"数学");
					 m_basic.InsertColumn(7,"物理");
					 m_basic.InsertColumn(8,"英语");
					 
					 CRect rectbasic;
					 m_basic.GetWindowRect(&rectbasic);
					 int widbasic=rectbasic.right -rectbasic.left ;
					 m_basic.SetColumnWidth(0,0);
					 m_basic.SetColumnWidth(1,widbasic/4);
					 m_basic.SetColumnWidth(2,widbasic/8);
					 m_basic.SetColumnWidth(3,widbasic/8);
					 m_basic.SetColumnWidth(4,widbasic/8);
					 m_basic.SetColumnWidth(5,widbasic/8);
				     m_basic.SetColumnWidth(6,widbasic/8);
					 m_basic.SetColumnWidth(7,widbasic/8);
					 m_basic.SetColumnWidth(8,widbasic/8);
					 m_basic.SetExtendedStyle(LVS_EX_FULLROWSELECT);
					 Refresh();
					 
					return TRUE;  // return TRUE  unless you set the focus to a control
				}
				
				void CADODlg::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 CADODlg::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 CADODlg::OnQueryDragIcon()
				{
					return (HCURSOR) m_hIcon;
				}
				
				// Automation servers should not exit when a user closes the UI
				//  if a controller still holds on to one of its objects.  These
				//  message handlers make sure that if the proxy is still in use,
				//  then the UI is hidden but the dialog remains around if it
				//  is dismissed.
				
				void CADODlg::OnClose() 
				{
					if (CanExit())
						CDialog::OnClose();
				}
				
				void CADODlg::OnOK() 
				{
					if (CanExit())
						CDialog::OnOK();
				}
				
				void CADODlg::OnCancel() 
				{
					if (CanExit())
						CDialog::OnCancel();
				}
				
				BOOL CADODlg::CanExit()
				{
					// If the proxy object is still around, then the automation
					//  controller is still holding on to this application.  Leave
					//  the dialog around, but hide its UI.
					if (m_pAutoProxy != NULL)
					{
						ShowWindow(SW_HIDE);
						return FALSE;
					}
				
					return TRUE;
				}
				
				void CADODlg::Refresh()
				{
				//	if(!OnIniting)
				//	{
				//	try
				// 	{	
				//	MySet=m_DBCnt->Execute (strSQL,NULL,adCmdText);
				//	}
				//	catch(_com_error e)
				//	{
				//		catcherror=true;
				//		MessageBox(e.ErrorMessage());
				//	    m_basic.SetRedraw (TRUE);
				//		if(MySet->adoEOF)
				//		{
				//			strSQL="SELECT *FROM score";
				//			MySet=m_DBCnt->Execute (strSQL,NULL,adCmdText);
				//		}
				//	
				//	}
				//
				//	}
					MySet->MoveFirst ();
					m_basic.DeleteAllItems ();
					m_basic.SetRedraw (FALSE);
					int i=0;
					_variant_t Holder;
					while(!MySet->adoEOF)
					{
						Holder=MySet->GetCollect("ID");
						if(Holder.vt!=VT_NULL)
						m_basic.InsertItem(i,(char*)(_bstr_t)Holder);
						
				       	Holder=MySet->GetCollect("stuID");		
						if(Holder.vt!=VT_NULL)
						m_basic.SetItemText(i,1,(char*)(_bstr_t)Holder);
						
						Holder=MySet->GetCollect("name");		
						if(Holder.vt!=VT_NULL)
						m_basic.SetItemText(i,2,(char*)(_bstr_t)Holder);
						
						Holder=MySet->GetCollect("sex");		
						if(Holder.vt!=VT_NULL)
						m_basic.SetItemText(i,3,(char*)(_bstr_t)Holder);
				
					    Holder=MySet->GetCollect("subject");		
						if(Holder.vt!=VT_NULL)
						m_basic.SetItemText(i,4,(char*)(_bstr_t)Holder);
						
						Holder=MySet->GetCollect("chinese");		
						if(Holder.vt!=VT_NULL)
						m_basic.SetItemText(i,5,(char*)(_bstr_t)Holder);
				
						Holder=MySet->GetCollect("math");		
						if(Holder.vt!=VT_NULL)
						m_basic.SetItemText(i,6,(char*)(_bstr_t)Holder);
				
						Holder=MySet->GetCollect("physics");		
						if(Holder.vt!=VT_NULL)
						m_basic.SetItemText(i,7,(char*)(_bstr_t)Holder);
				
						Holder=MySet->GetCollect("english");		
						if(Holder.vt!=VT_NULL)
						m_basic.SetItemText(i,8,(char*)(_bstr_t)Holder);
						MySet->MoveNext ();
						i++;
					}
					m_basic.SetRedraw (TRUE);
				
				}
				
				void CADODlg::OnButtonAdd() 
				{
					CString sql;
					CAddDialog *AddDialog=new CAddDialog;
					if(AddDialog->DoModal()==IDOK)
					{		 
						sql.Format ("INSERT INTO score (stuID,name,sex,subject,chinese,math,physics,english)VALUES(%d,'%s','%s','%s',%d,%d,%d,%d)",AddDialog->m_id,AddDialog->m_name 
							,AddDialog->m_sex ,AddDialog->m_subject ,AddDialog->m_chinese ,AddDialog->m_math ,AddDialog->m_physics ,AddDialog->m_english );
						strSQL=sql;
						try
						{
							m_DBCnt->Execute (strSQL,NULL,adCmdText);
						}
						catch(_com_error&e)
						{
							AfxMessageBox(e.ErrorMessage());
							return;
							
						}
						
						strSQL="SELECT *FROM score";
						try
						{
						 MySet= m_DBCnt->Execute (strSQL,NULL,adCmdText);
						}
						catch(_com_error&e)
						{
							AfxMessageBox(e.ErrorMessage());
							return;
							
						}
				   	   Refresh();
					}
				}
				
				void CADODlg::OnButtonFind() 
				{
					CFindDialog *FindDialog=new CFindDialog;
					if(FindDialog->DoModal()==IDOK)
					{
						
						CString str=FindDialog->FindStr();
					     strSQL  =str;
						
					try
				 	{	
					MySet=m_DBCnt->Execute (strSQL,NULL,adCmdText);
					if(MySet->adoEOF)
					{
						MessageBox("找不到所需数据");
						strSQL="SELECT *FROM score";
						MySet=m_DBCnt->Execute (strSQL,NULL,adCmdText);
					}
					}
					catch(_com_error e)
					{
						MessageBox(e.ErrorMessage());
					
					}			
						Refresh();
					}
				}
				
				
				void CADODlg::OnButtonOrder() 
				{
					COrderDialog *OrderDialog=new COrderDialog;
					if(OrderDialog->DoModal ()==IDOK)
					{
						CString str =OrderDialog->orderstr();
						CString sql="SELECT *FROM score ORDER BY "+str;
						strSQL=sql;
						try
						{
							MySet=m_DBCnt->Execute (strSQL,NULL,adCmdText);
						}
						catch(_com_error&e)
						{
							AfxMessageBox(e.ErrorMessage());
							return;
						}
						Refresh();	
					}	
				}
				
				void CADODlg::OnButtonDelet() 
				{
					CString sql;	
					CString str="是否要删除学号为 ‘"+SelectID+"’  的全部信息";	
					if(SelectID=="")
						MessageBox("请先选择需要删除的对象");
					else
					{
						if(MessageBox(str,NULL,MB_OKCANCEL)==IDOK)
						{
							long n=atoi(SelectID);
							sql.Format("DELETE FROM score WHERE stuID=%d",n);
							strSQL=sql;
							try
							{
								MySet=m_DBCnt->Execute (strSQL,NULL,adCmdText);
							}
							catch(_com_error&e)
							{
								AfxMessageBox(e.ErrorMessage());
								return;
							}
							strSQL="SELECT *FROM score";
								try
							{
								MySet=m_DBCnt->Execute (strSQL,NULL,adCmdText);
							}
							catch(_com_error&e)
							{
								AfxMessageBox(e.ErrorMessage());
								return;
							}
				
						Refresh();
						}
						
					}	
				}
				
				void CADODlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) 
				{
				
					NMLISTVIEW *pLV=(NMLISTVIEW*) pNMHDR;
					if(pLV->iItem 					SelectID=m_basic.GetItemText(pLV->iItem,1);
					*pResult = 0;	
				}
				
				void CADODlg::OnDestroy() 
				{
				if(m_DBCnt->State)
				MySet->Close();
				MySet.Release ();
				if(m_DBCnt->State);
				theApp.m_pConnection  ->Close ();
				theApp.m_pConnection.Release ();
				::CoUninitialize();
				CDialog::OnDestroy();	
				}
				
				void CADODlg::OnButtonShowall() 
				{
					strSQL="SELECT *FROM score";
					MySet=m_DBCnt->Execute(strSQL,NULL,adCmdText);
					Refresh();
					
				}
				
				void CADODlg::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) 
				{
					NMLISTVIEW *pLV=(NMLISTVIEW*) pNMHDR;
					if(pLV->iItem 					CAddDialog *showdialog=new CAddDialog;
					showdialog->m_id=atoi(m_basic.GetItemText(pLV->iItem,1));
					showdialog->m_name=m_basic.GetItemText(pLV->iItem ,2);
					showdialog->m_sex=m_basic.GetItemText(pLV->iItem ,3);
					showdialog->m_subject=m_basic.GetItemText(pLV->iItem ,4);
					showdialog->m_chinese=atoi(m_basic.GetItemText(pLV->iItem ,5));
					showdialog->m_math=atoi(m_basic.GetItemText(pLV->iItem ,6));
					showdialog->m_physics=atoi(m_basic.GetItemText(pLV->iItem ,7));
					showdialog->m_english=atoi(m_basic.GetItemText(pLV->iItem ,8));
					showdialog->DoModal();
					*pResult = 0;
				}
							

相关资源