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

📄 odbcdynamictestdlg.cpp

📁 Visual_C++[1].NET_Bible1 Visual_C++宝典书中的全部源码
💻 CPP
字号:
// ODBCDynamicTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ODBCDynamicTest.h"
#include "ODBCDynamicTestDlg.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() : CDialog(IDD_ABOUTBOX) {}

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	virtual void OnOK();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

void CAboutDlg::OnOK() 
{
	CWaitCursor wait;

	CString strURL;
	GetDlgItem(IDOK)->GetWindowText(strURL);
	if (!strURL.IsEmpty())
	{
		if (32 >= (int)ShellExecute(NULL, "open", strURL, NULL, NULL, SW_SHOWNORMAL))
		{
			AfxMessageBox("::ShellExecute failed to open this link!");
		}
	}
	else
	{
		AfxMessageBox("No URL defined on button!");
	}
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CODBCDynamicTestDlg dialog

CODBCDynamicTestDlg::CODBCDynamicTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CODBCDynamicTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CODBCDynamicTestDlg)
	m_strDSN = _T("");
	m_strTable = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_henv = NULL;
}

void CODBCDynamicTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CODBCDynamicTestDlg)
	DDX_Control(pDX, IDC_LST_DATA, m_lstData);
	DDX_Control(pDX, IDC_CBO_TABLES, m_cboTables);
	DDX_Control(pDX, IDC_CBO_DSNS, m_cboDSNs);
	DDX_CBString(pDX, IDC_CBO_DSNS, m_strDSN);
	DDX_CBString(pDX, IDC_CBO_TABLES, m_strTable);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CODBCDynamicTestDlg, CDialog)
	//{{AFX_MSG_MAP(CODBCDynamicTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_CBO_DSNS, OnSelchangeCboDsns)
	ON_WM_DESTROY()
	ON_CBN_SELCHANGE(IDC_CBO_TABLES, OnSelchangeCboTables)
	ON_BN_CLICKED(IDC_ABOUT, OnAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CODBCDynamicTestDlg message handlers

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

BOOL CODBCDynamicTestDlg::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
	
	// set list control's style
	LONG lStyle = m_lstData.SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
	lStyle |= LVS_EX_FULLROWSELECT;
	m_lstData.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)lStyle);

	m_lstData.SetColumnWidth(0, LVSCW_AUTOSIZE);


	SQLRETURN rc;

	if (SQL_SUCCESS == (rc = ::SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_henv)))
	{
		if (SQL_SUCCESS == (rc = ::SQLSetEnvAttr(m_henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER)))
		{
			if (SQL_SUCCESS == (rc = ::SQLAllocEnv(&m_henv)))
			{
				FillDSNComboBox();
			}
		}
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CODBCDynamicTestDlg::FillDSNComboBox()
{
	m_cboDSNs.ResetContent();

	if (m_henv)
	{
		SQLRETURN rc;

		UCHAR szDSN[SQL_MAX_DSN_LENGTH+1];
		UCHAR szDescription[512];                                                                                                                              
		SWORD swDescLen, swDSNLen;

		while (SQL_NO_DATA_FOUND != 
			(rc = ::SQLDataSources(m_henv, SQL_FETCH_NEXT, 
			szDSN, sizeof(szDSN), &swDSNLen, 
			szDescription, sizeof(szDescription), 
			&swDescLen)))
		{       
			m_cboDSNs.AddString(CString(szDSN));
		}		
		m_cboDSNs.SetCurSel(-1);
	}
}

void CODBCDynamicTestDlg::OnSelchangeCboDsns() 
{
	static CString strPrevDSN;

	if (m_henv)
	{
		if (UpdateData(TRUE))
		{
			if (0 != strPrevDSN.Compare(m_strDSN))
			{
				strPrevDSN = m_strDSN;
				FillTablesComboBox();
			}
		}
	}
}

void CODBCDynamicTestDlg::FillTablesComboBox()
{
	ResetTablesComboBox();

	SQLRETURN rc;
	SQLHDBC hdbc = NULL;
	if (SQL_SUCCESS == (rc = ::SQLAllocHandle(SQL_HANDLE_DBC, m_henv, &hdbc)))
	{
		UCHAR szDSN[SQL_MAX_DSN_LENGTH + 1];
		strcpy((char*)szDSN, m_strDSN);

		rc = ::SQLConnect(hdbc, (unsigned char*)szDSN, SQL_NTS,
			(unsigned char*)"", SQL_NTS, (unsigned char*)"", SQL_NTS);
		if ((SQL_SUCCESS == rc)
		|| (SQL_SUCCESS_WITH_INFO == rc))
		{
			SQLHSTMT hstmt = NULL;
			if (SQL_SUCCESS == (rc = ::SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt)))
			{
				if (SQL_SUCCESS == (rc = ::SQLTables(hstmt, NULL, 0, NULL, 0,
					NULL, 0, NULL, 0)))
				{
					SDWORD cb;
					char szTable[255];
					char szTableType[255];

					::SQLBindCol(hstmt, 3, SQL_C_CHAR, szTable, 255, &cb);
					::SQLBindCol(hstmt, 4, SQL_C_CHAR, szTableType, 255, &cb);

					while (SQL_SUCCESS == (rc = ::SQLFetch(hstmt)))
					{
						if (0 == strcmp(szTableType, "TABLE"))
						{
							m_cboTables.AddString(szTable);
						}
					}
				}
			}

			::SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
			hstmt = NULL;
		}
		::SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
		hdbc = NULL;
	}
}

void CODBCDynamicTestDlg::ResetTablesComboBox()
{
	m_cboTables.ResetContent();
	ResetDataListControl();
}

void CODBCDynamicTestDlg::ResetDataListControl()
{
	m_lstData.DeleteAllItems();
	int iNbrOfColumns;
	CHeaderCtrl* pHeader = (CHeaderCtrl*)m_lstData.GetDlgItem(0);
	if (pHeader)
	{
		iNbrOfColumns = pHeader->GetItemCount();
	}
	for (int i = iNbrOfColumns; i >= 0; i--)
	{
		m_lstData.DeleteColumn(i);
	}
}

void CODBCDynamicTestDlg::OnDestroy() 
{
	if (m_henv)
	{
		::SQLFreeHandle(SQL_HANDLE_ENV, m_henv);
		m_henv = NULL;
	}

	CDialog::OnDestroy();
}

void CODBCDynamicTestDlg::OnSelchangeCboTables() 
{
	static CString strPrevTable;

	if (m_henv)
	{
		if (UpdateData(TRUE))
		{
			if (0 != strPrevTable.Compare(m_strTable))
			{
				strPrevTable = m_strTable;

				
				FillDataListControl();
			}
		}
	}
}

void CODBCDynamicTestDlg::FillDataListControl()
{
	ResetDataListControl();

	if ((!m_strDSN.IsEmpty())
	&& (!m_strTable.IsEmpty()))
	{
		CClientDC dc(this);

		CODBCDynamic odbcDynamic(m_strDSN);

		CString strSQL;
		strSQL.Format("select * from %s", m_strTable);
		odbcDynamic.ExecuteSQL(strSQL);

		CODBCRecordArray* pODBCRecordArray = &odbcDynamic.m_ODBCRecordArray;

		for (int iRecord = 0; iRecord < pODBCRecordArray->GetSize(); iRecord++)
		{
			POSITION pos;
			CString strColName;
			CDBVariantEx* pvarValue;
			char szValue[255];

			int iCol = 0;
			CODBCRecord* pODBCRecord = (*pODBCRecordArray)[iRecord];
			for (pos = pODBCRecord->GetStartPosition(); pos != NULL;)
			{
				pODBCRecord->GetNextAssoc(pos, strColName, pvarValue);

				if (0 == iRecord)
				{
					CSize size = dc.GetTextExtent(strColName.GetBuffer(strColName.GetLength()));
					m_lstData.InsertColumn(iCol, strColName, LVCFMT_LEFT, size.cx, iCol);
				}

				if (pvarValue)
				{
					pvarValue->GetStringValue(szValue);
					if (0 < strlen(szValue))
					{
						if (0 == iCol)
						{
							iRecord = m_lstData.InsertItem(iRecord, szValue, iCol);
						}
						else
						{
							m_lstData.SetItemText(iRecord, iCol, szValue);
						}

						iCol++;
					}
				}
			}
		}
	}
}

void CODBCDynamicTestDlg::OnAbout() 
{
	CAboutDlg().DoModal();
}

⌨️ 快捷键说明

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