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

📄 demo1dlg.cpp

📁 在Visual C++数据库编程设计中可采用ODBC API访问数据库,这提供了源码,供参考
💻 CPP
字号:
// demo1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "demo1.h"
#include "demo1Dlg.h"

#include "MyODBC.h"
#include "odbcset.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()

/////////////////////////////////////////////////////////////////////////////
// CDemo1Dlg dialog

CDemo1Dlg::CDemo1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDemo1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDemo1Dlg)
	m_strID = _T("");
	m_strJob = _T("");
	m_strName = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	ImageList.Create(16,16,ILC_COLOR8,0,5);
	ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_MEN));
}

void CDemo1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDemo1Dlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Text(pDX, IDC_EDITID, m_strID);
	DDX_Text(pDX, IDC_EDITJOB, m_strJob);
	DDX_Text(pDX, IDC_EDITNAME, m_strName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDemo1Dlg, CDialog)
	//{{AFX_MSG_MAP(CDemo1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, OnItemchangedList1)
	ON_BN_CLICKED(IDB_BTNREFRESH, OnBtnrefresh)
	ON_BN_CLICKED(IDC_BTNADD, OnBtnadd)
	ON_BN_CLICKED(IDC_BTNDEL, OnBtndel)
	ON_BN_CLICKED(IDC_BTNMODIFY, OnBtnmodify)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDemo1Dlg message handlers

BOOL CDemo1Dlg::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

	DWORD dwStyle=GetWindowLong(m_list.GetSafeHwnd(),GWL_STYLE);
	dwStyle |= LVS_REPORT;
	SetWindowLongA(m_list.GetSafeHwnd(),GWL_STYLE,dwStyle);
	m_list.SetExtendedStyle(LVS_EX_HEADERDRAGDROP |
		LVS_EX_FULLROWSELECT | LVS_EX_TRACKSELECT);
	m_list.SetImageList(&ImageList, LVSIL_SMALL);

	ShowData();											//显示数据库的内容
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

BOOL CDemo1Dlg::ShowData()								//显示数据库的内容
{
	int i = 0, iCount;
	m_list.DeleteAllItems();							//清空list控件

	iCount = m_list.GetHeaderCtrl()->GetItemCount();
	for(i = 0; i < iCount; i++)
		m_list.DeleteColumn(0);
	for(i = 0; i < iCount; i++)
		m_list.GetHeaderCtrl()->DeleteItem(0);

	CString strSql;
	strSql = "select * from student";
	CMyODBC db;
	db.ConnectDB("studt","", "");
	CODBCSet set;
	db.PrepareSql(strSql, set);
	for(i = 0; i < set.GetCols(); i++)
		m_list.InsertColumn(i,							//插入表头
			set.m_coldatafmt[i].name, LVCFMT_CENTER, 95);


	for(int iRow = 0; db.FetchData(); iRow++)			//插入数据
		for(i = 0; i < set.GetCols(); i++)
		{
			if(i == 0)
				m_list.InsertItem(iRow, set.m_coldata[0].value);
			else
				m_list.SetItemText(iRow, i, set.m_coldata[i].value);
		}
		
	set.Empty();										//清空数据集
	db.DisConnect();									//断开连接
		
	
return TRUE;	


}

void CDemo1Dlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	if(pNMListView->uNewState&LVIS_SELECTED)
	{
		char pContent[50] ;

		int iCurrent = pNMListView->iItem;
		m_list.GetItemText(iCurrent,0,pContent,50);
		m_strID.Format("%s", pContent);
		m_list.GetItemText(iCurrent,1,pContent,50);
		m_strName = CString(pContent);
		m_list.GetItemText(iCurrent,2,pContent,50);
		m_strJob = CString(pContent);
		UpdateData(FALSE);
	}
	
	*pResult = 0;
}

void CDemo1Dlg::OnBtnrefresh() 
{
	// TODO: Add your control notification handler code here
	this->ShowData();
	
}

void CDemo1Dlg::OnBtnadd()								//增加记录
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CString strSql;

	long nID;
	nID=atol(m_strID);									//字符串转换成长整型
	if ((m_strID=="")||(m_strName=="")||(m_strJob==""))
	{
		MessageBox("请将信息输入完整!","提示",MB_ICONEXCLAMATION |MB_OK);
	}
	else
	{
	if(nID<=0)
	{
		//职工号不能为0或者负数
		MessageBox("“学号”错误,请重新输入“学号”", "demol", MB_ICONSTOP);
		m_strID="";
		UpdateData(false);
		return;
	}
	else
		strSql.Format("insert into student values(%d,'%s','%s')",
			nID, m_strName, m_strJob);

	CMyODBC db;
	db.ConnectDB("studt","", "");						//连接数据库
	db.ExeSqlDirect(strSql);							//执行sql语句
	db.DisConnect();									//断开连接
	ShowData();											//刷新数据
	}
}

void CDemo1Dlg::OnBtndel()								//删除记录
{
	// TODO: Add your control notification handler code here
	if (m_strID=="")
	{
		MessageBox("请指定要删除的记录!","提示",MB_ICONEXCLAMATION |MB_OK);
	}
	else
	{
	if (MessageBox("确定要删除吗?","提示",MB_YESNO)==IDYES)
	{
	UpdateData();
	CString strSql;
	strSql.Format("delete from student where 学号 = %s", m_strID);
	CMyODBC db;
	db.ConnectDB("studt","", "");						//连接数据库
	db.ExeSqlDirect(strSql);							//执行sql语句
	db.DisConnect();									//断开连接
	this->ShowData();									//刷新数据
	m_strID = "";
	m_strName = "";
	m_strJob = "";
	UpdateData(FALSE);
	}
	}
}

void CDemo1Dlg::OnBtnmodify()							//修改记录
{
	// TODO: Add your control notification handler code here
	if (m_strID=="")
	{
		MessageBox("请指定要修改的记录!","提示",MB_ICONEXCLAMATION |MB_OK);
	}
	else
	{
	UpdateData();
	CString strSql;
	strSql.Format("update  student set 学生姓名 = '%s', 系别 = '%s' where 学号 = %s",
		m_strName, m_strJob, m_strID);
	CMyODBC db;
	db.ConnectDB("studt","", "");						//连接数据库
	db.ExeSqlDirect(strSql);							//执行sql语句
	db.DisConnect();									//断开连接
	this->ShowData();									//刷新数据
	m_strID = "";
	m_strName = "";
	m_strJob = "";
	UpdateData(FALSE);
	}
}

⌨️ 快捷键说明

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