?? createdbdlg.cpp
字號(hào):
// 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("已經(jīng)刪除%d個(gè)表。"),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("正在創(chuàng)建表..."),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("已經(jīng)創(chuàng)建%d個(gè)表。"),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的數(shù)據(jù)超過(guò)了模擬器CDB文件的限制,不能插入。"));
UpdateData(FALSE);
//AfxMessageBox(_T("該表內(nèi)的數(shù)據(jù)超過(guò)了模擬器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("數(shù)據(jù)庫(kù)不存在."));
return;
}
CString csText;
CMarkup xml;
ReadXML2Str(lpszFileName,csText,xml);
CProgressWnd wndProgress(this, _T("正在導(dǎo)入錦囊數(shù)據(jù)..."),TRUE);
wndProgress.GoModal();
wndProgress.SetRange(0,3000);
long i = 0;
CString szID,szParentID,szName,szContent;
xml.FindElem(_T("營(yíng)銷錦囊"));
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("已經(jīng)導(dǎo)入%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("數(shù)據(jù)庫(kù)不存在."));
return;
}
int iCount = 0;
CString szFile;
CString szFileName;
CString szFindFile;
szFindFile.Format(_T("%sInsureDict*.xml"),lpszFilePath);
// AfxMessageBox(szFindFile);
CProgressWnd wndProgress(this, _T("正在導(dǎo)入詞典數(shù)據(jù)..."),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;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -