www.pudn.com > 6433624505.rar > ExecuteSQL.cpp


// ExecuteSQL.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Browser.h" 
#include "ExecuteSQL.h" 
 
#include "ProgDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CExecuteSQL dialog 
 
 
CExecuteSQL::CExecuteSQL(CWnd* pParent /*=NULL*/) 
	: CDialog(CExecuteSQL::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CExecuteSQL) 
	m_StaticSQL = _T(""); 
	m_Edit = _T(""); 
	m_Rstnum = _T(""); 
	//}}AFX_DATA_INIT 
} 
 
CExecuteSQL::~CExecuteSQL() 
{ 
	names.clear(); 
	values.clear(); 
} 
 
void CExecuteSQL::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CExecuteSQL) 
	DDX_Control(pDX, IDC_EXPORT, m_Export); 
	DDX_Control(pDX, IDOK, m_Ok); 
	DDX_Control(pDX, IDCANCEL, m_Close); 
	DDX_Control(pDX, IDC_SQLLIST, m_List); 
	DDX_Text(pDX, IDC_STATICSQL, m_StaticSQL); 
	DDX_Text(pDX, IDC_EDIT1, m_Edit); 
	DDX_Text(pDX, IDC_STATICNUM, m_Rstnum); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CExecuteSQL, CDialog) 
	//{{AFX_MSG_MAP(CExecuteSQL) 
	ON_NOTIFY(NM_CLICK, IDC_SQLLIST, OnClickSqllist) 
	ON_BN_CLICKED(IDC_EXPORT, OnExport) 
	ON_WM_PAINT() 
	ON_WM_LBUTTONDOWN() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CExecuteSQL message handlers 
BOOL CExecuteSQL::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
	 
	m_bitmapground.LoadBitmap(IDB_SONGROUND); 
	m_Close.LoadBitmaps(IDB_CLOSEONE,IDB_CLOSETWO); 
	m_Ok.LoadBitmaps(IDB_TUICHUONE,IDB_TUICHUTWO); 
	m_Export.LoadBitmaps(IDB_EXPONE,IDB_EXPTWO); 
	 
	m_ColorBtn1.SubclassDlgItem(IDC_STATICNUM,this); 
	m_ColorBtn1.SetBackColor(RGB(99,103,105)); 
 
	names.clear(); 
	values.clear(); 
	m_Edit.Empty(); 
	m_List.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT); 
	OnRefresh(); 
	this->UpdateData(false); 
	return TRUE; 
} 
 
void CExecuteSQL::OnRefresh() 
{ 
	CProgressDlg pro; 
	m_List.DeleteAllItems(); 
 
	_bstr_t sql = m_StaticSQL; 
	CString val,name; 
	int count = 0,rstnum = 0,counts = 0; 
 
	try 
	{ 
		pRst = pConn->Execute(sql,NULL,adCmdText); 
		colume = pRst->Fields->GetCount(); 
		for(int a = 0;a < colume;a++) 
		{ 
			name = (char *)_bstr_t(pRst->Fields->GetItem(long(a))->Name); 
			names.push_back(name); 
		} 
		while(!pRst->End) 
		{ 
			counts ++; 
			pRst->MoveNext(); 
		} 
 
		pro.Create(this); 
		pro.SetWindowText(_T("请稍后...")); 
		pro.m_Progress.SetRange(0,4*counts/30); 
		pro.m_Progress.SetPos(0); 
 
		pRst = pConn->Execute(sql,NULL,adCmdText); 
		while(!pRst->End) 
		{ 
			for(int b = 0;b < colume;b++) 
			{ 
				_variant_t vA = pRst->Fields->GetItem(long(b))->Value; 
				if(vA.vt == VT_NULL) 
					val = "NULL"; 
				else 
					val = (char *)_bstr_t(vA); 
				values.push_back(val); 
			} 
			count ++; 
			pro.m_Progress.SetPos(count/10); 
			pro.SetStatus(count); 
			rstnum ++; 
			pRst->MoveNext(); 
		} 
		m_Rstnum.Format("\r\n\r\n共返回 %d 条记录",rstnum); 
		 
		for(int col = 0;col < colume;col ++) 
		{ 
			m_List.InsertColumn(col,names[col],80,80); 
		} 
		pro.SetStatus(); 
		pRst = pConn->Execute(sql,NULL,adCmdText); 
		if(!pRst->End) 
		{ 
			for(int col = 0;col < count;col ++) 
			{ 
				m_List.InsertItem(col,values[col*colume]); 
				for(int subcol = 1;subcol < colume;subcol ++) 
				{ 
					m_List.SetItemText(col,subcol,values[subcol+col*colume]); 
				} 
				pro.m_Progress.SetPos(counts+col/30); 
			} 
		} 
		count = 0; 
		pRst->Close(); 
		pRst.Release(); 
 
		CTime time = CTime::GetCurrentTime(); 
		int nYear = time.GetYear(); 
		int nMonth = time.GetMonth(); 
		int nDay = time.GetDay(); 
		int nHour = time.GetHour(); 
		int nMinute = time.GetMinute(); 
		int nSecond = time.GetSecond(); 
		CString timestr; 
		timestr.Format("%d年%d月%d日 %d:%d:%d",nYear,nMonth,nDay,nHour,nMinute,nSecond); 
 
		CFile file(_T("log.txt"),CFile::modeWrite | CFile::modeCreate | CFile::modeNoTruncate); 
		DWORD InitLength = file.GetLength(); 
		file.Seek(InitLength,CFile::begin); 
		file.Write(timestr + _T("\r\n") + m_StaticSQL+_T("\r\n\r\n"),timestr.GetLength() + m_StaticSQL.GetLength()+6*sizeof(char)); 
 
	} 
	catch(_com_error &e) 
	{ 
		if(pConn == NULL) 
			AfxMessageBox("未连接数据库"); 
		else 
			AfxMessageBox(e.Description()); 
		CDialog::OnOK(); 
	} 
	pro.DestroyWindow(); 
} 
 
void CExecuteSQL::OnClickSqllist(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	cursel = m_List.GetSelectionMark(); 
	m_Edit.Empty(); 
	m_Edit.Format("当前是第 %d 条记录\r\n",cursel + 1); 
	CString str[200]; 
	if(cursel >= 0) 
	{ 
		for(int i = 0;i < names.size(); i++) 
		{ 
			str[i].Format("%s:        %s\r\n",names[i],values[cursel*colume+i]); 
			m_Edit += str[i]; 
		} 
		this->UpdateData(false); 
	} 
	 
	*pResult = 0; 
} 
 
void CExecuteSQL::OnExport()  
{ 
	try 
	{ 
		CFile file(_T("Export.txt"),CFile::modeReadWrite | CFile::modeCreate); 
		for(int i = 0;i < values.size();i ++) 
		{ 
			if(i%colume == 0&&i != 0) 
				file.Write(_T("\r\n\r\n"),4*sizeof(char)); 
			file.Write(values[i]+"   ",values[i].GetLength()+3*sizeof(char)); 
		} 
		file.Close(); 
		AfxMessageBox("成功导出!"); 
	} 
	catch(_com_error &e) 
	{ 
		AfxMessageBox(e.Description()); 
	} 
} 
 
void CExecuteSQL::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
	 
	CRect rect; 
	GetClientRect(&rect); 
	CDC dcMem;  
	dcMem.CreateCompatibleDC(&dc);  
	BITMAP bitMap; 
	m_bitmapground.GetBitmap(&bitMap); 
	CBitmap *pbmpOld=dcMem.SelectObject(&m_bitmapground); 
	dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitMap.bmWidth,bitMap.bmHeight,SRCCOPY); 
	dc.SetBkColor(RGB(99,103,105)); 
	dc.SetTextColor(RGB(200,200,200)); 
	dc.TextOut(34,36,_T("当前 SQL 语句")); 
 
	CDialog::OnPaint(); 
} 
 
void CExecuteSQL::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	if(point.x >=5 && point.x <= 65 && point.y >= 2 && point.y <=30) 
		PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x, point.y));			 
	CDialog::OnLButtonDown(nFlags, point); 
}