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

📄 asynctestdlg.cpp

📁 完成在ODBC中进行异步调用的C程序源代码
💻 CPP
字号:
// AsyncTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "AsyncTest.h"
#include "AsyncTestDlg.h"

#include "AsyncDatabase.h"
#include "AsyncRecordset.h"
//#include "DynamicRecordset.h"

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

#define _GET_MESSAGE_											\
	{															\
		MSG msg;												\
		if(::PeekMessage(&msg, m_hWnd, 0, WM_USER, PM_REMOVE))	\
			::DispatchMessage(&msg);							\
		if(!m_bInProgress)										\
		{														\
			m_strInfo.Format("Cancelled by user...");			\
			UpdateData(FALSE);									\
		}														\
	}

/////////////////////////////////////////////////////////////////////////////
// CAsyncTestDlg dialog

CAsyncTestDlg::CAsyncTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAsyncTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAsyncTestDlg)
	m_strQuery = _T("");
	m_strInfo = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bInProgress = FALSE;
}

void CAsyncTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAsyncTestDlg)
	DDX_Control(pDX, IDC_LIST1, m_ctrlList);
	DDX_Text(pDX, IDC_EDIT1, m_strQuery);
	DDX_Text(pDX, IDC_INFO, m_strInfo);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAsyncTestDlg, CDialog)
	//{{AFX_MSG_MAP(CAsyncTestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAsyncTestDlg message handlers

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

	// 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
	
	UpdateData(TRUE);
	m_strQuery = "SELECT * FROM Customers";
	UpdateData(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CAsyncTestDlg::OnButton1() 
{
	if(m_bInProgress)
	{
		::Beep(1400,100);
		return;
	}

	UpdateData(TRUE);
	m_bInProgress = TRUE;
	m_ctrlList.ResetContent();

	try
	{
		m_strInfo.Format("Executing a query...");
		UpdateData(FALSE);

		CAsyncDatabase DB;
		DB.OpenEx("");
		DB.SetQueryTimeout(0);

		m_strInfo.Format("Executing a query...");
		UpdateData(FALSE);
		
		/*
		// this is a async database ExecuteSQLAsync() test
		HSTMT hstmt = DB.ExecuteSQLAsync(m_strQuery);
		while(DB.SQLStillExecuting(hstmt))
		{
			_GET_MESSAGE_
			if(!m_bInProgress)
			{
				DB.Cancel(hstmt);
				
				m_strInfo.Format("Query cancelled at EXEC state...");
				UpdateData(FALSE);
				return;
			}
		}
		*/

		
		CAsyncRecordset rs(&DB);
		//rs.SetSqlQuery(m_strQuery);

		m_strInfo.Format("Opening recordset...");
		UpdateData(FALSE);

		rs.OpenAsync(CRecordset::snapshot, m_strQuery, CRecordset::executeDirect);
		
		while(rs.StillExecuting())
		{
			_GET_MESSAGE_
			if(!m_bInProgress)
			{
				m_strInfo.Format("Query canceled in OPEN state.");
				UpdateData(FALSE);
				rs.Cancel();
				rs.Close();
				return;
			}
		}

		if(!rs.IsOpen())
		{
			m_strInfo.Format("Query not opened...");
			UpdateData(FALSE);

			m_bInProgress = FALSE;
			return;
		}

		m_strInfo.Format("Getting query results...");
		UpdateData(FALSE);
		m_ctrlList.ResetContent();

		CString strHeader;

		FillHeader(rs, strHeader);
		m_ctrlList.AddString(strHeader);

		while(!rs.IsEOF())
		{
			CString str;
			FillRecord(rs, str);
			m_ctrlList.AddString(str);
			
			_GET_MESSAGE_
			if(!m_bInProgress)
			{
				m_strInfo.Format("Canceled in FETCH state...");
				UpdateData(FALSE);

				return;
			}
			
			rs.MoveNext();
		}

		rs.Close();

		m_strInfo.Format("Query complete...");
		UpdateData(FALSE);
	}
	catch(CDBException * err)
	{
		err->ReportError();
		err->Delete();
		m_strInfo.Format("Error executing query...");
		UpdateData(FALSE);
	}
	catch(...)
	{
		::AfxMessageBox("Internal error: exception caught!");
		m_strInfo.Format("Internal error...");
		UpdateData(FALSE);
	}

	m_bInProgress = FALSE;
}

void CAsyncTestDlg::OnCancel() 
{
	if(m_bInProgress)
		m_bInProgress = FALSE;
	else
		CDialog::OnCancel();
}

void CAsyncTestDlg::FillHeader(CRecordset & rs, CString & strHeader)
{
	CODBCFieldInfo fieldinfo;
	strHeader.Empty();
	try
	{
		ASSERT(rs.IsOpen());
		for(int i = 0; ;i++)
		{
			rs.GetODBCFieldInfo(i, fieldinfo);
			if(!strHeader.IsEmpty())
				strHeader += ',';
				strHeader += fieldinfo.m_strName;
		}
	}
	catch(...)
	{

	}
}

void CAsyncTestDlg::FillRecord(CRecordset & rs, CString & strRecord)
{
	strRecord.Empty();
	CString strValue;
	try
	{
		ASSERT(rs.IsOpen());
		for(int i = 0; ;i++)
		{
			rs.GetFieldValue(i, strValue);
			if(!strRecord.IsEmpty())
				strRecord += ',';
			strRecord += strValue;
		}
	}
	catch(...)
	{

	}

}

⌨️ 快捷键说明

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