mytestdlg.cpp

来自「本实例是vc+SQL server小程序,具有数据库的简单查询」· C++ 代码 · 共 371 行

CPP
371
字号
// MyTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyTest.h"
#include "MyTestDlg.h"
/////////////////////////////////////////////////////
#include "column.h"
#include "columns.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()

/////////////////////////////////////////////////////////////////////////////
// CMyTestDlg dialog

CMyTestDlg::CMyTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyTestDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyTestDlg)
	DDX_Control(pDX, IDC_DATAGRID1, m_pDataGrid);
	DDX_Control(pDX, IDC_ADODC1, m_pAdoDc);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyTestDlg, CDialog)
	//{{AFX_MSG_MAP(CMyTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_BN_CLICKED(IDC_QUERY, OnQuery)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyTestDlg message handlers

BOOL CMyTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	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);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyTestDlg::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 CMyTestDlg::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 CMyTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyTestDlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	CAddDlg cod;
	
	if(cod.DoModal() == IDOK)
	{
		m_pPAddr = cod.m_pAddAddr;
		m_pPName = cod.m_pAddName;
		m_pPSex = cod.m_pAddSex;
		m_pTabNum = cod.m_pTabNum;

		try
		{
			//找出数据库中的最大ID
			long lDepID;	
			_bstr_t sqlcs;
			sqlcs= "SELECT MAX(PID) AS Expr1 FROM Playerinfo";		
			_RecordsetPtr m_pRecordset;

			con.OninitADOConn();
			m_pRecordset = con.GetRecordSet(sqlcs);

			if(m_pRecordset->adoEOF)
			{
				lDepID = 0;
			}
			else
			{
				lDepID = atol((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Expr1"));
			}
			
			CString strPID;
			CString strPSex;
			CString strTabNum;

			strPID.Format("%d",lDepID+1);
			strPSex.Format("%d",m_pPSex);
			strTabNum.Format("%d",m_pTabNum);
			
			_bstr_t insertSql = "INSERT INTO Playerinfo(PID, PName, PSex, PAddr, TabNum) VALUES ('"
		+ strPID + "','" + m_pPName + "','" + strPSex + "', '" + m_pPAddr + "', '"  + strTabNum + "')";
			con.ExecuteSQL(insertSql);
			con.ExitConnect();

			CString cSource = "select * from Playerinfo";
			m_pAdoDc.SetRecordSource(cSource);
			m_pAdoDc.Refresh();
			//刷新DataGrid记录
//			CDataGrid m_Grid;
//			m_Grid.Refresh(); 
//			m_pDataGrid.SetRefDataSource(NULL);
//			m_pDataGrid.GetDataSource();
 //			m_pDataGrid.SetRefDataSource((LPUNKNOWN)m_pAdoDc.GetRecordset());
//			m_pDataGrid.Refresh();;
// 			UpdateData(TRUE);
		}
		catch(_com_error e)
		{
				AfxMessageBox(e.Description());
		}
	}
}

void CMyTestDlg::OnDelete() 
{
	try{
	
		if (MessageBox("是否删除当前记录?","确认删除", MB_YESNO) == IDYES)
		{
			CString delID = m_pDataGrid.GetItem(0);
			_bstr_t delSql;
			delSql = "delete from Playerinfo where PID=" + delID;
			con.OninitADOConn();
			con.ExecuteSQL(delSql);
			con.ExitConnect();	
			
			CString cSource = "select * from Playerinfo";
			m_pAdoDc.SetRecordSource(cSource);
			m_pAdoDc.Refresh();
		}
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
	}
}

void CMyTestDlg::OnQuery() 
{
	CQueryDlg cqd;

	cqd.m_pQueryName = "";
	cqd.m_pQueryAddr = "";
	cqd.m_pQuerySex = 2;
	cqd.m_pQueryTabNum = 0;
	
	if(cqd.DoModal() == IDOK)
	{
		UpdateData(TRUE);
		m_pPAddr = cqd.m_pQueryAddr;
		m_pPName = cqd.m_pQueryName;
		m_pPSex = cqd.m_pQuerySex;
		m_pTabNum = cqd.m_pQueryTabNum;

		try
		{
			con.OninitADOConn();
		
			CString strPSex;
			CString strTabNum;
			strPSex.Format("%d",m_pPSex);
			strTabNum.Format("%d",m_pTabNum);

			BOOL flag = true;
			
			_bstr_t querySql = "select * from Playerinfo ";
			
			if(m_pPName != "")
			{
				querySql += "where PName like '" ;
				querySql = querySql + m_pPName;
				querySql += "%'";
				flag = false;
			}
// 			AfxMessageBox(querySql);
			if(m_pPAddr != "")
			{
				if(flag)
				{
				querySql += "where PAddr like '";
				querySql = querySql + m_pPAddr;
				querySql += "%'";	
				flag = false;
				}
				else
				{
					querySql += " and PAddr like '";
					querySql = querySql + m_pPAddr;
					querySql += "%'";
				}
			}
			if(m_pPSex != 2)
			{
				if(flag)
				{
					querySql += "where PSex=";
					querySql = querySql + strPSex;
					flag = false;
				}
				else
				{
					querySql += " and PSex=";
					querySql = querySql + strPSex;
				}
			}
			if(m_pTabNum != 0)
			{
				if(flag)
				{
					querySql += "where TabNum=";
					querySql = querySql + strTabNum;
					flag = false;
				}
				else
				{
					querySql += " and TabNum=";
					querySql = querySql + strTabNum;
				}
			}

 // 			AfxMessageBox(querySql);
		
// 			con.ExecuteSQL(querySql);
// 			con.ExitConnect();

// 			CString cSource = "select * from Playerinfo";
			m_pAdoDc.SetRecordSource(querySql);
			m_pAdoDc.Refresh();
////
////			//刷新DataGrid记录
////			m_pDataGrid.SetRefDataSource(NULL);
////// 			m_pDataGrid.SetRefDataSource((LPUNKNOWN)m_pAdoDc.GetRecordset());
//			m_pDataGrid.Refresh();
		}
		catch(_com_error e)
		{
				AfxMessageBox(e.Description());
		}
	}
	
}

⌨️ 快捷键说明

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