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

📄 syncview.cpp

📁 WinCE开发技巧与实例的配套源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// SyncView.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "SyncView.h"

#include "CESYNC.h"
#include "CERDA.h"
#include "SQLCEDemo.h"
#include "MainFrm.h"
#include "User.h"
#include "Common.h"
#include "SQLCEDemoView.h"
#include "Storagecard.h"
#include "Afxtempl.h"
#include "AdminPasswordDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSyncView

IMPLEMENT_DYNCREATE(CSyncView, CFormView)

CSyncView::CSyncView()
	: CFormView(CSyncView::IDD)
{
	//{{AFX_DATA_INIT(CSyncView)
	m_bRenew = FALSE;
	m_strDBLoginID = _T("");
	m_strDBName = _T("");
	m_strDBPassword = _T("");
	m_strDBServerName = _T("");
	m_strURL = _T("");
	m_strOutMsg = _T("");
	//}}AFX_DATA_INIT
}

CSyncView::~CSyncView()
{
}

void CSyncView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSyncView)
	DDX_Control(pDX, IDC_BUTTON_RETURN, m_btnQuit);
	DDX_Check(pDX, IDC_CHECK_RENEW, m_bRenew);
	DDX_Text(pDX, IDC_EDIT_MSG, m_strOutMsg);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSyncView, CFormView)
	//{{AFX_MSG_MAP(CSyncView)
	ON_BN_CLICKED(IDC_BUTTON_SYNCHRONIZATION, OnButtonSynchronization)
	ON_BN_CLICKED(IDC_BUTTON_ADVANCE, OnButtonAdvance)
	ON_BN_CLICKED(IDC_BTN_RETURN, OnBtnReturn)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSyncView diagnostics

#ifdef _DEBUG
void CSyncView::AssertValid() const
{
	CFormView::AssertValid();
}

void CSyncView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSyncView message handlers



void CSyncView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (bActivate &&
		pActivateView->GetRuntimeClass() == RUNTIME_CLASS(CSyncView) &&
		pDeactiveView->GetRuntimeClass() !=	RUNTIME_CLASS(CSyncView)) 
	{
		if (pDeactiveView->GetRuntimeClass() == RUNTIME_CLASS(CSQLCEDemoView))
		//如果来自登录页面,则返回就是退出
		{
			m_bEnableExit = FALSE;
		}
		else
		{
			m_bEnableExit = TRUE;
		}
		
		m_bRenew = TRUE;
		CEdit* hWndMsg = (CEdit*)GetDlgItem(IDC_EDIT_MSG);
		hWndMsg->SetWindowText(L"");
		CSQLCEDemoApp* pTheApp = (CSQLCEDemoApp*)AfxGetApp();
		
		CString strSection      = L"Sync";
		CString strStringItem	= L"URL";			
		
		//如果地址没有就取默认值
		m_strURL = pTheApp->GetProfileString(strSection, strStringItem);	
		if (m_strURL.GetLength() <= 1)
		{
			m_strURL = L"http://192.168.1.1/ce2/sscesa20.dll";
			m_strDBServerName = L"taling";	
			m_strDBName = L"cnemb";
			m_strDBLoginID = L"cnemb";
			m_strDBPassword = L"cnemb";
		}
		else//否则就继续取注册表
						
		{
			strStringItem    = L"DBServerName";	
			m_strDBServerName = pTheApp->GetProfileString(strSection, strStringItem);	
			strStringItem    = L"DBName";			
			m_strDBName = pTheApp->GetProfileString(strSection, strStringItem);	
			strStringItem    = L"DBLoginID";			
			m_strDBLoginID = pTheApp->GetProfileString(strSection, strStringItem);	
			strStringItem    = L"DBPassword";			
			m_strDBPassword = pTheApp->GetProfileString(strSection, strStringItem);	
		}

		m_bJustBackUpSuccess = FALSE;	//没有备份过数据,或者没成功

		UpdateData(FALSE);
	}
	//CFormView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}

void CSyncView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
//	m_strURL = L"http://192.168.0.1/ce2/sscesa20.dll";
//	UpdateData(FALSE);
}

void CSyncView::OnButtonSynchronization() 
{
	CSQLCEDemoApp* pTheApp = (CSQLCEDemoApp*)AfxGetApp();
	CString tmp_cs_checkcode,tmp_cust_code,tmp_competitor_code,tmp_check_number,new_tmp_check_number;

	if (pTheApp->m_pADORs->IsOpened()) 
		pTheApp->m_pADORs->Close();		
	if (pTheApp->m_pADOConn->IsConnected())
		pTheApp->m_pADOConn->Close();

	CEdit* hWndMsg = (CEdit*)GetDlgItem(IDC_EDIT_MSG);
	hWndMsg->SetWindowText(L"");
	
/*	if (!pTheApp->m_bErrorDB)	//错误的数据库或者没有,不用备份
	{
		m_bJustBackUpSuccess = (BackUp(this, FALSE) == Cnemb::Success);
	}
*/	//启动线程,通过this来访问class内部的元素
	CWinThread* hThread = AfxBeginThread((AFX_THREADPROC)DoSync, this);	
}


void CSyncView::DoSync(CSyncView *pThis)
{
	BOOL bAllSuccess = TRUE, bPush = FALSE;
	SYSTEMTIME  tt;			
	CSQLCEDemoApp *pTheApp = (CSQLCEDemoApp*)AfxGetApp();
	CEdit* hWndMsg = (CEdit*)pThis->GetDlgItem(IDC_EDIT_MSG);

	CString strURL, strDBServerName, strDBName, strDBLoginID, strDBPassword;	
	strURL = pThis->m_strURL;
	strDBServerName = pThis->m_strDBServerName;
	strDBName = pThis->m_strDBName;
	strDBLoginID = pThis->m_strDBLoginID;
	strDBPassword = pThis->m_strDBPassword;

	CButton* hWndButton	= (CButton*)pThis->GetDlgItem(IDC_CHECK_RENEW);
	BOOL bRenew = hWndButton->GetCheck();

	CButton* hWndSync = (CButton*)pThis->GetDlgItem(IDC_BUTTON_SYNCHRONIZATION);
	hWndSync->EnableWindow(FALSE);
	CButton* hWndReturn = (CButton*)pThis->GetDlgItem(IDC_BUTTON_RETURN);	
	hWndReturn->EnableWindow(FALSE);
	CButton* hWndAdvance = (CButton*)pThis->GetDlgItem(IDC_BUTTON_ADVANCE);	
	hWndAdvance->EnableWindow(FALSE);
	

	CCERDA* pCERDA = new CCERDA();
	pCERDA->setInternetURL(_bstr_t(strURL));
	pCERDA->setInternetLogin(L"");
	pCERDA->setInternetPassword(L"");	
	pCERDA->setLocalConnectionString(L"Provider=microsoft.sqlserver.oledb.ce.2.0;Data Source=palmheart.sdf");	
	CString strRemoteConn;
	strRemoteConn.Format(
		L"provider=sqloledb; data source=%s; Initial Catalog=%s; user id=%s; password=%s",
		strDBServerName, 
		strDBName, 
		strDBLoginID, 
		strDBPassword);	
	pCERDA->setConnectionString(_bstr_t(strRemoteConn));

	UpdateMsg(hWndMsg, L"正在连接服务器...");
	if (FAILED(pCERDA->CERDA->SubmitSQL(L"UPDATE SERVERTIME SET CurTime=Getdate()",
			_bstr_t(strRemoteConn))
		))				
	{
		UpdateMsg(hWndMsg, L"与服务器连接失败,请检查后再试一次");
		hWndSync->EnableWindow(TRUE);
		hWndReturn->EnableWindow(TRUE);
		hWndAdvance->EnableWindow(TRUE);
		return;
	}

	CString strLocalTable, strSQL, strErrorTable;	
	COleDateTime dtNow = INVALIDDATE;
	CString strUOCode;

	if (SearchDB(L"\\palmheart.sdf") && (!pTheApp->m_bErrorDB))
	{
		//dtnow有时间,就表示标记过开始时间了
		dtNow = COleDateTime::GetCurrentTime();
		strUOCode = CCommon::CreatOnlineCode(CUser::m_pUser_ID, dtNow);

		UpdateMsg(hWndMsg, L"正在标记在线状态...");
		strSQL.Format(L"INSERT INTO UserOnline(uo_code, user_id, begin_time) VALUES('%s', %d, '%s')", strUOCode, CUser::m_pUser_ID, CCommon::getFormatDateTime(dtNow, TRUE));
		if (FAILED(pCERDA->CERDA->SubmitSQL(
				_bstr_t(strSQL),
				_bstr_t(strRemoteConn))
				))			
		{
			UpdateMsg(hWndMsg, L"标记用户状态失败,请检查服务器数据表");
			hWndSync->EnableWindow(TRUE);
			hWndReturn->EnableWindow(TRUE);
			hWndAdvance->EnableWindow(TRUE);
			return;
		}

		UpdateMsg(hWndMsg, L"正在上传table4...");		
		strLocalTable = L"table4";
		strErrorTable = L"table4errortable";
		
		if (FAILED(pCERDA->CERDA->Push(
			_bstr_t(strLocalTable), 
			_bstr_t(strRemoteConn), 
			BATCHINGON)))
		{
			UpdateMsg(hWndMsg, L"table4失败");
			hWndSync->EnableWindow(TRUE);
			hWndReturn->EnableWindow(TRUE);
			hWndAdvance->EnableWindow(TRUE);
			return;
			bAllSuccess = FALSE;		
		}	

		
		UpdateMsg(hWndMsg, L"正在上传table3...");
		strLocalTable = L"table3";
		strErrorTable = L"table3errortable";
		if (FAILED(pCERDA->CERDA->Push(
			_bstr_t(strLocalTable), 
			_bstr_t(strRemoteConn), 
			BATCHINGON)))
		{
			UpdateMsg(hWndMsg, L"table3失败");
			hWndSync->EnableWindow(TRUE);
			hWndReturn->EnableWindow(TRUE);
			hWndAdvance->EnableWindow(TRUE);
			return;
			bAllSuccess = FALSE;		
		}	
	
	}

	if (!bAllSuccess)
	{
		UpdateMsg(hWndMsg, L"上传中发生错误,建议检查网络连接后再试一次");
/*		if (pThis->m_bJustBackUpSuccess)	//备份过才恢复
		{
			Restore(pThis, FALSE);
		}
*/		//因为一旦上传成功一部分,再恢复备份的数据库没有意义
		hWndSync->EnableWindow(TRUE);
		hWndReturn->EnableWindow(TRUE);
		hWndAdvance->EnableWindow(TRUE);
		return;
	}
	else
	{
		if (!pTheApp->m_bErrorDB)
		{
			UpdateMsg(hWndMsg, L"上传全部成功");
		}		
	}

	bPush = TRUE;		//上传到此成功

	AdoNS::_ConnectionPtr	pConnection = NULL;
	bAllSuccess = TRUE;
	
	CCESync* pCEsync = new CCESync();

	DeleteFile(L"\\palmheart.sdf");

	UpdateMsg(hWndMsg, L"正在新建本地数据库...");
	pCEsync->AddSubscription();

	UpdateMsg(hWndMsg, L"正在得到服务器时间...");
	strLocalTable = L"ServerTime";
	strSQL.Format(L"SELECT * from ServerTime");
	if (FAILED(pCERDA->CERDA->Pull(
		_bstr_t(strLocalTable), 
		_bstr_t(strSQL), 
		_bstr_t(strRemoteConn), 
		TRACKINGOFF, 
		_bstr_t(strErrorTable))))
	{
		//AfxMessageBox(L"ServerTime table failed");
		UpdateMsg(hWndMsg, L"servertime失败...");		
		bAllSuccess = FALSE;
	}
	
	UpdateMsg(hWndMsg, L"正在接收用户表...");	
	strLocalTable = L"userlist";
	strSQL.Format(L"SELECT * FROM userlist");
	strErrorTable = L"errortable";	
	if (FAILED(pCERDA->CERDA->Pull(
		_bstr_t(strLocalTable), 
		_bstr_t(strSQL), 
		_bstr_t(strRemoteConn), 
		TRACKINGOFF, 
		_bstr_t(strErrorTable))))
	{
		UpdateMsg(hWndMsg, L"接收用户表失败");
		bAllSuccess = FALSE;
	}	
//得到userid
	int nUser_ID;
	
	pConnection = pTheApp->m_pADOConn->OpenDB(_T("\\palmheart.sdf"));
	UpdateMsg(hWndMsg, L"正在验证用户...");
	
	if ((pConnection != NULL) && 
		(pTheApp->m_pADORs->PutConnection(pConnection)))
	{
		//得到userid
		nUser_ID = CUser::GetUserID(CUser::m_pUser_Name, pTheApp->m_pADORs);
		if (nUser_ID == -1)
		{
			UpdateMsg(hWndMsg, L"验证用户失败");
			hWndSync->EnableWindow(TRUE);
			hWndReturn->EnableWindow(TRUE);
			hWndAdvance->EnableWindow(TRUE);
			if (pTheApp->m_pADORs->IsOpened()) 
				pTheApp->m_pADORs->Close();		
			if (pTheApp->m_pADOConn->IsConnected())
				pTheApp->m_pADOConn->Close();
			DeleteFile(L"\\palmheart.sdf");
			return;	
		}

		COleDateTime dtTime = COleDateTime::GetCurrentTime();
		if ((pTheApp->m_pADORs->Open(L"SELECT CurTime FROM ServerTime ")) &&
			!(pTheApp->m_pADORs->IsRecordsetNULL()))
		{
			dtTime = pTheApp->m_pADORs->GetFieldDateValue(L"CurTime");
			
		tt.wYear=(WORD)dtTime.GetYear();
		tt.wMonth=(WORD)dtTime.GetMonth();
		tt.wDay=(WORD)dtTime.GetDay();
		tt.wHour=(WORD)dtTime.GetHour();
		tt.wMinute=(WORD)dtTime.GetMinute();
		tt.wSecond=(WORD)dtTime.GetSecond();
		tt.wDayOfWeek =(WORD)dtTime.GetDayOfWeek();

		CString dd;
 		dd.Format(L"正在设置系统时间:%.4d-%.2d-%.2d %.2d:%.2d:%.2d", 
 				tt.wYear,
 				tt.wMonth,
 				tt.wDay,
				tt.wHour,
 				tt.wMinute,
 				tt.wSecond);

			UpdateMsg(hWndMsg,dd);
			
		}

		UpdateMsg(hWndMsg, L"正在更新本地时间...");		


			//设置PDA时间和服务器时间相同
		if (::SetLocalTime(&tt)){  	
		UpdateMsg(hWndMsg, L"系统时间设置成功!!!");	
		}	

	}

	if (pTheApp->m_pADORs->IsOpened()) 
		pTheApp->m_pADORs->Close();		
	if (pTheApp->m_pADOConn->IsConnected())
		pTheApp->m_pADOConn->Close();

	if (dtNow == INVALIDDATE)	//没取过时间,就是还没标记过
	{
		dtNow = COleDateTime::GetCurrentTime();
		strUOCode = CCommon::CreatOnlineCode(nUser_ID, dtNow);

		UpdateMsg(hWndMsg, L"正在标记在线状态...");		
		strSQL.Format(L"INSERT INTO UserOnline(uo_code, user_id, begin_time) VALUES('%s', %d, '%s')", strUOCode, nUser_ID, CCommon::getFormatDateTime(dtNow, TRUE));
		if (FAILED(pCERDA->CERDA->SubmitSQL(
				_bstr_t(strSQL),
				_bstr_t(strRemoteConn))
				))			
		{
			UpdateMsg(hWndMsg, L"标记用户状态失败,请检查服务器数据表");
			hWndSync->EnableWindow(TRUE);
			hWndReturn->EnableWindow(TRUE);
			hWndAdvance->EnableWindow(TRUE);
			return;
		}
	}

⌨️ 快捷键说明

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