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

📄 adostdlg.cpp

📁 用ADO+CListCtrl操作SQL的示例程序
💻 CPP
字号:
// AdoSTDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AdoST.h"
#include "AdoSTDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CAdoSTDlg dialog

CAdoSTDlg::CAdoSTDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAdoSTDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAdoSTDlg)
	m_id = _T("");
	m_name = _T("");
	m_qita = _T("");
	m_RecNo = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAdoSTDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAdoSTDlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Text(pDX, IDC_EDIT1, m_id);
	DDX_Text(pDX, IDC_EDIT2, m_name);
	DDX_Text(pDX, IDC_EDIT3, m_qita);
	DDX_Text(pDX, IDC_EDITREC, m_RecNo);
	DDV_MinMaxLong(pDX, m_RecNo, 0, 10000);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAdoSTDlg, CDialog)
	//{{AFX_MSG_MAP(CAdoSTDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNBL, OnBtnbl)
	ON_BN_CLICKED(IDC_BTNADD, OnBtnadd)
	ON_BN_CLICKED(IDC_BTNMOD, OnBtnmod)
	ON_BN_CLICKED(IDC_BTNDEL, OnBtndel)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAdoSTDlg message handlers

BOOL CAdoSTDlg::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
//	m_list.SetExtendedStyle(LVS_EX_FLATSB |LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_ONECLICKACTIVATE|LVS_EX_GRIDLINES);
	m_col=0;
		
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CAdoSTDlg::OnBtnbl() 
{
	// TODO: Add your control notification handler code here
	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	CString sql;
	sql.Format("select * from st order by No ASC");
	_RecordsetPtr m_pRecordset;
	m_pRecordset=m_AdoConn.GetRecordSet((_bstr_t)sql);
////////////////////////////////////////////////
	
	int row,col,rowsum;
	m_list.SetExtendedStyle(LVS_REPORT|LVS_EX_FULLROWSELECT|LVS_OWNERDRAWFIXED |LVS_EX_FLATSB|LVS_SHOWSELALWAYS|LVS_EX_HEADERDRAGDROP);	
	m_list.DeleteAllItems();
	for (col=m_col;col>=0;col--) 
		m_list.DeleteColumn(col);

//	for (i=0;i<m_pRecordset->Fields->Count;i++) 
			//m_list.InsertColumn(i,(const char *)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t((long)i))->Name)); 

	rowsum=0;
	while(!m_pRecordset->adoEOF)
	{
		rowsum++;
		m_pRecordset->MoveNext();
	}
	m_pRecordset->MoveFirst();
	for (row=0;row<rowsum;row++)
			m_list.InsertItem(row,"row行",0);//不写此句可能造成只显示标题行

	for (col=0;col<m_pRecordset->Fields->Count;col++) 
			m_list.InsertColumn(col,(const char *)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t((long)col))->Name)); 

	for(col=0;col<m_pRecordset->Fields->Count;col++)
			m_list.SetColumnWidth(col,80);
	
	CString str;
//	while(!m_pRecordset->adoEOF)
//	{
	//	k=0;
	//	m_list.InsertItem(0,(char*)
	//		((_bstr_t) m_pRecordset->GetFields()->GetItem(_variant_t((long)k))->GetValue()));
	rowsum=0;
	for(row=0;!m_pRecordset->adoEOF;row++) 
	{
		for(col=0;col<m_pRecordset->Fields->Count;col++) 
		{
			//m_list.SetItemText(i,j,(char*)
			//	((_bstr_t) m_pRecordset->GetFields()->GetItem(_variant_t((long)j))->GetValue()));
		//	str.Format("%d行,%d列",row,col);
		//	m_list.SetItemText(row,col,str);
		    m_list.SetItemText(row,col,(char*)
				((_bstr_t) m_pRecordset->GetFields()->GetItem(_variant_t((long)col))->GetValue()));// 行,列,内容
				
		}
		m_pRecordset->MoveNext();
	}
//		m_pRecordset->MoveNext();
//		
//	}
	m_col=m_pRecordset->Fields->Count;
	//m_pRecordset->Close();
	m_AdoConn.ExitConnect();  //ExitConnect 已包含m_pRecordset->Close();
}

void CAdoSTDlg::OnBtnadd() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	if(m_id.IsEmpty())
	{
		MessageBox("此项不能为空!");
	}
	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	_bstr_t sql;
	sql="select * from st order by No ASC";
	_RecordsetPtr m_pRecordset;
	m_pRecordset=m_AdoConn.GetRecordSet((_bstr_t)sql);
	try
	{
		m_pRecordset->AddNew();
		m_pRecordset->PutCollect("No",(_bstr_t)m_id);
		m_pRecordset->PutCollect("Name",(_bstr_t)m_name);
		m_pRecordset->PutCollect("Address",(_bstr_t)m_qita);
		m_pRecordset->Update();
	}
	catch(...)
	{
		MessageBox("操作失败");
		return;
	}
	MessageBox("添加成功");
	m_list.DeleteAllItems();
	OnBtnbl();   //遍历显示
}

void CAdoSTDlg::OnBtnmod() 
{
	// TODO: Add your control notification handler code here
//**************************************************
	UpdateData(TRUE);
	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	_bstr_t sql;
	CString strsql;
//	strsql.Format("delete from st where no = 66%s",m_id);
	strsql.Format("update st set Name =  %s,Address =  %s where No =  %s",m_name,m_qita,m_id);
	//sql="delete from st where No=66";
	m_AdoConn.ExecuteSQL((_bstr_t)strsql);
	//m_AdoConn.ExecuteSQL(sql);

	m_AdoConn.ExitConnect();
	m_list.DeleteAllItems();
	OnBtnbl();  //遍历显示


//**************************************************
/*	UpdateData(TRUE);

	if(m_id.IsEmpty())
	{
		MessageBox("此项不能为空!");
	}
	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	_bstr_t sql;
	sql="select * from st order by No ASC";
	_RecordsetPtr m_pRecordset;
	m_pRecordset=m_AdoConn.GetRecordSet((_bstr_t)sql);
	
	try
	{
		m_pRecordset->Move(m_RecNo-1,vtMissing);
		m_pRecordset->PutCollect("No",(_bstr_t)m_id);
		m_pRecordset->PutCollect("Name",(_bstr_t)m_name);
		m_pRecordset->PutCollect("Address",(_bstr_t)m_qita);
		m_pRecordset->Update();
	}
	catch(...)
	{
		MessageBox("操作失败");
		return;
	}
	MessageBox("修改成功");
	m_list.DeleteAllItems();
	OnBtnbl();    */  //遍历显示 
	
}

void CAdoSTDlg::OnBtndel() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	_bstr_t sql;
	CString strsql;
//	strsql.Format("delete from st where no = 66%s",m_id);
	strsql.Format("delete from st where No = %s",m_id);
	//sql="delete from st where No=66";
	m_AdoConn.ExecuteSQL((_bstr_t)strsql);
	//m_AdoConn.ExecuteSQL(sql);

	m_AdoConn.ExitConnect();
	m_list.DeleteAllItems();
	OnBtnbl();  //遍历显示


	/*UpdateData(TRUE);

	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	_bstr_t sql;
	sql="select * from st order by No ASC";
	_RecordsetPtr m_pRecordset;
	m_pRecordset=m_AdoConn.GetRecordSet((_bstr_t)sql);
	CString m_id;
	
	try
	{
		m_pRecordset->Move(m_RecNo-1,vtMissing);
		m_pRecordset->Delete(adAffectCurrent);
		m_pRecordset->Update();
	}
	catch(...)
	{
		MessageBox("操作失败");
		return;
	}
	MessageBox("删除成功");
	m_list.DeleteAllItems();
	OnBtnbl();   *///遍历显示
}

void CAdoSTDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	int pos;
	pos=m_list.GetSelectionMark();
	m_id=m_list.GetItemText(pos,0);
	m_name=m_list.GetItemText(pos,1);
	m_qita=m_list.GetItemText(pos,2);
	UpdateData(false);
	*pResult = 0;
}


⌨️ 快捷键说明

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