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

📄 odbcdemodlg.cpp

📁 《Visual C++ Bible》或者说是《Visual C++ 宝典》的对应的源码文件
💻 CPP
字号:
// ODBCDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ODBCDemo.h"
#include "ODBCDemoDlg.h"
#include "user.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

#define LEN_USERID 16
#define LEN_USERNAME 51

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

/////////////////////////////////////////////////////////////////////////////
// CODBCDemoDlg dialog

CODBCDemoDlg::CODBCDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CODBCDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CODBCDemoDlg)
	m_strUserID = _T("");
	m_strUserName = _T("");
	m_iStatus = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CODBCDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CODBCDemoDlg)
	DDX_Control(pDX, IDC_LBXUSERS, m_lbxUsers);
	DDX_Text(pDX, IDC_EDTUSERID, m_strUserID);
	DDX_Text(pDX, IDC_EDTUSERNAME, m_strUserName);
	DDX_Text(pDX, IDC_EDTSTATUS, m_iStatus);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CODBCDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CODBCDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_LBN_SELCHANGE(IDC_LBXUSERS, OnSelchangeLbxusers)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CODBCDemoDlg message handlers

BOOL CODBCDemoDlg::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
	
	FillListboxWithUsers();
		
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CODBCDemoDlg::FillListboxWithUsers()
{
	BOOL bSuccess = FALSE;

	CString strFunction;

	SQLHENV henv = NULL;
	SQLHDBC hdbc = NULL;
	BOOL bIsConnected = FALSE;
	SQLHSTMT hstmt = NULL;

	SQLRETURN rc;

	SWORD    sMsgNum = 0;
	char     szState[7]="";
	SDWORD   pfNative=0;

	if (SQL_SUCCESS == (rc = ::SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv)))
	{
		if (SQL_SUCCESS == (rc = ::SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER)))
		{
			if (SQL_SUCCESS == (rc = ::SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc)))
			{
				rc = ::SQLConnect(hdbc, 
					(SQLCHAR*)"Visual C++ Bible - Chapter 17", SQL_NTS,
					(SQLCHAR*)"", SQL_NTS, (SQLCHAR*)"", SQL_NTS);
				if ((SQL_SUCCESS == rc)
				|| (SQL_SUCCESS_WITH_INFO == rc))
				{
					bIsConnected = TRUE;
					if (SQL_SUCCESS == (rc = ::SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt)))
					{
						LPCSTR szSQL = "SELECT * FROM UserMaster";
						if (SQL_SUCCESS == (rc = ::SQLPrepare(hstmt, (unsigned char*)szSQL, SQL_NTS)))
						{
							if (SQL_SUCCESS == (rc = ::SQLExecute(hstmt)))
							{
								SDWORD cb;
								char szUserID[LEN_USERID];
								char szUserName[LEN_USERNAME];
								short iStatus;

								SQLBindCol(hstmt, 1, SQL_C_CHAR, szUserID, LEN_USERID, &cb);
								SQLBindCol(hstmt, 2, SQL_C_CHAR, szUserName, LEN_USERNAME, &cb);
								SQLBindCol(hstmt, 3, SQL_C_SHORT, &iStatus, 0, &cb);

								int iIndex;

								rc = SQLFetch(hstmt);
								while (SQL_SUCCESS == rc)
								{
									CUser* pUser = new CUser();
									pUser->m_strUserID = szUserID;
									pUser->m_strUserName = szUserName;
									pUser->m_iStatus = iStatus;

									iIndex = m_lbxUsers.AddString(szUserName);
									m_lbxUsers.SetItemData(iIndex, (DWORD)pUser);

									rc = SQLFetch(hstmt);
								}
							}
						}
					}
				}
			}
		}
	}

	if (henv)
	{
		if (hdbc)
		{
			if (bIsConnected)
			{
				if (hstmt)
				{
					::SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
				}
				::SQLDisconnect(hdbc);
				bIsConnected = FALSE;
			}
			::SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
			hdbc = NULL;
		}
		::SQLFreeHandle(SQL_HANDLE_ENV, henv);
		henv = NULL;
	}
}

BOOL CODBCDemoDlg::DestroyWindow() 
{
	CUser* pUser;
	int iUsers = m_lbxUsers.GetCount();
	for (int i = 0; i < iUsers; i++)
	{
		pUser = (CUser*)m_lbxUsers.GetItemData(i);
		if (pUser)
		{
			delete pUser;
		}
	}
	
	return CDialog::DestroyWindow();
}

void CODBCDemoDlg::OnSelchangeLbxusers() 
{
	int iIndex = m_lbxUsers.GetCurSel();
	if (LB_ERR != iIndex)
	{
		CUser* pUser = (CUser*)m_lbxUsers.GetItemData(iIndex);

		if (pUser)
		{
			m_strUserID = pUser->m_strUserID;
			m_strUserName = pUser->m_strUserName;
			m_iStatus = pUser->m_iStatus;

			UpdateData(FALSE);
		}
	}
}

⌨️ 快捷键说明

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