createdbdlg.cpp

来自「windows ce开发技巧与实例光盘代码」· C++ 代码 · 共 834 行 · 第 1/2 页

CPP
834
字号
// CreateDBDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CreateDB.h"
#include "CreateDBDlg.h"


#include "voconnection.h"
#include "vorecordset.h"


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


extern CVOConnection	g_Conn;

/////////////////////////////////////////////////////////////////////////////
// CCreateDBDlg dialog

CCreateDBDlg::CCreateDBDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCreateDBDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCreateDBDlg)
	m_szFileName = _T("My Documents\\InsureEase.cdb");
	m_szTable = _T("");
	m_iOperation = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCreateDBDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCreateDBDlg)
	DDX_Control(pDX, IDC_COMBO_TABLE, m_comboTableCtrl);
	DDX_Control(pDX, IDC_LIST, m_listCtrl);
	DDX_Text(pDX, IDC_EDIT_FILENAME, m_szFileName);
	DDX_CBString(pDX, IDC_COMBO_TABLE, m_szTable);
	DDX_CBIndex(pDX, IDC_COMBO_OPERATION, m_iOperation);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCreateDBDlg, CDialog)
	//{{AFX_MSG_MAP(CCreateDBDlg)
	ON_BN_CLICKED(IDC_BTN_CREATE, OnBtnCreate)
	ON_CBN_CLOSEUP(IDC_COMBO_OPERATION, OnCloseupComboOperation)
	ON_BN_CLICKED(IDC_BTN_BEGIN, OnBtnBegin)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCreateDBDlg message handlers

BOOL CCreateDBDlg::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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	LoadTableList2Comb(_T("My Documents\\XML\\TableList.xml"));
	m_comboTableCtrl.EnableWindow(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CCreateDBDlg::OnBtnCreate() 
{
	m_listCtrl.ResetContent();
	
	UpdateData(TRUE);

	if( IsExistDB(m_szFileName) )
	{
		//It is important
		g_Conn.SetConnectDB(m_szFileName);

		m_listCtrl.AddString(_T("DB exist."));
		UpdateData(FALSE);
		
		DropTables(_T("My Documents\\XML\\TableList.xml"));

		m_listCtrl.AddString(_T("Drop tables."));
		UpdateData(FALSE);
	}
	else
	{
		if( CreateCDB(m_szFileName) )
		{
			m_listCtrl.AddString(_T("Create CDB."));
			UpdateData(FALSE);

			//It is important
			g_Conn.SetConnectDB(m_szFileName);
		}
		else
		{
			AfxMessageBox(_T("Create DB Error."));
			return;
		}
	}
	
	CreateTables(_T("My Documents\\XML\\CreateTables.xml"));
	
	m_listCtrl.AddString(_T("Create tables."));
	UpdateData(FALSE);

	InsertData();

	m_listCtrl.AddString(_T("Insert data."));
	UpdateData(FALSE);

}

BOOL CCreateDBDlg::IsExistDB(LPCTSTR lpszFileName)
{
	LPWIN32_FIND_DATA pFindFile = new WIN32_FIND_DATA;
	HANDLE hFind = FindFirstFile(lpszFileName,pFindFile);
	if( hFind == INVALID_HANDLE_VALUE ) 
	{
		DELETE_POINTER(pFindFile);
		
		return FALSE;
	}

	FindClose(hFind);
		
	DELETE_POINTER(pFindFile);

	return TRUE;
}

void CCreateDBDlg::DropTables(LPCTSTR lpszFileName)
{
	CString csText;
	CMarkup xml;

	ReadXML2Str(lpszFileName,csText,xml);

	CProgressWnd wndProgress(this, _T("正在删除表..."),TRUE);
    wndProgress.GoModal();
	wndProgress.SetRange(0,3000);
	long i = 0;

	CVORecordset rsTmp(g_Conn);
	CString szSQL;

	xml.FindElem(_T("TABLELIST"));

	while( xml.FindChildElem(_T("ITEM")) )
	{
		szSQL.Empty();
		
		szSQL.Format(_T("DROP TABLE %s"),xml.GetChildData());

		if( rsTmp.Open(szSQL) )
		{
			wndProgress.SetText(_T("已经删除%d个表。"),i+1);
			wndProgress.SetPos(i+1);
			wndProgress.PeekAndPump();

			i++;
		}
	}
	
	rsTmp.Close();

	wndProgress.Close();	
}

BOOL CCreateDBDlg::CreateCDB(LPCTSTR lpszFileName)
{	
	g_Conn.ClearConnect();
	
	UpdateData(TRUE);
	CVORecordset rs(g_Conn);
	CString szDB;
	szDB.Format(_T("CREATE DATABASE '%s'"),m_szFileName);
	if( rs.Open ( szDB ) )
	{
		rs.Close ();
	}
	else
	{
		return FALSE;
	}
	
	return TRUE;
}

void CCreateDBDlg::CreateTables(LPCTSTR lpszFileName)
{
	if( IsExistDB(m_szFileName) )
	{
		g_Conn.SetConnectDB(m_szFileName);
	}
	else
	{
		AfxMessageBox(_T("Database is NOT Exist."));
		return;
	}
	
	CString csText;
	CMarkup xml;
	
	ReadXML2Str(lpszFileName,csText,xml);

	CProgressWnd wndProgress(this, _T("正在创建表..."),TRUE);
    wndProgress.GoModal();
	wndProgress.SetRange(0,3000);
	long i = 0;

	CVORecordset rsTmp(g_Conn);
	CString szSQL;

	xml.FindElem(_T("CREATETABLES"));

	while( xml.FindChildElem(_T("ITEM")) )
	{
		szSQL.Empty();
		
		szSQL = xml.GetChildData();

		if( rsTmp.Open(szSQL) )
		{
			wndProgress.SetText(_T("已经创建%d个表。"),i+1);
			wndProgress.SetPos(i+1);
			wndProgress.PeekAndPump();

			i++;
		}
	}
	
	rsTmp.Close();

	wndProgress.Close();	
}

void CCreateDBDlg::InsertData()
{
	CString szText;
	for( int i=0;i<m_comboTableCtrl.GetCount();i++ )
	{
		szText.Empty();
		m_comboTableCtrl.GetLBText(i,szText);

		if( szText == _T("[INSUREDICT]"))
		{
#ifdef _X86_
			m_listCtrl.AddString(_T("INSUREDICT的数据超过了模拟器CDB文件的限制,不能插入。"));
			UpdateData(FALSE);
			//AfxMessageBox(_T("该表内的数据超过了模拟器CDB文件的限制,不能插入。"));
#else
			InsertInsureDict2DB(_T("My Documents\\XML\\"));
#endif
		}
		else if( szText == _T("[MARKETTIPS]"))
		{
			InsertMarketTips2DB(_T("My Documents\\XML\\MarketTips.xml"));
		}
		else
		{
			InsertDataFromXML(_T("My Documents\\XML\\"),szText);
		}
	}
}

void CCreateDBDlg::InsertMarketTips2DB(LPCTSTR lpszFileName)
{
	if( IsExistDB(m_szFileName) )
	{
		g_Conn.SetConnectDB(m_szFileName);
	}
	else
	{
		AfxMessageBox(_T("数据库不存在."));
		return;
	}
	
	CString csText;
	CMarkup xml;

	ReadXML2Str(lpszFileName,csText,xml);

	CProgressWnd wndProgress(this, _T("正在导入锦囊数据..."),TRUE);
    wndProgress.GoModal();
	wndProgress.SetRange(0,3000);
	long i = 0;

	CString szID,szParentID,szName,szContent;

	xml.FindElem(_T("营销锦囊"));

	while( xml.FindChildElem(_T("ITEM")) )
	{
		szID.Empty();
		szParentID.Empty();
		szName.Empty();
		szContent.Empty();

		xml.IntoElem();
		xml.FindChildElem(_T("ID"));
		szID = xml.GetChildData();
		//AfxMessageBox(szID);
		xml.FindChildElem(_T("PARENT_ID"));
		szParentID = xml.GetChildData();
		//AfxMessageBox(szParentID);
		xml.FindChildElem(_T("NAME"));
		szName = xml.GetChildData();
		//AfxMessageBox(szName);
		xml.FindChildElem(_T("CONTENT"));
		szContent = xml.GetChildData();
		//AfxMessageBox(szContent);
		
		if( Write2DB(szID,szParentID,szName,szContent,_T("[MARKETTIPS]")) )
		{
			wndProgress.SetText(_T("已经导入%d条记录。"),i+1);
			wndProgress.SetPos(i+1);
			wndProgress.PeekAndPump();

			i++;
		}

		xml.OutOfElem();
	}
	
	wndProgress.Close();
}
void CCreateDBDlg::InsertInsureDict2DB(LPCTSTR lpszFilePath)
{
	if( IsExistDB(m_szFileName) )
	{
		g_Conn.SetConnectDB(m_szFileName);
	}
	else
	{
		AfxMessageBox(_T("数据库不存在."));
		return;
	}

	int iCount = 0;
	CString szFile;
	CString szFileName;
	CString szFindFile;
	szFindFile.Format(_T("%sInsureDict*.xml"),lpszFilePath);

//	AfxMessageBox(szFindFile);
	
	CProgressWnd wndProgress(this, _T("正在导入词典数据..."),TRUE);
    wndProgress.GoModal();
	wndProgress.SetRange(0,3000);

	LPWIN32_FIND_DATA pFindFile = new WIN32_FIND_DATA;
	HANDLE hFind = FindFirstFile(szFindFile,pFindFile);
	if( hFind != INVALID_HANDLE_VALUE ) 
	{
		do
		{
			szFile.Empty();
			szFileName.Empty();
			szFileName = pFindFile->cFileName;
			szFile = lpszFilePath;
			szFile += szFileName;
		//	AfxMessageBox(szFile);
			DealWithInsureDict(iCount,wndProgress,szFile);
		}while(FindNextFile(hFind,pFindFile));
	}
	
	if( hFind != NULL && hFind != INVALID_HANDLE_VALUE)	
		FindClose(hFind);
		
	DELETE_POINTER(pFindFile);
}

BOOL CCreateDBDlg::Write2DB(CString &szID,CString &szParentID,CString &szName,CString &szContent,CString szTable)
{
	CVORecordset *pRecordset = new CVORecordset(g_Conn);
	CString szSQL;
	szSQL.Empty();	
	szSQL.Format(_T("INSERT INTO %s ([ID], [PARENT_ID], [NAME]) \
		VALUES (%s,%s,'%s')"),szTable,szID,szParentID,szName);

	if( !pRecordset->Open(szSQL) )
	{
		return FALSE;
	}

	szSQL.Empty();
	szSQL.Format(_T("SELECT [CONTEXT] FROM %s WHERE [ID]=%s"),szTable,szID);

	if( pRecordset->Open(szSQL,adOpenForwardOnly,adLockOptimistic) )
	{
		_variant_t varContext = szContent;
		if( !pRecordset->SetFieldValue(0,varContext) )
		{
			pRecordset->Close();
			delete pRecordset;
			return FALSE;
		}
	}

	pRecordset->Close();
	delete pRecordset;

	return TRUE;

⌨️ 快捷键说明

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