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

📄 mainfrm.cpp

📁 连接MYSQL数据库
💻 CPP
字号:
//author : Jarry
//E-mail : lansingk@online.sh.cn
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "mysqlManager.h"

#include "MainFrm.h"
#include "LeftView.h"
#include "RightPanel.h"
#include "SQLView.h"
#include "MysqlManagerView.h"

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame,CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_SQL_RUN, OnSqlRun)
	ON_COMMAND(ID_BTN_SAVE_SQL, OnBtnSaveSql)
	ON_COMMAND(ID_BTN_LOAD_SQL, OnBtnLoadSql)
	ON_COMMAND(ID_BTN_OPEN_REG, OnBtnLoadReg)
	ON_COMMAND(ID_BTN_SAVE_REG, OnBtnSaveReg)
	ON_COMMAND(ID_BTN_NEW_REG, OnBtnNewReg)
	//}}AFX_MSG_MAP
//	ON_UPDATE_COMMAND_UI_RANGE(AFX_ID_VIEW_MINIMUM, AFX_ID_VIEW_MAXIMUM, OnUpdateViewStyles)
//	ON_COMMAND_RANGE(AFX_ID_VIEW_MINIMUM, AFX_ID_VIEW_MAXIMUM, OnViewStyle)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
//	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
//	EnableDocking(CBRS_ALIGN_ANY);
//	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
	CCreateContext* pContext)
{
	// create splitter window
	if (!m_wndSplitter.CreateStatic(this, 1, 2))
		return FALSE;

	if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftView), CSize(200, 200), pContext) ||
		!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CRightPanel), CSize(100, 100), pContext))
	{
		m_wndSplitter.DestroyWindow();
		return FALSE;
	}

	return TRUE;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;

	cs.style &= ~FWS_ADDTOTITLE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
CLeftView* CMainFrame::GetLeftPanel()
{
	CWnd* pWnd = m_wndSplitter.GetPane(0, 0);
	CLeftView *pView = DYNAMIC_DOWNCAST(CLeftView, pWnd);
	return pView;
}

CRightPanel* CMainFrame::GetRightPane()
{
	CWnd* pWnd = m_wndSplitter.GetPane(0, 1);
	CRightPanel *pView = DYNAMIC_DOWNCAST(CRightPanel, pWnd);
	return pView;
}

void CMainFrame::OnSqlRun() 
{
	CEdit& edtSQL = GetRightPane()->GetSQLView()->GetEditCtrl(); 
	CListCtrl& lstRecords = GetRightPane()->GetListView()->GetListCtrl(); 
	MYSQL *pMysql = GetLeftPanel()->m_pCurMysql;
	int nStartChar,nEndChar,nStartLine,nEndLine;
	int nLineEnd,nLineCount;
	int i;
	CHAR szLine[2048];
	CString strSQL;

	strSQL = "";
	edtSQL.GetSel(nStartChar,nEndChar);
	nLineCount = edtSQL.GetLineCount();
	if ((nStartChar >= 0) && (nStartChar < nEndChar))  //Some text selected
	{
		nStartLine = edtSQL.LineFromChar(nStartChar);
		nEndLine   = edtSQL.LineFromChar(nEndChar);
		if (nStartLine >= nEndLine) //The same line
		{
			nLineEnd = edtSQL.GetLine(nStartLine,szLine,2048 - 1); //reserved null character
			szLine[nLineEnd] = '\0';
			strSQL = szLine;
			i = edtSQL.LineIndex(nStartLine);
			nStartChar -= i;  //The index in this line
			nEndChar -= i;
			strSQL = strSQL.Mid(nStartChar,nEndChar - nStartChar);
		}
		else
		{
			nLineEnd = edtSQL.GetLine(nStartLine,szLine,2048 - 1); //reserved null character
			szLine[nLineEnd] = '\0';
			strSQL = szLine;
			i = edtSQL.LineIndex(nStartLine + 1) - 2; //Next line , don't include line feed CR-LF
			strSQL = strSQL.Right(i - nStartChar); //The first selected line
			strSQL += '\n';
			for (i = nStartLine + 1; i < nEndLine; i++)
			{
				nLineEnd = edtSQL.GetLine(i,szLine,2048 - 1); //reserved null character
				szLine[nLineEnd] = '\n'; //add line feed
				nLineEnd++;
				szLine[nLineEnd] = '\0'; //null character 
				strSQL += szLine;
			}
			nLineEnd = edtSQL.GetLine(nEndLine,szLine,2048 - 1); //reserved null character
			szLine[nLineEnd] = '\0';
			i = edtSQL.LineIndex(nEndLine);
			strSQL += CString(szLine).Left(nEndChar - i); //The last selected line 
		}
	}
	else
	{
		for (i = 0 ; i < nLineCount ; i++)
		{
			nLineEnd = edtSQL.GetLine(i,szLine,2048 - 1); //reserved null character
			szLine[nLineEnd] = '\n'; //add line feed
			nLineEnd++;
			szLine[nLineEnd] = '\0'; //null character 
			strSQL += szLine;
		}
	}

	if (pMysql == NULL)
	{
		HTREEITEM tviSel = GetLeftPanel()->GetTreeCtrl().GetSelectedItem();
		GetLeftPanel()->ItemStyle(tviSel,&pMysql);
	}

	if (pMysql != NULL)
	{
		MYSQL_RES	*res;
		GetLeftPanel()->m_pCurMysql = pMysql;
		if (mysql_query(pMysql,strSQL) == 0)
		{
			CHAR szStatus[1024];
			__int64 nAffect;

			res = mysql_store_result(pMysql);
			nAffect = mysql_affected_rows(pMysql);  //must use mysql_store_result at first
			                                        //otherwise mysql_affected_rows return wrong
			                                        //numeric when using SELECT statement
			ShowRecords(res,lstRecords,true);
			if (res != NULL)
				mysql_free_result(res);
			sprintf(szStatus,"%I64u row(s) affected",nAffect);
			m_wndStatusBar.SetPaneText(0,szStatus);	
		}
		else
			AfxMessageBox(mysql_error(pMysql));
	}
	else
		AfxMessageBox(IDS_MSG_SELECT_DATABASE);

	edtSQL.SetFocus();
	lstRecords.SetExtendedStyle(LVS_EX_FULLROWSELECT);
}

void CMainFrame::OnBtnSaveSql() 
{
	CEdit& edtSQL = GetRightPane()->GetSQLView()->GetEditCtrl();
	CHAR szLine[2048];

	CFileDialog dlgSave(false,"sql",NULL,NULL,"SQL files(*.sql)|*.sql||");

	if (dlgSave.DoModal() == IDOK)
	{
		CFile fileSave(dlgSave.GetPathName(),CFile::modeCreate | CFile::modeWrite);
		CArchive arSave(&fileSave,CArchive::store);
		int j = edtSQL.GetLineCount();
		for (int i = 0 ; i < j; i ++)
		{
			int nLineEnd = edtSQL.GetLine(i,szLine,2048 - 1);
			szLine[nLineEnd] = '\n'; //add line feed
			nLineEnd++;
			szLine[nLineEnd] = '\0';
			arSave.WriteString(szLine);
		}
		arSave.Flush();
		arSave.Close();
		fileSave.Close();
	}	
}

void CMainFrame::OnBtnLoadSql() 
{
	CEdit& edtSQL = GetRightPane()->GetSQLView()->GetEditCtrl();
	CHAR szSQL[64 * 1024];
	CHAR szLine[2048];

	CFileDialog dlgLoad(true,"sql",NULL,NULL,"SQL files(*.sql)|*.sql||");

	if (dlgLoad.DoModal() == IDOK)
	{
		CFile fileLoad(dlgLoad.GetPathName(),CFile::modeRead);
		CArchive arLoad(&fileLoad,CArchive::load);
		edtSQL.Clear();
		strcpy(szSQL,"");
		while (arLoad.ReadString(szLine,2048 - 1) != NULL)
		{
			strcat(szSQL,szLine);
			strcat(szSQL,"\r\n");
		}
		edtSQL.SetWindowText(szSQL);

		arLoad.Close();
		fileLoad.Close();
	}	
}

void CMainFrame::OnBtnLoadReg() 
{
	CLeftView *pView = GetLeftPanel();
	CFileDialog dlgLoad(true,"myr",NULL,NULL,"Register files(*.myr)|*.myr||");

	if (dlgLoad.DoModal() == IDOK)
	{
		pView->RemoveAllServer();  //Remove the list
		pView->InitServerGrp();    //Reset the tree
		CFile fileLoad(dlgLoad.GetPathName(),CFile::modeRead);
		CArchive arLoad(&fileLoad,CArchive::load);

		pView->Serialize(arLoad);

		arLoad.Close();
		fileLoad.Close();
	}	
}

void CMainFrame::OnBtnSaveReg() 
{
	CFileDialog dlgSave(false,"myr",NULL,NULL,"Register files(*.myr)|*.myr||");

	if (dlgSave.DoModal() == IDOK)
	{
		CFile fileSave(dlgSave.GetPathName(),CFile::modeCreate | CFile::modeWrite);
		CArchive arSave(&fileSave,CArchive::store);

		GetLeftPanel()->Serialize(arSave);

		arSave.Flush();
		arSave.Close();
		fileSave.Close();
	}	
}

void CMainFrame::OnBtnNewReg() 
{
	CDlgRegister& dlgReg = GetLeftPanel()->dlgRegister;

	while (dlgReg.DoModal() == IDOK &&
		  !GetLeftPanel()->RegisterServer(dlgReg.m_strServer,
		                                  dlgReg.m_strHost,
										  dlgReg.m_nPort,
										  dlgReg.m_bTrust,
										  dlgReg.m_strUser,
										  dlgReg.m_strPasswd));
}

//When close this window , the showwindow message is generated ,
//It's strange , I didn't known why , but it happened
//So I can't put auto-register server code in showwindow

⌨️ 快捷键说明

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