createdbdlg.cpp

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

CPP
834
字号
}

void CCreateDBDlg::DealWithInsureDict(int &iCount,CProgressWnd &wnd,CString &szFileName)
{
	CString csText;
	CMarkup xml;
	
	ReadXML2Str(szFileName,csText,xml);

	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("[INSUREDICT]")) )
		{
			wnd.SetText(_T("已经导入%d条记录。"),iCount+1);
			wnd.SetPos(iCount+1);
			wnd.PeekAndPump();

			iCount++;
		}

		xml.OutOfElem();
	}
}

void CCreateDBDlg::ReadXML2Str(LPCTSTR lpszFileName,CString &csText,CMarkup &xml)
{
	csText.Empty();

	// Load up buffer
	unsigned char* pBuffer = NULL;
	int nFileLen = 0;

	CFile file( lpszFileName, CFile::modeRead );
	nFileLen = file.GetLength();

	// Allocate Buffer for Ansi file data
	pBuffer = new unsigned char[nFileLen + 1];
	nFileLen = file.Read( pBuffer, nFileLen );
	file.Close();
	pBuffer[nFileLen] = '\0';

#if defined(_UNICODE)
	// Convert file to UNICODE if necessary
	int nWideSize = MultiByteToWideChar(CP_ACP,0,(const char*)pBuffer,nFileLen,csText.GetBuffer(nFileLen),nFileLen*2);
	csText.ReleaseBuffer(nWideSize);
#else
	csText = (char*)pBuffer;
#endif

	// Convert newlines to CRLFs for CEdit
	CString csCRLFText;
	const _TCHAR* pSource = (LPCTSTR)csText;
	_TCHAR* pDest = csCRLFText.GetBuffer(csText.GetLength() * 2);
	int nSrcChar = 0, nDestChar = 0;
	while ( pSource[nSrcChar] )
	{
		if ( pSource[nSrcChar] == '\n' && (nSrcChar == 0 || pSource[nSrcChar-1]!='\r') )
			pDest[nDestChar++] = '\r';
		pDest[nDestChar++] = pSource[nSrcChar++];
	}
	csCRLFText.ReleaseBuffer(nDestChar);
	csText = csCRLFText;

	xml.SetDoc( csText );
	delete [] pBuffer;
}

void CCreateDBDlg::LoadTableList2Comb(LPCTSTR lpszFileName)
{
	CString csText;
	CMarkup xml;
	ReadXML2Str(lpszFileName,csText,xml);

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

	while( xml.FindChildElem(_T("ITEM")) )
	{
		m_comboTableCtrl.AddString(xml.GetChildData());
	}

	UpdateData(FALSE);
}

void CCreateDBDlg::OnCloseupComboOperation() 
{
	UpdateData(TRUE);

	if( m_iOperation == 0 ||//Create Database
		m_iOperation == 2 ||//Create All Tables
		m_iOperation == 4 ||//Drop All Tables
		m_iOperation == 6 ||//Insert All Data
		m_iOperation == 8)//Delete All Data
	{
		m_comboTableCtrl.EnableWindow(FALSE);
	}
	else
	{
		m_comboTableCtrl.EnableWindow(TRUE);
	}

	UpdateData(FALSE);
}

void CCreateDBDlg::OnBtnBegin() 
{
	UpdateData(TRUE);
	
	switch( m_iOperation )
	{
	case 0:
		//Create Database
		if( IsExistDB(m_szFileName) )
		{
			AfxMessageBox(_T("DB Exist."));
		}
		else
		{
			if( CreateCDB(m_szFileName) )
				AfxMessageBox(_T("Create DB Successfully."));
		}
		break;
	case 1:
		//Create sepcial table
		CreateTable(_T("My Documents\\XML\\CreateTables.xml"));
		break;
	case 2:
		//Create all tables
		DropTables(_T("My Documents\\XML\\TableList.xml"));
		CreateTables(_T("My Documents\\XML\\CreateTables.xml"));
		break;
	case 3:
		//Drop sepcial table
		DropTable();
		break;
	case 4:
		//Drop all tables
		DropTables(_T("My Documents\\XML\\TableList.xml"));
		break;
	case 5:
		//Insert data
		if( m_szTable == _T("[MARKETTIPS]") )
			InsertMarketTips2DB(_T("My Documents\\XML\\MarketTips.xml"));
		else if( m_szTable == _T("[INSUREDICT]") )
		{
#ifdef _X86_
			AfxMessageBox(_T("INSUREDICT的数据超过了模拟器CDB文件的限制,不能插入。"));
#else
			InsertInsureDict2DB(_T("My Documents\\XML\\"));
#endif
		}
		else
			InsertDataFromXML(_T("My Documents\\XML\\"),m_szTable);
		break;
	case 6:
		//Insert all data
		InsertData();
		break;
	case 7:
		//Delete data
		DeleteData();
		break;
	case 8:
		//Delete all data
		DeleteAllData(_T("My Documents\\XML\\TableList.xml"));
		break;
	}
}

void CCreateDBDlg::DeleteAllData(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("TABLELIST"));

	while( xml.FindChildElem(_T("ITEM")) )
	{
		szSQL.Empty();
		
		szSQL.Format(_T("DELETE FROM %s"),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::DeleteData()
{
	if( IsExistDB(m_szFileName) )
	{
		g_Conn.SetConnectDB(m_szFileName);
	}
	else
	{
		AfxMessageBox(_T("Database is NOT Exist."));
		return;
	}
	
	UpdateData(TRUE);

	CVORecordset rsTmp(g_Conn);

	CString szSQL;
	szSQL.Format(_T("DELETE FROM %s"),m_szTable);

	if( rsTmp.Open(szSQL) )
	{
		AfxMessageBox(_T("Delete data Successfully."));
		rsTmp.Close();
	}
}

void CCreateDBDlg::DropTable()
{
	if( IsExistDB(m_szFileName) )
	{
		g_Conn.SetConnectDB(m_szFileName);
	}
	else
	{
		AfxMessageBox(_T("Database is NOT Exist."));
		return;
	}


	UpdateData(TRUE);

	CVORecordset rsTmp(g_Conn);

	CString szSQL;
	szSQL.Format(_T("DROP TABLE %s"),m_szTable);

	if( rsTmp.Open(szSQL) )
	{
		AfxMessageBox(_T("Drop Table Successfully."));
		rsTmp.Close();
	}
}

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

	CString szFileName;
	szFileName = szTable;
	szFileName.Delete(szFileName.GetLength()-1);
	szFileName.Delete(0);

	CString szFile;
	szFile = lpszFilePath;
	szFile += szFileName;
	szFile += _T(".xml");

	CString tmp;

	if( !IsExistDB(szFile) )
	{
		tmp.Empty();
		tmp.Format(_T("表[%s]不存在导入文件"),szFileName);
		m_listCtrl.AddString(tmp);
		UpdateData(FALSE);
	//	MessageBox(_T("不存在导入文件。"),szFileName);
		return;
	}

	ReadXML2Str(szFile,csText,xml);

	if( !xml.FindElem(szFileName) )
	{
		tmp.Empty();
		tmp.Format(_T("在表[%s]的导入文件文件中找不到主要标识符"),szFileName);
		m_listCtrl.AddString(tmp);
		UpdateData(FALSE);
		//AfxMessageBox(_T("没找到主要标识符."));
	}
	
	szFileName.Format(_T("正在插入%s..."),szFileName);

	CProgressWnd wndProgress(this, szFileName,TRUE);
    wndProgress.GoModal();
	wndProgress.SetRange(0,3000);
	long i = 0;

	CVORecordset rsTmp(g_Conn);
	CString szSQL;

	while( xml.FindChildElem(_T("ITEM")) )
	{
		szSQL.Empty();
		
		szSQL = xml.GetChildData();
	//	AfxMessageBox(szSQL);
		if( rsTmp.Open(szSQL) )
		{
			wndProgress.SetText(_T("已经插入%d条记录。"),i+1);
			wndProgress.SetPos(i+1);
			wndProgress.PeekAndPump();

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

	wndProgress.Close();	
}

void CCreateDBDlg::CreateTable(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);

	CVORecordset rsTmp(g_Conn);
	CString szSQL;

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

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

		if( szSQL.Find(m_szTable) != -1)
		{
			 if( rsTmp.Open(szSQL) )
			 {
				AfxMessageBox(_T("Create table successfully."));
			 }
			 else
			 {
				AfxMessageBox(_T("Exist table or Create table Failly."));
			 }
		}
	}
	rsTmp.Close();

}


⌨️ 快捷键说明

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